We have a requirment where we need to insert accountExpires date in Active directory.
And AD only take input date as Large integer (18 digit LDAP date).
I have a date in format yyyy-MM-dd
(for ex : 2014-04-29
) and I want to convert this Java date into LDAP date 18 digit format i.e (130432824000000000
).
Please let me know any work around to convert the below format and populate the AD with curre nt date format.
I've spent a while on writing LDAP timestamp converter using java.time API (Java 8).
Maybe this little tool will help someone like me (I've been here when I was looking for proper solution :)
import java.time.*;
public class LdapTimestampUtil {
/**
* Microsoft world exist since since Jan 1, 1601 UTC
*/
public static final ZonedDateTime LDAP_MIN_DATE_TIME = ZonedDateTime.of(1601, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"));
public static long instantToLdapTimestamp(Instant instant) {
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
return zonedDateToLdapTimestamp(zonedDateTime);
}
public static long localDateToLdapTimestamp(LocalDateTime dateTime) {
ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
return zonedDateToLdapTimestamp(zonedDateTime);
}
public static long zonedDateToLdapTimestamp(ZonedDateTime zonedDatetime) {
Duration duration = Duration.between(LDAP_MIN_DATE_TIME, zonedDatetime);
return millisecondsToLdapTimestamp(duration.toMillis());
}
/**
* The LDAP timestamp is the number of 100-nanoseconds intervals since since Jan 1, 1601 UTC
*/
private static long millisecondsToLdapTimestamp(long millis) {
return millis * 1000 * 10;
}
}
I've published full tool on GitHub: https://github.com/PolishAirports/ldap-utils
this is JDK based solution
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
c.clear();
c.set(2014, 3, 29);
long t1 = c.getTimeInMillis();
c.set(1601, 0, 1);
long t2 = c.getTimeInMillis();
long ldap = (t1 - t2) * 10000;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With