从类型1 uuid获取unix时间戳

Thi*_*esh 3 java uuid unix-timestamp

在我们的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();
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

编辑:修正了除数(10 ^ 4)

Jon*_*eet 11

来自以下文档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
Run Code Online (Sandbox Code Playgroud)

  • 你无法想象我在你的一个答案中找到一个小错误我是多么自豪:) (4认同)
  • 你不是将 `uuid.timestamp()` 除以错误的数字 (10^8) 吗?时间戳给出了 100 ns (10^-7 s) 块的数量,因此您需要 10^4 个垃圾才能获得 1 ms (10^-3 s)。因此,您必须除以 10000L。OP 在他的代码中有同样的错误。 (3认同)