使用 DateTimeFormatter ofPattern 解析日期
Parsing a date using DateTimeFormatter ofPattern
我正在尝试使用 java.time.format.DateTimeFormatter
解析包含日期和时间的字符串(我的最终目标是将此字符串中的日期转换为 java.time.LocalDate
)。
我在尝试解析日期时不断收到 DateTimeParseExceptions。有人可以帮忙吗?
日期格式为“2015-07-14T11:42:12.000+01:00”。
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss.SSSZ");
LocalDateTime temp = LocalDateTime.ofInstant(Instant.from(f.parse(this.dateCreated)),
ZoneId.systemDefault());
LocalDate localDate = temp.toLocalDate();
我尝试了 ofPattern 的不同变体,例如尝试通过用单引号将 T 括起来(如上所述)来转义 T,并对 .我试过同时逃避两者。
冒号也需要转义吗?
感谢对此的任何帮助。
应该是
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
//or
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");
而不是
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss.SSSZ");
来自 JAVADoc:
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'.
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ" 和 "yyyy-MM-dd'T'HH:mm:ss.SSSVV" 都可以。请注意,5 Z 有效但不少于
我正在尝试使用 java.time.format.DateTimeFormatter
解析包含日期和时间的字符串(我的最终目标是将此字符串中的日期转换为 java.time.LocalDate
)。
我在尝试解析日期时不断收到 DateTimeParseExceptions。有人可以帮忙吗?
日期格式为“2015-07-14T11:42:12.000+01:00”。
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss.SSSZ");
LocalDateTime temp = LocalDateTime.ofInstant(Instant.from(f.parse(this.dateCreated)),
ZoneId.systemDefault());
LocalDate localDate = temp.toLocalDate();
我尝试了 ofPattern 的不同变体,例如尝试通过用单引号将 T 括起来(如上所述)来转义 T,并对 .我试过同时逃避两者。
冒号也需要转义吗?
感谢对此的任何帮助。
应该是
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
//or
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");
而不是
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss.SSSZ");
来自 JAVADoc:
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'.
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ" 和 "yyyy-MM-dd'T'HH:mm:ss.SSSVV" 都可以。请注意,5 Z 有效但不少于