如何使用 DateTimeFormatter 解析带冒号的偏移量?
How to parse offset with colon using DateTimeFormatter?
我有以下字符串:String timeStamp = "2020-01-31 12:13:14 +03:00"
。
我尝试使用 Java 8 DateTimeFormatter.
来解析它
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( format );
tmpTimestamp = ZonedDateTime.parse( timeStamp, formatter );
其中 format
是以下之一:
"yyyy-MM-dd' 'HH:mm:ss' 'Z",
"yyyy-MM-dd' 'HH:mm:ss' 'X",
"yyyy-MM-dd' 'HH:mm:ss' 'x",
"yyyy-MM-dd HH:mm:ss Z",
"yyyy-MM-dd HH:mm:ss X",
"yyyy-MM-dd HH:mm:ss x",
None 正在工作。我总是得到 DateTimeParseException
指向偏移子字符串“+03:00”
中的“+”或“:”字符
根据 Java文档:Class DateTimeFormatter“+03:00”应受以下任何一项支持:Z
、X
和 x
。
所以问题是如何构造格式化字符串来解析它?
来自javadoc
Offset Z: This formats the offset based on the number of pattern
letters. One, two or three letters outputs the hour and minute,
without a colon, such as '+0130'. The output will be '+0000' when the
offset is zero. Four letters outputs the full form of localized
offset, equivalent to four letters of Offset-O. The output will be the
corresponding localized offset text if the offset is zero. Five
letters outputs the hour, minute, with optional second if non-zero,
with colon. It outputs 'Z' if the offset is zero. Six or more letters
throws IllegalArgumentException.
你需要 5 个 Z
String format = "yyyy-MM-dd HH:mm:ss ZZZZZ";
你应该使用时间 X
(yyyy-MM-dd HH:mm:ss XXX
):
String timeStamp = "2020-01-31 12:13:14 +03:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss XXX");
ZonedDateTime tmpTimestamp = ZonedDateTime.parse(timeStamp, formatter);
来自docs:
Offset X and x: This formats the offset based on the number of pattern letters.
One letter outputs just the hour, such as '+01', unless the minute is non-zero in which case the minute is also output, such as '+0130'.
Two letters outputs the hour and minute, without a colon, such as '+0130'.
Three letters outputs the hour and minute, with a colon, such as '+01:30'.
Four letters outputs the hour and minute and optional second, without a colon, such as '+013015'.
Five letters outputs the hour and minute and optional second, with a colon, such as '+01:30:15'.
Six or more letters throws IllegalArgumentException.
Pattern letter 'X' (upper case) will output 'Z' when the offset to be output would be zero, whereas pattern letter 'x' (lower case) will output '+00', '+0000', or '+00:00'.
您也可以使用五个字母 (XXXXX
),也可以使用 ZZZ
或 ZZZZZ
代替 XXX
或 XXXXX
。
您不需要 fiddle 使用格式模式字符串。
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.appendLiteral(' ')
.appendOffsetId()
.toFormatter();
String timeStampString = "2020-01-31 12:13:14 +03:00";
OffsetDateTime dateTime = OffsetDateTime.parse(timeStampString, formatter);
System.out.println(dateTime);
它更冗长,但更难出错。我只是用内置部件组装格式化程序。输出为:
2020-01-31T12:13:14+03:00
我也在解析成 OffsetDateTime
而不是 ZonedDateTime
。由于该字符串包含偏移量 (+03:00) 并且没有时区(例如 America/Boa_Vista),即使它可能与 ZonedDateTime
一起使用,OffsetDateTime
也是正确的 class 使用。
我有以下字符串:String timeStamp = "2020-01-31 12:13:14 +03:00"
。
我尝试使用 Java 8 DateTimeFormatter.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( format );
tmpTimestamp = ZonedDateTime.parse( timeStamp, formatter );
其中 format
是以下之一:
"yyyy-MM-dd' 'HH:mm:ss' 'Z",
"yyyy-MM-dd' 'HH:mm:ss' 'X",
"yyyy-MM-dd' 'HH:mm:ss' 'x",
"yyyy-MM-dd HH:mm:ss Z",
"yyyy-MM-dd HH:mm:ss X",
"yyyy-MM-dd HH:mm:ss x",
None 正在工作。我总是得到 DateTimeParseException
指向偏移子字符串“+03:00”
根据 Java文档:Class DateTimeFormatter“+03:00”应受以下任何一项支持:Z
、X
和 x
。
所以问题是如何构造格式化字符串来解析它?
来自javadoc
Offset Z: This formats the offset based on the number of pattern letters. One, two or three letters outputs the hour and minute, without a colon, such as '+0130'. The output will be '+0000' when the offset is zero. Four letters outputs the full form of localized offset, equivalent to four letters of Offset-O. The output will be the corresponding localized offset text if the offset is zero. Five letters outputs the hour, minute, with optional second if non-zero, with colon. It outputs 'Z' if the offset is zero. Six or more letters throws IllegalArgumentException.
你需要 5 个 Z
String format = "yyyy-MM-dd HH:mm:ss ZZZZZ";
你应该使用时间 X
(yyyy-MM-dd HH:mm:ss XXX
):
String timeStamp = "2020-01-31 12:13:14 +03:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss XXX");
ZonedDateTime tmpTimestamp = ZonedDateTime.parse(timeStamp, formatter);
来自docs:
Offset X and x: This formats the offset based on the number of pattern letters.
One letter outputs just the hour, such as '+01', unless the minute is non-zero in which case the minute is also output, such as '+0130'.
Two letters outputs the hour and minute, without a colon, such as '+0130'.
Three letters outputs the hour and minute, with a colon, such as '+01:30'.
Four letters outputs the hour and minute and optional second, without a colon, such as '+013015'.
Five letters outputs the hour and minute and optional second, with a colon, such as '+01:30:15'.
Six or more letters throws IllegalArgumentException.
Pattern letter 'X' (upper case) will output 'Z' when the offset to be output would be zero, whereas pattern letter 'x' (lower case) will output '+00', '+0000', or '+00:00'.
您也可以使用五个字母 (XXXXX
),也可以使用 ZZZ
或 ZZZZZ
代替 XXX
或 XXXXX
。
您不需要 fiddle 使用格式模式字符串。
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.appendLiteral(' ')
.appendOffsetId()
.toFormatter();
String timeStampString = "2020-01-31 12:13:14 +03:00";
OffsetDateTime dateTime = OffsetDateTime.parse(timeStampString, formatter);
System.out.println(dateTime);
它更冗长,但更难出错。我只是用内置部件组装格式化程序。输出为:
2020-01-31T12:13:14+03:00
我也在解析成 OffsetDateTime
而不是 ZonedDateTime
。由于该字符串包含偏移量 (+03:00) 并且没有时区(例如 America/Boa_Vista),即使它可能与 ZonedDateTime
一起使用,OffsetDateTime
也是正确的 class 使用。