Convert java date to 18 dgit LDAP date

We have a requirement when we need to insert the accountExpires date in Active Directory. And AD accepts only the input date as Large integer (18-digit LDAP date). I have a date in a format yyyy-MM-dd(for example:) 2014-04-29, and I want to convert this Java date to LDAP date in 18-bit ie ( 130432824000000000) format .

Please let me know any work to convert the format below and fill in AD using the date format.

+4
source share
4 answers

This is a solution using Joda Time, as defined here :

- 100 (1 = ) 1 1601 UTC.

:

public final class Foo
{
    private static final DateTime LDAP_START_DATE
        = new DateTime(1601, 1, 1, 0, 0, DateTimeZone.UTC);

    public static void main(final String... args)
    {
        final DateTime now = DateTime.now();
        final Interval interval = new Interval(LDAP_START_DATE, now);
        System.out.println(interval.toDurationMillis() * 10000);
    }
}

now , .

+3

JDK

    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;
+3

LDAP API java.time(Java 8).

, - ( , :)

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;
    }

}

GitHub: https://github.com/PolishAirports/ldap-utils

+1

:

static String parseLdapDate(String ldapDate) {

            long nanoseconds = Long.parseLong(ldapDate);   // nanoseconds since target time that you want to convert to java.util.Date

            long mills = (nanoseconds / 10000000);

            long unix = (((1970 - 1601) * 365) - 3 + Math.round((1970 - 1601) / 4)) * 86400L;

            long timeStamp = mills - unix;

            Date date = new Date(timeStamp * 1000L); // *1000 is to convert seconds to milliseconds
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); // the format of your date
    //        sdf.setTimeZone(TimeZone.getTimeZone("GMT")); // give a timezone reference for formating (see comment at the bottom
            String formattedDate = sdf.format(date);

            return  formattedDate;
        }
0

Source: https://habr.com/ru/post/1530936/


All Articles