【发布时间】:2016-10-08 18:41:35
【问题描述】:
我正在解析来自 AD 的时间。有两种格式,即whenCreated,whenChanged的YMD LDAP时间戳,lastLogonTimestamp,pwdLastSet等的18位LDAP / FILETIME时间戳。因为我需要按时间分析数据。获取当地时间是有意义的。这是我为解析两种不同格式而编写的两个函数。我从Convert 18-digit LDAP Timestamps To Human Teadable Date Using Java引用的第二个函数中的计算
public static String parseLdapDate(String ldapDate) {
String[] parts = ldapDate.split("[.]");
String dateTimePart = parts[0]; //take the date string before .0Z
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT")); //Z means UTC time, to the local timezone
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date tempDate = sdf.parse(dateTimePart); //parse the string to a date
return formatter.format(tempDate); //format it as what we want
} catch (ParseException ex) {
System.out.println("Parsing LDAP Date exception \n");
ex.printStackTrace();
}
return null;
}
public static String parseLdapTimestamp(String ldapTimestamp) {
long nanoseconds = Long.parseLong(ldapTimestamp); // 100 nanoseconds
long mills = (nanoseconds/10000000); // To seconds
long unix = (((1970-1601)*365)-3+Math.round((1970-1601)/4))*86400L;
long timeStamp = mills - unix;
Date date = new Date(timeStamp*1000L); // To milliseconds
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
return sdf.format(date);
}
我有一个示例 20150520143936.0Z,它被转换为“2015-05-20 16:39:36”。 例如131097986571852097,它被转换为“2016-06-07 18:44:17”,而http://www.epochconverter.com/ldap告诉我这是格林威治标准时间,当地时间是“2016-06-07 20:44:17” .如果我评论设置时区的代码,我会得到当地时间。
所以现在我很困惑,sdf.setTimeZone(TimeZone.getTimeZone("GMT")); 给了我本地时区或世界时。我在想AD是否在通用时间存储whenCreated,在本地时间存储lastLogonTimestamp。但在函数中,我将它们解析为字符串。时区没有符号。如果我在第二个函数中评论这句话,当我在另一个地方访问 LDAP 目录时,我会得到这两个属性的本地时间吗?
【问题讨论】:
-
在第二个函数中注释了
setTimeZone。当解析参数为0时,即1601-01-01 00:00:00 GMT,返回值为1601-01-01 01:00:00。根据epochconverter.com/ldap,在我的时区中,真实值为 1601-01-01, 00:53:28。大致正确。
标签: java datetime active-directory ldap gmt