我正在尝试使用LocalDateTime.parse
方法解析日期,但是出现错误。如果我使用SimpleDateFormat
简单日期格式对象,则日期字符串将被解析。
有人遇到这个问题!从DateFormat
和LocalDateTime
解析之间有什么区别
package com.example.demo;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class App {
public static final String DATE_TIME_PATTERN = "dd-MM-yyyy hh:mm:ss.SSS";
public static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat(DATE_TIME_PATTERN);
public static final String SEPERATOR = ",";
public static void main(String[] args) {
try {
Date date = DATE_TIME_FORMAT.parse("12-03-2019 10:28:50.013");
System.out.println("date : {} " + date);
LocalDateTime startTimestamp = LocalDateTime.parse("12-03-2019 10:28:50.013", DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)).plusNanos(1000000);
System.out.println("startTimestamp : {} " + startTimestamp);
} catch(Exception e) {
e.printStackTrace();
}
}
}
输出
date : {} Tue Mar 12 10:28:50 SGT 2019
java.time.format.DateTimeParseException: Text '12-03-2019 10:28:50.013' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {NanoOfSecond=13000000, HourOfAmPm=10, MicroOfSecond=13000, SecondOfMinute=50, MilliOfSecond=13, MinuteOfHour=28},ISO resolved to 2019-03-12 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 com.example.demo.App.main(App.java:21)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {NanoOfSecond=13000000, HourOfAmPm=10, MicroOfSecond=13000, SecondOfMinute=50, MilliOfSecond=13, MinuteOfHour=28},ISO resolved to 2019-03-12 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: {NanoOfSecond=13000000, HourOfAmPm=10, MicroOfSecond=13000, SecondOfMinute=50, MilliOfSecond=13, MinuteOfHour=28},ISO resolved to 2019-03-12 of type java.time.format.Parsed
at java.time.LocalTime.from(LocalTime.java:409)
at java.time.LocalDateTime.from(LocalDateTime.java:457)
... 4 more
答案 0 :(得分:4)
您正在使用 am-clock-hour-am-pm(1-12)(小时),该时间在您的模式中是 h
,而不是< strong>一天中的小时(0-23),即 H
,因此它需要AM / PM的其他信息。
因此,理想情况下,必须在要解析的日期字符串中加上 a
的AM / PM,以确保每天的上午/下午还需要将其添加到DATE_TIME_PATTERN
字符串中。
public static final String DATE_TIME_PATTERN = "dd-MM-yyyy hh:mm:ss.SSS a";
public static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat(DATE_TIME_PATTERN);
public static final String SEPERATOR = ",";
public static void main(String[] args) {
try {
Date date = DATE_TIME_FORMAT.parse("12-03-2019 10:28:50.013 AM");
System.out.println("date : {} " + date);
LocalDateTime startTimestamp = LocalDateTime.parse("12-03-2019 10:28:50.013 AM", DateTimeFormatter.ofPattern(DATE_TIME_PATTERN)).plusNanos(1000000);
System.out.println("startTimestamp : {} " + startTimestamp);
} catch(Exception e) {
e.printStackTrace();
}
}
输出:
date : {} Tue Mar 12 10:28:50 IST 2019
startTimestamp : {} 2019-03-12T10:28:50.014
我们可以看到,在解析无效的日期字符串时,SimpleDateFormat
没有正确的格式,而LocalDateTime
却更严格。在您的情况下,由于缺少必需的AM / PM信息,从LocalTime
返回的TemporalAccessor
为null
,因此您得到Unable to obtain LocalTime from TemporalAccessor
。
不知道为什么SimpleDateFormat
起作用的原因,如果您经过的小时数大于12
,而模式和AM中未提及a
,则有一种名为setLenient(boolean lenient)
的方法/ PM在日期字符串中将引发java.text.ParseException: Unparseable date:
。
但是由于您的情况是您将小时传递为10
,因此小于12
,因此默认情况下将其解释为 AM 。
这是进行此检查的SimpleDateFormat类中的代码:
case PATTERN_HOUR1: // 'h' 1-based. eg, 11PM + 1 hour =>> 12 AM
if (!isLenient()) {
// Validate the hour value in non-lenient
if (value < 1 || value > 12) {
break parsing;
}
}
// [We computed 'value' above.]
if (value == calendar.getLeastMaximum(Calendar.HOUR) + 1) {
value = 0;
}
calb.set(Calendar.HOUR, value);
return pos.index;