Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert java date to 18 dgit LDAP date

Tags:

java

ldap

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.

like image 798
user3400969 Avatar asked Dec 19 '22 17:12

user3400969


2 Answers

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

like image 50
Maciej Marczuk Avatar answered Jan 13 '23 07:01

Maciej Marczuk


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;
like image 28
Evgeniy Dorofeev Avatar answered Jan 13 '23 07:01

Evgeniy Dorofeev