在我们的 Java 应用程序中,我们尝试从 UUID 获取 UNIX 时间版本1 https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_(date-time_and_MAC_address)。但它没有给出正确的日期时间值。
long time = uuid.timestamp();
time = time / 10000L; // Dividing by 10^4 as it's in 100 nanoseconds precision
Calendar c = Calendar.getInstance();
c.setTimeInMillis(time);
c.getTime();
有人可以帮忙吗?
从文档中timestamp()
:
生成的时间戳自 UTC 时间 1582 年 10 月 15 日午夜起以 100 纳秒为单位进行测量。
所以你需要从中抵消它。例如:
Calendar uuidEpoch = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
uuidEpoch.clear();
uuidEpoch.set(1582, 9, 15, 0, 0, 0); // 9 = October
long epochMillis = uuidEpoch.getTime().getTime();
long time = (uuid.timestamp() / 10000L) + epochMillis;
// Rest of code as before
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)