从类型1 uuid获取unix时间戳 [英] get the unix timestamp from type 1 uuid

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

问题描述

在我们的java应用程序中,我们试图从类型1 uuid获取unix时间。
但它没有给出正确的日期时间值。

In our java application we are trying to get the unix time from the type 1 uuid. But its not giving the correct date time values.

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)

Fixed the divisor(10^4)

推荐答案

来自时间戳()的文档


自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

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

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