java 使用 DateTimeFormatter ofPattern 解析日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35156809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing a date using DateTimeFormatter ofPattern
提问by Chris
I'm trying to parse a string containing a date and time using the java.time.format.DateTimeFormatter
(my ultimate goal is to get the date from this string into a java.time.LocalDate
).
我正在尝试使用 解析包含日期和时间的字符串java.time.format.DateTimeFormatter
(我的最终目标是将此字符串中的日期获取到 a 中java.time.LocalDate
)。
I keep getting DateTimeParseExceptions when trying to parse the date. Can someone help please?
尝试解析日期时,我不断收到 DateTimeParseExceptions。有人可以帮忙吗?
The date is in the format "2015-07-14T11:42:12.000+01:00".
日期的格式为“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();
I've tried different variations in the ofPattern, such as trying to escape the T by surrounding it with single quotes (as done above), and doing the same with the . and I've tried escaping both at the same time.
我在 ofPattern 中尝试了不同的变体,例如尝试通过用单引号将 T 括起来(如上所述)来转义 T,并对 . 我试过同时逃避两者。
Do the colons need escaping too?
冒号也需要转义吗?
Appreciate any help with this.
感谢您对此的任何帮助。
回答by user2004685
It should be 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");
instead of DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss.SSSZ");
.
它应该是DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
或DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz");
而不是DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss.SSSZ");
。
From JAVADoc:
来自 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'.
偏移量 X 和 x:这会根据模式字母的数量格式化偏移量。一个字母只输出小时,例如“+01”,除非分钟不为零,在这种情况下也会输出分钟,例如“+0130”。两个字母输出小时和分钟,没有冒号,例如“+0130”。三个字母输出小时和分钟,带一个冒号,例如“+01:30”。四个字母输出小时和分钟以及可选的秒,没有冒号,例如“+013015”。五个字母输出小时和分钟以及可选的秒,带有冒号,例如“+01:30:15”。六个或更多字母会引发 IllegalArgumentException。当要输出的偏移量为零时,模式字母“X”(大写)将输出“Z”,而模式字母“x”(小写)将输出“
回答by Michael Gantman
Both "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ" and "yyyy-MM-dd'T'HH:mm:ss.SSSVV" would work. Note that 5 Zs work but no less
“yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ”和“yyyy-MM-dd'T'HH:mm:ss.SSSVV”都可以。请注意,5 Z 有效但不少于