jdk1.8的新时间日期类

L1414430904 2018-07-18 08:05:48
// 5. 格式化时间/日期
@Test
public void test6() {
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
LocalDateTime ldt = LocalDateTime.now();

String date1 = ldt.format(dtf);
System.out.println(date1);

DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm:ss");
String date2 = dtf2.format(ldt);

LocalDateTime ldt2 = ldt.parse(date2, dtf2); //这句报错,
System.out.println(ldt2);

}
java.time.format.DateTimeParseException: Text '2018年07月18日 08:04:26' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {SecondOfMinute=26, NanoOfSecond=0, MicroOfSecond=0, HourOfAmPm=8, MinuteOfHour=4, MilliOfSecond=0},ISO resolved to 2018-07-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 time.TestLocalDateTime.test6(TestLocalDateTime.java:49)
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:498)
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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {SecondOfMinute=26, NanoOfSecond=0, MicroOfSecond=0, HourOfAmPm=8, MinuteOfHour=4, MilliOfSecond=0},ISO resolved to 2018-07-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)
... 25 more
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: {SecondOfMinute=26, NanoOfSecond=0, MicroOfSecond=0, HourOfAmPm=8, MinuteOfHour=4, MilliOfSecond=0},ISO resolved to 2018-07-18 of type java.time.format.Parsed
at java.time.LocalTime.from(LocalTime.java:409)
at java.time.LocalDateTime.from(LocalDateTime.java:457)
... 27 more

...全文
285 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
L1414430904 2018-07-18
  • 打赏
  • 举报
回复
感谢大佬。
幽饮烛 2018-07-18
  • 打赏
  • 举报
回复
yyyy年MM月dd日 hh:mm:ss
改成 yyyy年MM月dd日 HH:mm:ss
hh 是12小时制,HH是24小时制。
如果使用12小时制,需要指定上午或者下午,不然不能确定时间。

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧