Share all details related to your problem, including any error messages you may have received.
I’m trying to set the accountExpires attribute, and based my solution on the following post:
When the date is loaded from a delimited file there are instances where the accountExpires is set to the day after the date in the delimited file.
On the comments of the post this issue is also mentioned:
“I have used above code to update accountExpires in AD , Some how it is showing one day before date
Example : i have given in CSV 12/30/2024,Im getting in AD 12/29/2024 07:00:00 PM
I wanted to populate same date with 12:00:00 PM .”
The issue you’re experiencing could be due to timezone differences between your system and the Active Directory server. To fix this, you can set the hours, minutes, and seconds to the desired values (12:00:00 PM) in the Calendar instance for your date.
Try using below code
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.TimeZone;
public String getAcccountExpiresValue(String terminationDate) {
String ldapTimeStamp = "never";
if (null != terminationDate && !"never".equalsIgnoreCase(terminationDate)) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate currentDate = LocalDate.parse(terminationDate, formatter);
int day = currentDate.getDayOfMonth();
Month month = currentDate.getMonth();
int year = currentDate.getYear();
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.clear();
c.set(year, month.getValue() - 1, day, 12, 0, 0); // your date
long time1 = c.getTimeInMillis();
c.set(1601, 0, 1, 0, 0, 0); // Windows epoch start
long time2 = c.getTimeInMillis();
long ldap = (time1 - time2) * 10000;
ldapTimeStamp = Long.toString(ldap);
}
return ldapTimeStamp;
}