我们有一个要求,需要在 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/