[英]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:ssZ
对2019-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:ssZZZZZ
或uuuu-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.