yyyy-MM-dd -> LocalDateTime
直接把yyyy-MM-dd格式的日期字符串解析成LocalDateTime会抛出异常
try { LocalDateTime localDateTime = LocalDateTime.parse("2019-05-27", DateTimeFormatter.ofPattern("yyyy-MM-dd")); System.out.println(localDateTime); } catch (Exception ex) { ex.printStackTrace(); }
抛出如下异常
java.time.format.DateTimeParseException: Text '2019-05-27' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2019-05-27 of type java.time.format.Parsed at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1919) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1854) at java.time.LocalDateTime.parse(LocalDateTime.java:492) at com.ahut.common.utils.DateTest.testLocalDate(DateTest.java:28) 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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 2019-05-27 of type java.time.format.Parsed at java.time.LocalDateTime.from(LocalDateTime.java:461) at java.time.LocalDateTime$$Lambda$7/762152757.queryFrom(Unknown Source) at java.time.format.Parsed.query(Parsed.java:226) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850) ... 24 more Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {},ISO resolved to 2019-05-27 of type java.time.format.Parsed at java.time.LocalTime.from(LocalTime.java:409) at java.time.LocalDateTime.from(LocalDateTime.java:457) ... 27 more
解决办法yyyy-MM-dd -> LocalDate -> LocalDateTime
try { LocalDate localDate = LocalDate.parse("2019-05-27", DateTimeFormatter.ofPattern("yyyy-MM-dd")); LocalDateTime localDateTime = localDate.atStartOfDay(); System.out.println(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); } catch (Exception ex) { ex.printStackTrace(); }
输出
2019-05-27 00:00:00
LocalDate atStartOfDay方法源码
/** * Combines this date with the time of midnight to create a {@code LocalDateTime} * at the start of this date. * <p> * This returns a {@code LocalDateTime} formed from this date at the time of * midnight, 00:00, at the start of this date. * * @return the local date-time of midnight at the start of this date, not null */ public LocalDateTime atStartOfDay() { return LocalDateTime.of(this, LocalTime.MIDNIGHT); }
LocalTime常量
/** * The minimum supported {@code LocalTime}, '00:00'. * This is the time of midnight at the start of the day. */ public static final LocalTime MIN; /** * The maximum supported {@code LocalTime}, '23:59:59.999999999'. * This is the time just before midnight at the end of the day. */ public static final LocalTime MAX; /** * The time of midnight at the start of the day, '00:00'. */ public static final LocalTime MIDNIGHT; /** * The time of noon in the middle of the day, '12:00'. */ public static final LocalTime NOON; /** * Constants for the local time of each hour. */ private static final LocalTime[] HOURS = new LocalTime[24];
扩展
获取当前日期最早时间和最晚时间
System.out.println(LocalDateTime.of(LocalDate.now(), LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); System.out.println(LocalDateTime.of(LocalDate.now(), LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
2019-05-27 00:00:00 2019-05-27 23:59:59
封装解析方法
/** * desc : * create_user : cheng * create_date : 2019/5/27 19:28 */ private LocalDateTime parseToLocalDateTime(String str, String pattern) { if (StringUtils.isAnyBlank(str, pattern)) { return null; } LocalDateTime localDateTime; try { localDateTime = LocalDateTime.parse(str, DateTimeFormatter.ofPattern(pattern)); } catch (Exception ex) { ex.printStackTrace(); LocalDate localDate = parseLocalDate(str, pattern); localDateTime = Objects.isNull(localDate) ? null : localDate.atStartOfDay(); } return localDateTime; } /** * desc : * create_user : cheng * create_date : 2019/5/27 19:30 */ private LocalDate parseLocalDate(String str, String pattern) { if (StringUtils.isAnyBlank(str, pattern)) { return null; } LocalDate localDate = null; try { localDate = LocalDate.parse(str, DateTimeFormatter.ofPattern(pattern)); } catch (Exception ex) { ex.printStackTrace(); } return localDate; }
调用
System.out.println("解析: " + parseToLocalDateTime("2019-05-27", "yyyy-MM-dd") .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
结果
解析: 2019-05-27 00:00:00