从类型1 uuid获取unix时间戳
在我们的java应用程序中,我们试图从类型1 uuid获取unix时间。 但它没有给出正确的日期时间值。
long time = uuid.timestamp(); time = time / 10000L; // Dividing by 10^4 as its in 100 nanoseconds precision Calendar c = Calendar.getInstance(); c.setTimeInMillis(time); c.getTime();
有人可以帮忙吗?
编辑:修正了除数(10 ^ 4)
来自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
如果您使用datastax驱动程序,它是:
UUIDs.unixTimestamp(UUID)