从 UUID 版本 1 获取 UNIX 时间戳 [英] Get the UNIX timestamp from UUID version 1

查看:74
本文介绍了从 UUID 版本 1 获取 UNIX 时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的 Java 应用程序中,我们试图从 UUID 中获取 UNIX 时间 版本 1.但它没有给出正确的日期时间值.

In our Java application we are trying to get the UNIX time from the UUID version 1. But it's not giving the correct date time values.

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 纳秒为单位进行测量.

The resulting timestamp is measured in 100-nanosecond units since midnight, October 15, 1582 UTC.

所以你需要抵消它.例如:

So you need to offset it from that. For example:

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

这篇关于从 UUID 版本 1 获取 UNIX 时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆