简体   繁体   English

ISO_OFFSET_DATE_TIME 的 Java 日期时间格式模式

[英]Java date time format pattern for ISO_OFFSET_DATE_TIME

I'm after the date time format pattern for ISO_OFFSET_DATE_TIME我追求的是ISO_OFFSET_DATE_TIME的日期时间格式模式

2019-09-30T10:05:16+10:00

yyyy-MM-dd'T'HH:mm:ssZ is valid for 2019-09-30T10:05:16+1000 but I need the colon in the zone offset yyyy-MM-dd'T'HH:mm:ssZ2019-09-30T10:05:16+1000有效,但我需要在区域偏移量中使用冒号

If this is not possible, I'll need a regular expression.如果这是不可能的,我将需要一个正则表达式。

You need uuuu-MM-dd'T'HH:mm:ssXXX here.你需要uuuu-MM-dd'T'HH:mm:ssXXX在这里。

String str = "2019-09-30T10:05:16+10:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXXX");
OffsetDateTime datetime = OffsetDateTime.parse(str, formatter);
System.out.println(datetime);

It depends.这取决于。 DateTimeFormatter.ISO_OFFSET_DATE_TIME prints and parses strings with and without seconds and with and without fraction of second, the latter up to 9 decimals. DateTimeFormatter.ISO_OFFSET_DATE_TIME打印和解析带和不带秒以及带和不带秒小数的字符串,后者最多 9 位小数。

If you only need the pattern for the variant of the format given in your question, with seconds and without fraction of second, the answer by MC Emperor is exactly what you need.如果您只需要问题中给出的格式变体的模式,有秒和没有秒,MC 皇帝的答案正是您所需要的。

If you need the full flexibility of ISO_OFFSET_DATE_TIME , then there is no pattern that can give you that.如果您需要ISO_OFFSET_DATE_TIME的完全灵活性,那么没有任何模式ISO_OFFSET_DATE_TIME您的需求。 Then you will need to go the regular expression way.然后你需要去正则表达式的方式。 Which in turn can hardly give you as strict a validation as the formatter.这反过来又很难给你像格式化程序那样严格的验证。 And the regular expression may still grow complicated and very hard to read.并且正则表达式可能仍然变得复杂且难以阅读。

Link: My answer to a similar question with a few more details.链接:我对类似问题的回答以及更多细节。

You can use yyyy-MM-dd'T'HH:mm:ssZZZZZ or uuuu-MM-dd'T'HH:mm:ssXXX .您可以使用yyyy-MM-dd'T'HH:mm:ssZZZZZuuuu-MM-dd'T'HH:mm:ssXXX

Demo:演示:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2019-09-30T10:05:16+10:00";
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
        // Default format i.e. OffsetDateTime#toString
        System.out.println(odt);

        // Custom format
        System.out.println(odt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZZZZZ")));
        System.out.println(odt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX")));
    }
}

Output:输出:

2019-09-30T10:05:16+10:00
2019-09-30T10:05:16+10:00
2019-09-30T10:05:16+10:00

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

相关问题 将日期从 GMT 时区转换为本地时区 -- 使用 ISO_OFFSET_DATE_TIME - Convert date from GMT timezone to local time zone -- using ISO_OFFSET_DATE_TIME 如何格式化日期/时间/区域以在 Java 中使用与 UTC 的偏移量模式? - How to format date/time/zone to pattern with offset from UTC in Java? ISO_DATE_TIME.format()到具有可选偏移量的LocalDateTime - ISO_DATE_TIME.format() to LocalDateTime with optional offset Java时间:如何解释ISO_DATE_TIME格式的时间 - Java time: How to interpret time of ISO_DATE_TIME format DateTimeFormatter.ISO_OFFSET_DATE_TIME 的等效格式字符串是什么? - What is the equivalent format string of DateTimeFormatter.ISO_OFFSET_DATE_TIME? java ISO_LOCAL_DATE_TIME 的 Elasticsearch 等效日期模式 - Elasticsearch equivalent date pattern for java ISO_LOCAL_DATE_TIME Java 8 DateTimeFormatter使用偏移拒绝正确的ISO 8601日期/时间 - Java 8 DateTimeFormatter rejects correct ISO 8601 date/time with offset 如何在java中以ISO日期格式打印当前时间和日期? - How to print the current time and date in ISO date format in java? Java 日期时间作为 ISO_DATE_TIME - Java Date Time as ISO_DATE_TIME 如何在 Java 中将 UTC 日期时间转换为 ISO 8601 格式? - how to convert UTC date-time into ISO 8601 format in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM