这里有几个步骤。第一个是将基于时间的 UUID 的时间戳(从 1582 年 10 月 15 日开始以 100 纳秒为单位)转换为与 Java 的日期功能兼容的时间戳(即从 1970 年 1 月 1 日开始的毫秒)。值得注意的是,您要求的精度优于毫秒。
接下来,我们需要将该日期解释为正确的时区。
最后,我们需要将其格式化为所需格式的文本。
这是代码:
// this is the difference between midnight October 15, 1582 UTC and midnight January 1, 1970 UTC as 100 nanosecond units
private static final long EPOCH_DIFFERENCE = 122192928000000000L;
private static final ZoneId GREENWICH_MEAN_TIME = ZoneId.of("GMT");
private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
.appendText(DAY_OF_WEEK, FULL)
.appendLiteral(", ")
.appendText(MONTH_OF_YEAR, FULL)
.appendLiteral(' ')
.appendValue(DAY_OF_MONTH)
.appendLiteral(", ")
.appendValue(YEAR, 4)
.appendLiteral(" at ")
.appendValue(CLOCK_HOUR_OF_AMPM)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.appendLiteral('.')
.appendFraction(NANO_OF_SECOND, 7, 7, false)
.appendLiteral(' ')
.appendText(AMPM_OF_DAY)
.appendLiteral(' ')
.appendZoneText(FULL)
.toFormatter(Locale.getDefault());
public static String formattedDateFromTimeBasedUuid(UUID uuid) {
ZonedDateTime date = timeBasedUuidToDate(uuid);
return FORMATTER.format(date);
}
public static ZonedDateTime timeBasedUuidToDate(UUID uuid) {
if (uuid.version() != 1) {
throw new IllegalArgumentException("Provided UUID was not time-based.");
}
// the UUID timestamp is in 100 nanosecond units.
// convert that to nanoseconds
long nanoseconds = (uuid.timestamp() - EPOCH_DIFFERENCE) * 100;
long milliseconds = nanoseconds / 1000000000;
long nanoAdjustment = nanoseconds % 1000000000;
Instant instant = Instant.ofEpochSecond(milliseconds, nanoAdjustment);
return ZonedDateTime.ofInstant(instant, GREENWICH_MEAN_TIME);
}
为了方便重用,我会将这些方法和常量放在一个实用程序类中。
几点注意事项:
- 这里有很多静态导入的常量。它们来自
java.time.format.TextStyle
和java.time.temporal.ChronoField
。
- 我用
DateTimeFormatterBuilder
的而不是更常见的DateTimeFormatter.forPattern(String)
. 我发现它更具可读性,并且愿意容忍由此产生的冗长。
- 我对您想要的格式进行了调整:您要求的时间是
2:06:30:001
;此代码产生2:06:30.001
- 秒和毫秒之间的小数点,而不是冒号。这更正确,但是如果您更喜欢冒号,只需将对应的内容更改.appendLiteral('.')
为传递冒号即可。
- 您经常会发现内联定义 DateTimeFormatters、ZoneIds 等的示例代码。这些类是线程安全且可重用的,因此为了获得最佳结果,您应该将它们定义为常量,就像我在这里所做的那样。您将获得更好的性能并减少内存使用量。
- 请注意,DataStax 驱动程序的
Uuids
类使用系统时钟的毫秒精度值作为输入,因此您只会在最后四个位置看到零,除非您实现自己的基于纳秒的变体。您可以使用 来做到这一点System.nanoTime()
,但有一些复杂性 -请查看 JavaDoc 上的注释以获取更多信息。
要确定两个 ZonedDateTimes 之间的时间量,您只需执行以下操作:
Duration duration = Duration.between(date1, date2);
该类Duration
有几个有用的方法可以用来解释结果。