Many times IIQ developer facing an issue to set accountexpires active directory application attribute value via beanshell code.
Please find below solution for setting accountexpires, pwdLastSet,accountExpires, LastLogon, LastLogonTimestamp, and LastPwdSet value using beanshell code.
The expected timestamp required for the accountexpires AD attribute is the 18-digit Active Directory timestamps, also named 'Windows NT time format', 'Win32 FILETIME or SYSTEMTIME' or NTFS file time. These are used in Microsoft Active Directory for pwdLastSet, accountExpires, LastLogon, LastLogonTimestamp, and LastPwdSet. The timestamp is the number of 100-nanosecond intervals (1 nanosecond = one billionth of a second) since Jan 1, 1601 UTC.
Please find below solution to calculate pwdLastSet, accountExpires, LastLogon, LastLogonTimestamp, and LastPwdSet from String value.
import java.time.LocalDate;
import java.time.Month;
import java.util.Calendar;
import java.util.TimeZone;
/**
* This method will convert date into the 18-digit Active Directory timestamps,
* also named 'Windows NT time format', 'Win32 FILETIME or SYSTEMTIME' or NTFS file time
* @param terminationDate
* @return ldapTimeStamp
*/
public String getAcccountExpiresValue(String terminationDate) {
// "2020-07-18" - example date
String ldapTimeStamp = "never";
if (null != terminationDate && !"never".equalsIgnoreCase(terminationDate)) {
LocalDate currentDate = LocalDate.parse(terminationDate);
// Get day from date
int day = currentDate.getDayOfMonth();
// Get month from date
Month month = currentDate.getMonth();
// Get year from date
int year = currentDate.getYear();
// Print the day, month, and year
System.out.println("Day: " + day);
System.out.println("Month: " + month.getValue());
System.out.println("Year: " + year);
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.clear();
c.set(year, month.getValue(), day); // your date
long time1 = c.getTimeInMillis();
c.set(1601, 1, 1);
long time2 = c.getTimeInMillis();
long ldap = (time1 - time2) * 10000;
System.out.println(ldap);
ldapTimeStamp = Long.toString(ldap);
}
return ldapTimeStamp;
}
You can validate the ldaptimestamp or the return String is correct or not follow below steps -
Please let me know if any one have any questions!!
Thanks,
IAM PDU