无法从 TemporalAccessor 获取 LocalTime
Unable to obtain LocalTime from TemporalAccessor
我第一次接触 Java 8 次,所以这个问题可能有一个明显的答案,但在阅读了其他类似的 SO 问题后,我无法发现导致我的问题的任何相似之处。
这是我的 class:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class LocalDateTimeIssue {
public static void main(String[] args) {
String dateTimeString = "18-04-2019 12:14:46";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm:ss", Locale.US);
LocalDateTime ldt = LocalDateTime.parse(dateTimeString , dtf);
System.out.println(ldt.getSecond());
}
}
这将引发以下异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text '18-04-2019 12:14:46' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=14, HourOfAmPm=0, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=46},ISO resolved to 2019-04-18 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at LocalDateTimeIssue.main(LocalDateTimeIssue.java:10)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=14, HourOfAmPm=0, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=46},ISO resolved to 2019-04-18 of type java.time.format.Parsed
at java.time.LocalDateTime.from(LocalDateTime.java:461)
at java.time.format.Parsed.query(Parsed.java:226)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
... 2 more
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=14, HourOfAmPm=0, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=46},ISO resolved to 2019-04-18 of type java.time.format.Parsed
at java.time.LocalTime.from(LocalTime.java:409)
at java.time.LocalDateTime.from(LocalDateTime.java:457)
... 4 more
必须是dd-MM-yyyy HH:mm:ss
h clock-hour-of-am-pm (1-12) number 12
H hour-of-day (0-23) number 0
因为在您的示例中,12 可以是上午或下午。
由于缺少信息,格式化程序无法决定是上午还是下午:
a am-pm-of-day text PM
两者都可以,所以出现错误Unable to obtain LocalTime
。
我第一次接触 Java 8 次,所以这个问题可能有一个明显的答案,但在阅读了其他类似的 SO 问题后,我无法发现导致我的问题的任何相似之处。
这是我的 class:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class LocalDateTimeIssue {
public static void main(String[] args) {
String dateTimeString = "18-04-2019 12:14:46";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm:ss", Locale.US);
LocalDateTime ldt = LocalDateTime.parse(dateTimeString , dtf);
System.out.println(ldt.getSecond());
}
}
这将引发以下异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text '18-04-2019 12:14:46' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=14, HourOfAmPm=0, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=46},ISO resolved to 2019-04-18 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at LocalDateTimeIssue.main(LocalDateTimeIssue.java:10)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=14, HourOfAmPm=0, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=46},ISO resolved to 2019-04-18 of type java.time.format.Parsed
at java.time.LocalDateTime.from(LocalDateTime.java:461)
at java.time.format.Parsed.query(Parsed.java:226)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
... 2 more
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=14, HourOfAmPm=0, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=46},ISO resolved to 2019-04-18 of type java.time.format.Parsed
at java.time.LocalTime.from(LocalTime.java:409)
at java.time.LocalDateTime.from(LocalDateTime.java:457)
... 4 more
必须是dd-MM-yyyy HH:mm:ss
h clock-hour-of-am-pm (1-12) number 12
H hour-of-day (0-23) number 0
因为在您的示例中,12 可以是上午或下午。
由于缺少信息,格式化程序无法决定是上午还是下午:
a am-pm-of-day text PM
两者都可以,所以出现错误Unable to obtain LocalTime
。