如何使用Java从UUID中提取日期? [英] How do I extract a date from a UUID using Java?

查看:491
本文介绍了如何使用Java从UUID中提取日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 UUID 转换为日期格式 2011-04-22

How to convert the UUID to date format 2011-04-22 ?

例如,我有这样的UUID

For example, I have UUID like this

118ffe80-466b-11e1-b5a5-5732cf729524.

如何将此转换为日期格式?

How to convert this to date format?

我试过

 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();
        }

输出我得到: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";


推荐答案

UUID 说关于时间戳记字段:

The javadoc for UUID says the following about the timestamp field:


从UUID的time_low,time_mid和time_hi字段构建60位时间戳值。结果时间戳从1582年10月15日UTC 15分钟以来以100纳秒为单位测量

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.

强调我的)

Java时间戳记是自1970-01-01以来的毫秒。为了从UUID获得有意义的日期,您需要做两件事情:从100ns转换为1ms精度(除以10000),并从1582-10-15转换为1970-01-01,您可以执行通过添加常量值。

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.

WolframAlpha告诉我们,1582-10-15对应于 -12219292800 的UNIX时间戳,所以要获得正确的日期,您必须将$ code> 12219292800 添加到除以10000后获得的毫秒数。

WolframAlpha tells us that 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.

作为备注: / p>

As a side note:


时间戳记值仅在具有版本类型为1的基于时间的UUID中有意义。如果此UUID不是基于时间的UUID那么这个方法会抛出UnsupportedOperationException。

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.

...所以确保你的代码只有遇到Type 1 UUID,或者可以处理它们没有时间戳。

...so make sure your code either only ever encounters Type 1 UUID's, or can handle that they don't have a timestamp.

这篇关于如何使用Java从UUID中提取日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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