首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从UUID类型1中获取日期/时间

如何从UUID类型1中获取日期/时间
EN

Stack Overflow用户
提问于 2018-07-01 01:42:49
回答 3查看 7.7K关注 0票数 3

我已经包含了以下UUID库

代码语言:javascript
运行
复制
compile group: 'com.fasterxml.uuid', name: 'java-uuid-generator', version: '3.1.5'

在我的体型里。

我有一些这样的代码

代码语言:javascript
运行
复制
        NoArgGenerator timeBasedGenerator = Generators.timeBasedGenerator()
        UUID tuid = timeBasedGenerator.generate()
        Timestamp timestamp = new Timestamp ((tuid.timestamp()/1000) as Long)
        Date dateTime = new Date (timestamp.getTime())

但是,当我尝试查看日期时,它并不像它应该是的那样,例如,当今天是30/06/2018时,我得到uid fef57eca-7c8b-11e8-bedd-992c2ac3197a was Sun Feb 06 07:55:54 GMT 6327

有人知道如何使用fasterxml.uuid库从基于时间的UUID中正确提取实际日期和时间吗?

但是被难住了

ps转而尝试这样做

代码语言:javascript
运行
复制
        UUID tuid = timeBasedGenerator.generate()
        Long t = tuid.timestamp()
        Timestamp timestamp = new Timestamp (t)
        Date dateTime = new Date (timestamp.getTime())

它给出了Thu Aug 14 11:11:40 BST 4359073的uid ff79d7d9-7cb5-11e8-976c-6ba57a5e9636和日期

EN

回答 3

Stack Overflow用户

发布于 2020-12-22 19:01:15

要获得作为java.util.Instant的100 as的完整精度,您可以执行以下操作:

代码语言:javascript
运行
复制
private static final long NUM_HUNDRED_NANOS_IN_A_SECOND = 10_000_000L;

private static final long NUM_HUNDRED_NANOS_FROM_UUID_EPOCH_TO_UNIX_EPOCH = 122_192_928_000_000_000L;


/**
 * Extracts the Instant (with the maximum available 100ns precision) from the given time-based (version 1) UUID.
 *
 * @return the {@link Instant} extracted from the given time-based UUID
 * @throws UnsupportedOperationException If this UUID is not a version 1 UUID
 */
public static Instant getInstantFromUUID(final UUID uuid) {
    final long hundredNanosSinceUnixEpoch = uuid.timestamp() - NUM_HUNDRED_NANOS_FROM_UUID_EPOCH_TO_UNIX_EPOCH;
    final long secondsSinceUnixEpoch = hundredNanosSinceUnixEpoch / NUM_HUNDRED_NANOS_IN_A_SECOND;
    final long nanoAdjustment = ((hundredNanosSinceUnixEpoch % NUM_HUNDRED_NANOS_IN_A_SECOND) * 100);
    return Instant.ofEpochSecond(secondsSinceUnixEpoch, nanoAdjustment);
}
票数 2
EN

Stack Overflow用户

发布于 2018-07-01 22:32:55

我在网上又搜索了一下。

我构建了以下“简单实用工具”类,可以根据需要进行扩展:

代码语言:javascript
运行
复制
import com.fasterxml.uuid.Generators
import com.fasterxml.uuid.NoArgGenerator

class UuidUtil {

    static final NoArgGenerator timeBasedGenerator = Generators.timeBasedGenerator()


    /**
     * From UUID javadocs the resulting timestamp is measured in 100-nanosecond units since midnight, October 15, 1582 UTC
     * timestamp() from UUID is measured in 100-nanosecond units since midnight, October 15, 1582 UTC
     *
     * The Java timestamp in milliseconds since 1970-01-01 as baseline
     *
     * @return
     */
    static long getStartOfUuidRelativeToUnixEpochInMilliseconds () {
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-0"))
        c.set(Calendar.YEAR, 1582)
        c.set(Calendar.MONTH, Calendar.OCTOBER)
        c.set(Calendar.DAY_OF_MONTH, 15)
        c.set(Calendar.HOUR_OF_DAY, 0)
        c.set(Calendar.MINUTE, 0)
        c.set(Calendar.SECOND, 0)
        c.set(Calendar.MILLISECOND, 0)

        return c.getTimeInMillis()
    }

    //https://www.wolframalpha.com/input/?i=convert+1582-10-15+UTC+to+unix+time
    final static long START_OF_UUID_RELATIVE_TO_UNIX_EPOCH_SECONDS = -12219292800L
    final static long START_OF_UUID_RELATIVE_TO_UNIX_EPOCH_MILLIS = -12219292800L * 1000L

    /**
     * timestamp() from UUID is measured in 100-nanosecond units since midnight, October 15, 1582 UTC,
     * so we must convert for 100ns units to millisecond procession
     * @param tuid
     * @return
     */
    static long getMillisecondsFromUuid (UUID tuid) {

        assert tuid.version() == 1      //ensure its a time based UUID

        // timestamp returns in 10^-7 (100 nano second chunks), 
        // java Date constructor  assumes 10^-3 (millisecond precision)
        // so we have to divide by 10^4 (10,000) to get millisecond precision  
        long milliseconds_since_UUID_baseline = tuid.timestamp() /10000L

    }

    static getDateFromUuid (UUID tuid) {
        // Allocates a Date object and initializes it to represent the specified number of milliseconds since the 
        // standard java (unix) base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT
        // have to add relative offset from UUID start date of unix epoch to get start date in unix time milliseconds 
        new Date (getMillisecondsFromUuid (tuid) + START_OF_UUID_RELATIVE_TO_UNIX_EPOCH_MILLIS )
    }

    static UUID getTimeBasedUuid () {
        UUID tuid = timeBasedGenerator.generate()
    }

}

我已经添加了解释性注释,这样您就可以了解如何将UUID timestamp()方法处理成适用于常规Java日期和时间处理的格式。

为什么Java UUID类不能提供人们期望的方法,使基于时间的UUID可以与基于普通unix纪元的普通java日期/时间格式互操作,这对我来说是一个谜。

我使用上面的静态方法运行了一个小测试脚本:

代码语言:javascript
运行
复制
println "get start of epoch in milliseconds " + UuidUtil.getStartOfUuidRelativeToUnixEpochInMilliseconds()
assert UuidUtil.START_OF_UUID_RELATIVE_TO_UNIX_EPOCH_MILLIS == UuidUtil.startOfUuidRelativeToUnixEpochInMilliseconds

UUID tuid = UuidUtil.timeBasedUuid

println "uid : $tuid"

Date date = UuidUtil.getDateFromUuid(tuid)
println "extracted date from uid is " + UuidUtil.getDateFromUuid(tuid)

然后得到了这个

代码语言:javascript
运行
复制
get start of epoch in milliseconds -12219292800000
uid : 43acb588-7d39-11e8-b37b-59f77bf2d333
extracted date from uid is Sun Jul 01 15:15:53 BST 2018

对于脚本运行的时间,它看起来是正确的。

票数 1
EN

Stack Overflow用户

发布于 2020-06-18 13:38:43

库' UUID -creator‘有一个实用程序类,可以帮助提取时间和节点id等UUID部分。请参见此示例:

代码语言:javascript
运行
复制
long milliseconds = UuidUtil.extractUnixMilliseconds(uuid);

项目:https://github.com/f4b6a3/uuid-creator

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51117194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档