什么时间戳记格式这是什么?

问题描述

我一直在试图找出时间戳是这样的:

2021-01-06T11:00:00.459907-07:00

在特别是0.459907,我一直都在线,但我一直没能找到这一点。

解决方法

ISO 8601

格式为国际标准 ISO 8601。长篇故事在底部的链接中。

.459907 是几分之一秒。另一种描述方式是,时间是上午 11 点之后的 459 907 微秒(百万分之一秒)。时间部分之前的固定字母 T(我想是时间)是 ISO 8601 的特征。

您的字符串还包括一个日期,即 2021 年 1 月 6 日,以及与 UTC 的负 7 小时 00 分钟的偏移量。例如,在美国/埃德蒙顿和美国/丹佛时区(山地时间)中,每年的这个时候都会使用这种偏移量。偏移 -07:00 表示时间比 UTC 晚 7 小时。所以对应的UTC时间为18:00:00.459907。

链接

Wikipedia article: ISO 8601

,

the other answer Ole V.V.已经描述了这个日期时间字符串的格式。这个回答对他的回答进行了补充。

大多数语言都有库来直接/间接解析这种日期时间字符串,例如在 Java 中,您可以将此字符串解析为 OffsetDateTime,从而从中检索个人信息。

演示:

import java.time.Duration;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("2021-01-06T11:00:00.459907-07:00");
        long nanos = odt.getNano();
        System.out.println(odt + " has " + nanos + " nanoseconds");
        System.out.println(odt + " has " + odt.get(ChronoField.MICRO_OF_SECOND) + " microseconds");
        System.out.println(odt + " has " + odt.get(ChronoField.MILLI_OF_SECOND) + " milliseconds");
        System.out.println(nanos + " nanoseconds = " + TimeUnit.NANOSECONDS.toMicros(nanos) + " microseconds");
        System.out.println(nanos + " nanoseconds = " + TimeUnit.NANOSECONDS.toMillis(nanos) + " milliseconds");
        System.out.println(odt + " has a zone-offset of " + odt.getOffset() + " from UTC");
        System.out.println("Month: " + odt.getMonth());
        System.out.println("Day of month: " + odt.getDayOfMonth());
        System.out.println("Weekday name: " + odt.getDayOfWeek());
        System.out.println("Week of the year: " + odt.get(ChronoField.ALIGNED_WEEK_OF_YEAR));
        System.out
                .println("In terms of UTC," + odt + " can be represented as " + odt.atZoneSameInstant(ZoneOffset.UTC));

        OffsetDateTime odtWithNextCompleteSecond = OffsetDateTime.parse("2021-01-06T11:00:01-07:00");
        System.out.println("After " + Duration.between(odt,odtWithNextCompleteSecond).toNanos()
                + " nanoseconds,this time will change to " + odtWithNextCompleteSecond);
    }
}

输出:

2021-01-06T11:00:00.459907-07:00 has 459907000 nanoseconds
2021-01-06T11:00:00.459907-07:00 has 459907 microseconds
2021-01-06T11:00:00.459907-07:00 has 459 milliseconds
459907000 nanoseconds = 459907 microseconds
459907000 nanoseconds = 459 milliseconds
2021-01-06T11:00:00.459907-07:00 has a zone-offset of -07:00 from UTC
Month: JANUARY
Day of month: 6
Weekday name: WEDNESDAY
Week of the year: 1
In terms of UTC,2021-01-06T11:00:00.459907-07:00 can be represented as 2021-01-06T18:00:00.459907Z
After 540093000 nanoseconds,this time will change to 2021-01-06T11:00:01-07:00

具有 Java 工作知识的任何人都可以从 Trail: Date Time 了解日期时间 API。