简体   繁体   English

了解具体的UTC时间格式YYYY-MM-DDTHH:MM:SS.SSSZ

[英]Understanding specific UTC time format YYYY-MM-DDTHH:MM:SS.SSSZ

I have two related questions.我有两个相关的问题。

Assume a program running in BST generates a date time value for current time in UTC YYYY-MM-DDTHH:MM:SS.SSSZ format假设在 BST 中运行的程序以 UTC YYYY-MM-DDTHH:MM:SS.SSSZ 格式生成当前时间的日期时间值

Also assume current time in London is 2016-06-01 12:33:54还假设伦敦的当前时间是 2016-06-01 12:33:54

  1. if the current time given by the program is 2016-06-01T11:33:54.000Z , is the program wrong?如果程序给出的当前时间是 2016-06-01T11:33:54.000Z ,是不是程序出错了?

  2. how is summer offset for BST noted in the corresponding time format for YYYY-MM-DDTHH:MM:SS.SSSZ BST 的夏季偏移如何以 YYYY-MM-DDTHH:MM:SS.SSSZ 的相应时间格式记录

I assume YYYY-MM-DDTHH:MM:SS+0001 Am I correct ?我假设 YYYY-MM-DDTHH:MM:SS+0001 我正确吗?

Firstly please have a read of the iso8601 information.首先请阅读iso8601信息。 It's becoming more common place to deal with times in different time zones (eg server time zone and client time zone) and the standard is really useful.处理不同时区(例如服务器时区和客户端时区)的时间变得越来越普遍,并且该标准非常有用。

In particular please read about UTC or "Zulu" time here .特别是请在此处阅读 UTC 或“祖鲁”时间。

  1. The program is correct, since the London time is one hour ahead of "UTC" time in summer程序是正确的,因为伦敦时间比夏季的“UTC”时间提前一小时

  2. The trailing 'Z' is a short notation for UTC (Zulu).尾随的“Z”是 UTC(祖鲁语)的缩写。 You could also write "+00:00" instead of 'Z'.你也可以写“+00:00”而不是“Z”。 The SS.SSS refer to seconds and milliseconds - not related to the time zone. SS.SSS 指的是秒和毫秒 - 与时区无关。 In devnull's comment he shows you how to apply an offset for summer.在 devnull 的评论中,他向您展示了如何为夏季应用偏移量。

Edit :编辑

There's been some discussion in the comments about whether iso8601 timezone includes timezone or not, and whether timezone will in fact be printed out.评论中有一些关于iso8601时区是否包含时区以及时区是否会被打印出来的讨论。

This depends completely on the date/time implementation.这完全取决于日期/时间实现。 If we are using SimpleDateFormat then timezone is supported and will be printed.如果我们使用SimpleDateFormat则支持时区并将被打印。

Here's a code example to illustrate这是一个代码示例来说明

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(formatter.format(new Date()));
formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
System.out.println(formatter.format(new Date()));

Output输出

2016-06-02T12:53:14.924Z
2016-06-02T13:53:14.925+01:00

Naturally, if you are using a different date/time library such as joda-time , then the implentation details will be different.自然,如果您使用不同的日期/时间库,例如joda-time ,那么实现细节将有所不同。

Edit: As @DerrylThomas pointed out with SimpleDateFormat wise to use lower case y for years - unless it's intended to use week year - explained in a bit of detail in another answer to a similar question https://stackoverflow.com/a/56911450 .编辑:正如@DerrylThomas 指出的, SimpleDateFormat明智地使用小写y多年 - 除非它打算使用周年 - 在另一个类似问题的答案中详细解释了https://stackoverflow.com/a/56911450 .

if the current time given by the program is 2016-06-01T11:33:54.000Z , is the program wrong?如果程序给出的当前时间是 2016-06-01T11:33:54.000Z ,是不是程序出错了?

The format is correct and conforms to ISO 8601 but it does not represent Europe/London time.格式正确且符合ISO 8601,但不代表欧洲/伦敦时间。 In London, in 2016 , the DST started at Sunday, March 27, 1:00 am and ended at Sunday, October 30, 2:00 am and therefore a date-time representation for Europe/London during this time should have a timezone offset of +01:00 hours.2016年的伦敦,夏令时从3 月 27 日星期日凌晨 1 点开始,到 10 月 30 日星期日凌晨 2 点结束,因此在此期间欧洲/伦敦的日期时间表示应具有时区偏移+01:00小时。 The Z at the end specifies Zulu time which is UTC time and thus has a timezone offset of +00:00 hours.末尾的Z指定Zulu时间,即 UTC 时间,因此时区偏移为+00:00小时。 The same instant can be represented for Europe/London as 2016-06-01T12:33:54+01:00 .欧洲/伦敦的同一时刻可以表示为2016-06-01T12:33:54+01:00

java.time时间

The java.util date-time API and their formatting API, SimpleDateFormat are outdated and error-prone. java.util日期时间 API 及其格式化 API SimpleDateFormat已过时且容易出错。 It is recommended to stop using them completely and switch to java.time , the modern date-time API * .建议完全停止使用它们并切换到java.time现代日期时间 API *

Even Joda-Time should not be used anymore.甚至Joda-Time也不应该再使用了。 Notice the following note at the Home Page of Joda-Time请注意Joda-Time 主页上的以下说明

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (JSR-310). Joda-Time 是 Java SE 8 之前的 Java 事实标准日期和时间库。现在要求用户迁移到 java.time (JSR-310)。

java.time API is based on ISO 8601 and the date-time string, 2016-06-01T11:33:54.000Z can be parsed into java.time.ZonedDateTime and java.time.OffsetDateTime without needing a date-time parsing/formatting type. java.time API 基于ISO 8601和日期时间字符串, 2016-06-01T11:33:54.000Z可以解析为java.time.ZonedDateTimejava.time.OffsetDateTime而无需日期时间解析/格式化类型。

Demo:演示:

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdt = ZonedDateTime.parse("2016-06-01T11:33:54.000Z");
        System.out.println(zdt);

        ZoneId zoneId = ZoneId.of("Europe/London");
        ZonedDateTime zdtInLondon = zdt.withZoneSameInstant(zoneId);
        System.out.println(zdtInLondon);
    }
}

Output:输出:

2016-06-01T11:33:54Z
2016-06-01T12:33:54+01:00[Europe/London]

How to deal with Daylight Saving Time (DST)?如何处理夏令时 (DST)?

As mentioned earlier, the date-time string, 2016-06-01T11:33:54.000Z can also be parsed into java.time.OffsetDateTime without needing a date-time parsing/formatting type.如前所述,日期时间字符串2016-06-01T11:33:54.000Z也可以解析为java.time.OffsetDateTime而无需日期时间解析/格式化类型。 However, OffsetDateTime has been designed to deal with a fixed timezone offset whereas ZonedDateTime has been designed to deal with a timezone and thus it take care of DST automatically.然而, OffsetDateTime被设计为处理固定的时区偏移,ZonedDateTime被设计为处理时区,因此它会自动处理 DST。 You can convert a ZonedDateTime to OffsetDateTime using ZonedDateTime#toOffsetDateTime if required.您可以将转换ZonedDateTimeOffsetDateTime使用ZonedDateTime#toOffsetDateTime如果需要的话。

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS z", Locale.ENGLISH);

        String strDateTime = "2016-03-01T11:33:54.000 Europe/London";
        ZonedDateTime zdt = ZonedDateTime.parse(strDateTime, dtf);
        System.out.println(zdt);

        strDateTime = "2016-06-01T11:33:54.000 Europe/London";
        zdt = ZonedDateTime.parse(strDateTime, dtf);
        System.out.println(zdt);
    }
}

Output:输出:

2016-03-01T11:33:54Z[Europe/London]
2016-06-01T11:33:54+01:00[Europe/London]

Notice how the timezone offset has automatically changed from Z to 01:00 to reflect DST change.请注意时区偏移如何自动从Z更改为01:00以反映 DST 更改。 On the other hand,另一方面,

import java.time.OffsetDateTime;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2016-03-01T11:33:54.000+01:00";
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
        System.out.println(odt);

        strDateTime = "2016-06-01T11:33:54.000+01:00";
        odt = OffsetDateTime.parse(strDateTime);
        System.out.println(odt);
    }
}

Output:输出:

2016-03-01T11:33:54+01:00
2016-06-01T11:33:54+01:00

In this case, you do not talk about a timezone (eg Europe/London);在这种情况下,您不谈论时区(例如欧洲/伦敦); rather, you talk about a fixed timezone offset of +01:00 hours.相反,您谈论的是+01:00小时的固定时区偏移。

Learn more about the modern date-time API from Trail: Date Time .Trail: Date Time 中了解有关现代日期时间 API 的更多信息。


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project . * 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和您的 Android API 工作级别仍然不符合 Java-8,请检查 通过 desugaringHow to use ThreeTenABP in Android Project 可用的 Java 8+ APIs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ISO 8601 DateTimeFormatter 截断此格式的毫秒:'YYYY-MM-DDTHH:mm:ss.sssZ' - ISO 8601 DateTimeFormatter truncates the ms of this format:'YYYY-MM-DDTHH:mm:ss.sssZ' 将sql时间戳转换为yyyy-mm-ddThh:mm:ss.sssZ - Convert sql timestamp to yyyy-mm-ddThh:mm:ss.sssZ 如何将时间戳从yyyy-MM-ddThh:mm:ss:SSSZ格式转换为MM / dd / yyyy hh:mm:ss.SSS格式?从ISO8601到UTC - How can I convert a timestamp from yyyy-MM-ddThh:mm:ss:SSSZ format to MM/dd/yyyy hh:mm:ss.SSS format? From ISO8601 to UTC 如何解析格式 yyyy-MM-ddTHH:mm:ss.SSS+ZZZZ 的 UTC 时间戳 - How to parse UTC timestamp of format yyyy-MM-ddTHH:mm:ss.SSS+ZZZZ 无法理解“ YYYY-MM-DDTHH:MM:SS”日期格式 - Not able to understand “YYYY-MM-DDTHH:MM:SS” date format Java 格式 yyyy-MM-dd'T'HH:mm:ss.SSSz 到 yyyy-mm-dd HH:mm:ss - Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss 以oracle和PostgreSQL查询以yyyy-MM-dd'T'HH:mm:ss.SSSZ格式打印日期 - Query to print date in yyyy-MM-dd'T'HH:mm:ss.SSSZ format in oracle and postgresql Java 8 Date and Time API - 解析yyyy-MM-dd'T'HH:mm:ss.SSSZ - Java 8 Date and Time API - parse yyyy-MM-dd'T'HH:mm:ss.SSSZ YYYY-MM-DDThh:mm:ss在Java中老化的字符串 - YYYY-MM-DDThh:mm:ss string to age in java 日期时间格式 (yyyy-mm-ddThh:mm:ss) ISO8601 格式的正则表达式 - regular expression for datetimeformat (yyyy-mm-ddThh:mm:ss) ISO8601 format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM