我想從以下模式解析LocalDateTime:LocalDateTime.parse()的模式只有數字
yyyyMMddHHmmss000000
這意味着一貫的「YYYY ...... SS」和後面是6個尾隨零。
因此,格式化工作正常:
String p = "yyyyMMddHHmmss'000000'";
LocalDateTime.now().format(DateTimeFormatter.ofPattern(p));
和解析,但:
String p, v;
p = "yyyyMMddHHmmss"; // without '000000'
v = "20160131235930";
LocalDateTime.parse(v, DateTimeFormatter.ofPattern(p)); // it works
p = "yyyy-MMddHHmmss'000000'"; // with '-' in between
v = "2016-0131235930000000";
LocalDateTime.parse(v, DateTimeFormatter.ofPattern(p)); // it works
p = "yyyyMMddHHmmss'000000'"; // with '000000' but without '-'
v = "20160131235930000000";
LocalDateTime.parse(v, DateTimeFormatter.ofPattern(p)); // it throws Exception
例外:
java.time.format.DateTimeParseException: Text '20160131235930000000' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
...
我不能改變輸入值的格式。我如何正確解析它?我的模式錯了嗎?
Java版本:1.8.0_60上OSX
爲了完整起見,bug:https://bugs.openjdk.java.net/browse/JDK-8031085 – Marcel