如何转换UUID为日期格式2011-04-22?
例如,我有这样的UUID
118ffe80-466b-11e1-b5a5-5732cf729524.
Run Code Online (Sandbox Code Playgroud)
如何将其转换为日期格式?
我试过了
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();
}
Run Code Online (Sandbox Code Playgroud)
输出我得到:4294744/11/02
同样的情况对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";
Run Code Online (Sandbox Code Playgroud)
Bar*_*end 18
UUID关于时间戳字段的javadoc 说明如下:
60位时间戳值是根据此UUID的time_low,time_mid和time_hi字段构造的.从UTC时间1582年10月15日午夜开始,生成的时间戳以100纳秒为单位进行测量.
(强调我的)
自1970-01-01以来,Java时间戳以毫秒为单位.为了从UUID获得有意义的日期,你需要做两件事:从100ns转换为1ms精度(除以10000)和从1582-10-15转换到1970-01-01,你可以做通过添加常量值.
WolframAlpha告诉我们 1582-10-15对应于UNIX时间戳-12219292800,因此要获得正确的日期,必须加上12219292800除以10000后得到的毫秒数.
作为旁注:
时间戳值仅在基于时间的UUID中有意义,该UUID具有版本类型1.如果此UUID不是基于时间的UUID,则此方法将抛出UnsupportedOperationException.
...所以确保你的代码只遇到Type 1 UUID,或者可以处理它们没有时间戳.