使用DateTimeFormatter解析AM/PM时间时,Java 8 DateTimeParseException [英] Java 8 DateTimeParseException when parsing AM/PM time with DateTimeFormatter
问题描述
我正在尝试使用Java 8的java.time.format.DateTimeFormatter
将格式化的字符串解析为java.time.LocalTime
对象.但是,我在解析某些输入字符串时遇到了一些问题.当我的输入字符串具有"AM"时,它将正确解析,但是当我的字符串具有"PM"时,它将引发异常.这是一个简单的示例:
I'm trying to use Java 8's java.time.format.DateTimeFormatter
to parse a formatted string into a java.time.LocalTime
object. However, I'm running into some problems with parsing some input strings. When my input string has "AM" it parses correctly, but when my string has "PM" it throws an exception. Here's a simple example:
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class FormatterExample {
private static final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm a");
public static void main(String[] args) {
parseDateAndPrint("08:06 AM");
parseDateAndPrint("08:06 PM");
}
public static void parseDateAndPrint(String time) {
LocalTime localTime = LocalTime.parse((time), timeFormatter);
System.out.println(localTime.format(timeFormatter));
}
}
输出:
08:06 AM
Exception in thread "main" java.time.format.DateTimeParseException: Text '08:06 PM' could not be parsed: Conflict found: Field AmPmOfDay 0 differs from AmPmOfDay 1 derived from 08:06
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1919)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1854)
at java.time.LocalTime.parse(LocalTime.java:441)
at FormatterExample.parseDateAndPrint(FormatterExample.java:11)
at FormatterExample.main(FormatterExample.java:8)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.time.DateTimeException: Conflict found: Field AmPmOfDay 0 differs from AmPmOfDay 1 derived from 08:06
at java.time.format.Parsed.crossCheck(Parsed.java:582)
at java.time.format.Parsed.crossCheck(Parsed.java:563)
at java.time.format.Parsed.resolve(Parsed.java:247)
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1954)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850)
... 8 more
因此08:06 AM
可以正确解析,但是08:06 PM
会在消息中抛出DateTimeParseException:
So 08:06 AM
is parsed correctly, but 08:06 PM
throws the DateTimeParseException with the message:
Text '08:06 PM' could not be parsed: Conflict found: Field AmPmOfDay 0 differs from AmPmOfDay 1 derived from 08:06
但这是我遇到的问题..我不太确定该错误的确切含义,但这绝对与我输入字符串的AM/PM部分有关.我也尝试搜索类似的错误,但找不到任何东西.我觉得我在定义格式化程序模式时可能犯了一个简单的错误,但我被卡住了.任何帮助将不胜感激!
But this is where I got stuck.. I'm not really sure what that error means exactly, but it is definitely related to the AM/PM part of my input string. I also tried searching for similar errors but I wasn't able to find anything. I have a feeling I'm probably making a simple mistake here with defining my formatter pattern but I'm stuck. Any help would be appreciated!
推荐答案
您使用的是错误的模式,H
是一天中的小时(0-23);您需要h
.
You are using the wrong pattern, H
is hour-of-day (0-23); you need h
.
错误告诉您H
是 24小时小时,因此8
显然是上午.因此,当您告诉解析器在24小时时钟上使用8
并告诉解析器它是PM时,它会炸毁.
The error is telling you that H
is a 24 hour hour, and 8
is therefore obviously AM. Hence when you tell the parser to use 8
on the 24 hour clock and also tell the parse that it's PM it blows up.
简而言之,请阅读文档.
这篇关于使用DateTimeFormatter解析AM/PM时间时,Java 8 DateTimeParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!