如何使用 Java 从 UUID 中提取日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15179428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I extract a date from a UUID using Java?
提问by BALASCJP
How to convert the UUID
to date format 2011-04-22
?
如何转换UUID
为日期格式2011-04-22
?
For example, I have UUID like this
例如,我有这样的 UUID
118ffe80-466b-11e1-b5a5-5732cf729524.
How to convert this to date format?
如何将其转换为日期格式?
I tried
我试过
String uuid="118ffe80-466b-11e1-b5a5-5732cf729524";
UUID uid = UUID.fromString(uuid);
long ls=convertTime(uid.timeStamp()); // it returns long value
public String convertTime(long time){
System.out.println("====="+time);
Date date = new Date(time);
Format format = new SimpleDateFormat("yyyy/MM/dd");
return format.format(date).toString();
}
output I got:4294744/11/02
我得到的输出:4294744/11/02
Same case working fine for perl
相同的案例适用于 perl
$uuid='ef802820-46b3-11e2-bf3a-47ef6b3e28e2';
$uuid =~ s/-//g;
my $timelow = hex substr( $uuid, 2 * 0, 2 * 4 );
my $timemid = hex substr( $uuid, 2 * 4, 2 * 2 );
my $version = hex substr( $uuid, 2 * 6, 1 );
my $timehi = hex substr( $uuid, 2 * 6 + 1, 2 * 2 - 1 );
my $time = ( $timehi * ( 2**16 ) + $timemid ) * ( 2**32 ) + $timelow;
my $epoc = int( $time / 10000000 ) - 12219292800;
my $nano = $time - int( $time / 10000000 ) * 10000000;
#$time_date = scalar localtime $epoc;
#print strftime( '%d-%m-%Y %H:%M:%S', localtime($epoc) );
#print "\n Time: ", scalar localtime $epoc, " +", $nano / 10000, "ms\n";
回答by Barend
The javadoc for UUID
says the following about the timestamp field:
javadoc forUUID
说明了以下有关时间戳字段的内容:
The 60 bit timestamp value is constructed from the time_low, time_mid, and time_hi fields of this UUID. The resulting timestamp is measured in 100-nanosecond units since midnight, October 15, 1582 UTC.
60 位时间戳值由该 UUID 的 time_low、time_mid 和 time_hi 字段构造而成。生成的时间戳从 UTC 1582 年 10 月 15 日午夜起以 100 纳秒为单位进行测量。
(emphasis mine)
(强调我的)
The Java timestamp is in milliseconds since 1970-01-01. In order to get a meaningful date from a UUID, you'll need to do two things: convert from 100ns to 1ms precision (divide by 10000) and rebase from 1582-10-15 to 1970-01-01, which you can do by adding a constant value.
Java 时间戳以 1970-01-01 以来的毫秒为单位。为了从 UUID 获得有意义的日期,您需要做两件事:从 100ns 转换为 1ms 精度(除以 10000)和从 1582-10-15 变基到 1970-01-01,您可以这样做通过添加一个常数值。
WolframAlpha tells usthat 1582-10-15 corresponds to a UNIX timestamp of -12219292800
, so to get the correct date, you must add 12219292800
to the number of milliseconds you got after dividing by 10000.
WolframAlpha 告诉我们1582-10-15 对应于 的 UNIX 时间戳-12219292800
,因此要获得正确的日期,您必须加上12219292800
除以 10000 后得到的毫秒数。
As a side note:
作为旁注:
The timestamp value is only meaningful in a time-based UUID, which has version type 1. If this UUID is not a time-based UUID then this method throws UnsupportedOperationException.
时间戳值仅在版本类型为 1 的基于时间的 UUID 中有意义。如果此 UUID 不是基于时间的 UUID,则此方法抛出 UnsupportedOperationException。
...so make sure your code either only ever encounters Type 1 UUID's, or can handle that they don't have a timestamp.
...所以请确保您的代码要么只遇到类型 1 UUID,要么可以处理它们没有时间戳的情况。