如何使用 java.time.ZonedDateTime 生成 UTC 格式的原始 long 值
How to use java.time.ZonedDateTime to generate a primitive long value in UTC
什么是一种相当有效但最重要的是一种将 UTC 时间戳生成为 long 值(java 原始类型)的简洁明了的方法。
我只需要它精确到最精确的毫秒值。
我更愿意使用 java.time.ZonedDateTime,因为它是一种获取 UTC 时间戳的简洁方法,并且不需要任何我不想使用的外部日期库,例如 Joda 或 Apache。
组成原始long值的19位数字将被分解(格式化)如下并举例:
2015 - 4 digit year with range 1000 - 9223.
- no zero padding allowed to ensure length is always
a 19 digit long value
- 1000 is the minimum value of a four digit year
- 9223 is the maximum value based on first four digits
of Long.MAX_VALUE or 9223372036854775807
01 - 2 digit month with range 01 - 12
- zero padded to ensure 2 digits always
- jan is 01 and dec is 12
20 - 2 digit day with range 01 - 31
- zero padded to ensure 2 digits always
07 - 2 digit hour with range 00 - 23
- zero padded to ensure 2 digits always
- uses a 24 hour clock
- 00 is 12 AM while 23 is 11 PM
00 - 2 digit minute with range 00 - 59
- zero padded to ensure 2 digits always
00 - 2 digit second with range 00 - 59
- zero padded to ensure 2 digits always
999 - 3 digit milliseconds with range 000 - 999
- zero padded to ensure 3 digits always
99 - only 2 digits left over from primitive long. Ignore these as they
will be used internally as specialized counters within my program
我的出发点是使用以下内容:
ZonedDateTime value = ZonedDateTime.now( ZoneOffset.UTC );
ZonedDateTime value = ZonedDateTime.now(ZoneOffset.UTC);
System.out.println(value.get(ChronoField.NANO_OF_SECOND));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
long time = Long.parseLong(formatter.format(value)) * 100;
System.out.println(time);
什么是一种相当有效但最重要的是一种将 UTC 时间戳生成为 long 值(java 原始类型)的简洁明了的方法。
我只需要它精确到最精确的毫秒值。
我更愿意使用 java.time.ZonedDateTime,因为它是一种获取 UTC 时间戳的简洁方法,并且不需要任何我不想使用的外部日期库,例如 Joda 或 Apache。
组成原始long值的19位数字将被分解(格式化)如下并举例:
2015 - 4 digit year with range 1000 - 9223.
- no zero padding allowed to ensure length is always
a 19 digit long value
- 1000 is the minimum value of a four digit year
- 9223 is the maximum value based on first four digits
of Long.MAX_VALUE or 9223372036854775807
01 - 2 digit month with range 01 - 12
- zero padded to ensure 2 digits always
- jan is 01 and dec is 12
20 - 2 digit day with range 01 - 31
- zero padded to ensure 2 digits always
07 - 2 digit hour with range 00 - 23
- zero padded to ensure 2 digits always
- uses a 24 hour clock
- 00 is 12 AM while 23 is 11 PM
00 - 2 digit minute with range 00 - 59
- zero padded to ensure 2 digits always
00 - 2 digit second with range 00 - 59
- zero padded to ensure 2 digits always
999 - 3 digit milliseconds with range 000 - 999
- zero padded to ensure 3 digits always
99 - only 2 digits left over from primitive long. Ignore these as they
will be used internally as specialized counters within my program
我的出发点是使用以下内容:
ZonedDateTime value = ZonedDateTime.now( ZoneOffset.UTC );
ZonedDateTime value = ZonedDateTime.now(ZoneOffset.UTC);
System.out.println(value.get(ChronoField.NANO_OF_SECOND));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
long time = Long.parseLong(formatter.format(value)) * 100;
System.out.println(time);