java - 将 java 日期转换为 18 位 LDAP 日期

标签 java ldap

我们有一个要求,需要在 Active Directory 中插入 accountExpires 日期。 并且 AD 仅将输入日期作为大整数(18 位 LDAP 日期)。 我有一个格式为 yyyy-MM-dd 的日期(例如:2014-04-29),我想将此 Java 日期转换为 LDAP 日期 18 位格式,即(130432824000000000)。

请让我知道转换以下格式并使用当前日期格式填充 AD 的任何解决方法。

最佳答案

我花了一段时间使用 java.time API 编写 LDAP 时间戳转换器 (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

关于java - 将 java 日期转换为 18 位 LDAP 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22295792/

相关文章:

java - 背景与我其他 JLabels 的视野重叠

java - Android 中的 Json 和 ListView 崩溃

shell - 如何使用 ldapsearch 检查 UID 是否存在

python - 在 Django 中无需登录即可从 LDAP 填充用户

C# 如何向具有多个对象类的 LDAP 添加条目

authentication - LDAP 和 Active Directory 身份验证有什么区别?

java - Apache DBUtils queryRunner - where in 子句

java - Java新手,尝试使用日期作为属性

java - 当我与自定义 SWT 组件交互时它会闪烁

active-directory - 通过 Windows CMD 进行 LDAP 查询