将java日期转换为18 dgit LDAP日期

将java日期转换为18 dgit LDAP日期,java,ldap,Java,Ldap,我们需要在Active directory中插入accountExpires日期。 AD只将输入日期作为大整数(18位LDAP日期)。 我有一个日期格式为yyyy-MM-dd(例如:2014-04-29),我想将这个Java日期转换为LDAP日期18位格式,即(13043282400000000) 请告诉我转换以下格式并使用当前日期格式填充广告的任何解决方案。根据找到的定义,这是一个使用Joda Time的解决方案: 时间戳是自UTC 1601年1月1日起的100纳秒间隔数(1纳秒=十亿分之一秒

我们需要在Active directory中插入accountExpires日期。 AD只将输入日期作为大整数(18位LDAP日期)。
我有一个日期格式为
yyyy-MM-dd
(例如:
2014-04-29
),我想将这个Java日期转换为LDAP日期18位格式,即(
13043282400000000


请告诉我转换以下格式并使用当前日期格式填充广告的任何解决方案。

根据找到的定义,这是一个使用Joda Time的解决方案:

时间戳是自UTC 1601年1月1日起的100纳秒间隔数(1纳秒=十亿分之一秒)

示例代码:

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

现在将此代码中的
替换为日期,应该设置日期。

这是基于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;

我花了一段时间使用java.time API编写LDAP时间戳转换器 (Java 8)

也许这个小工具会帮助像我这样的人(我一直在这里寻找合适的解决方案:)

我在GitHub上发布了完整的工具:
这对我来说很好:

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

您使用的是Joda Time还是Java的date API?
13043282400000000
是自1601年1月2日以来的刻度数。由于Java
日期
使用1970年1月1日00:00:00 GMT作为起点,因此您必须计算两者之间的差异。类似的问题,但方向相反:您确定
*10000
吗?1ns=1/100000000秒,100纳秒=1/10000000秒或1/10000毫秒我不应该在所需时区中获得另一个日历实例到t2吗?我的意思是,到“t1”。
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;
        }