使用DateTimeFormatter

时间:2016-06-17 11:05:20

标签: java

我正在尝试通过以下代码将EST日期字符串转换为zonedDateTime对象 -

@Test
public void dateTest() {
    String date = "Fri Feb 26 00:00:00 EST 2016";//"Fri Feb 26 00:00:00 EST 2016"
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EEE MMM dd hh:mm:ss zzz YYYY");
    ZonedDateTime zoneDateTime = ZonedDateTime.parse(date,dateTimeFormatter);
    System.out.println(zoneDateTime.toString());
}

但要低于例外 -

  

java.time.format.DateTimeParseException:无法解析文本'Fri Feb 26 00:00:00 EST 2016':无法从TemporalAccessor获取ZonedDateTime:{DayOfMonth = 26,MicroOfSecond = 0,MinuteOfHour = 0,MilliOfSecond = 0,MonthOfYear = 2,HourOfAmPm = 0,DayOfWeek = 5,WeekBasedYear [WeekFields [SUNDAY,1]] = 2016,NanoOfSecond = 0,SecondOfMinute = 0},ISO,America / New_York,类型为java.time.format.Parsed       at java.time.format.DateTimeFormatter.createError(Unknown Source)       at java.time.format.DateTimeFormatter.parse(Unknown Source)       在java.time.ZonedDateTime.parse(未知来源)       at com.citi.uno.alerts.controller.XMLTest.dateTest(XMLTest.java:23)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)       at java.lang.reflect.Method.invoke(Unknown Source)       在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)       在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)       在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)       在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)       在org.junit.runners.ParentRunner.run(ParentRunner.java:363)       在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)       在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:670)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)   引起:java.time.DateTimeException:无法从TemporalAccessor获取ZonedDateTime:{DayOfMonth = 26,MicroOfSecond = 0,MinuteOfHour = 0,MilliOfSecond = 0,MonthOfYear = 2,HourOfAmPm = 0,DayOfWeek = 5,WeekBasedYear [WeekFields [SUNDAY] ,1]] = 2016,NanoOfSecond = 0,SecondOfMinute = 0},ISO,America / New_York,类型为java.time.format.Parsed       at java.time.ZonedDateTime.from(Unknown Source)       在java.time.format.Parsed.query(未知来源)       ......还有26个   引起:java.time.DateTimeException:无法从TemporalAccessor获取LocalDate:{DayOfMonth = 26,MicroOfSecond = 0,MinuteOfHour = 0,MilliOfSecond = 0,MonthOfYear = 2,HourOfAmPm = 0,DayOfWeek = 5,WeekBasedYear [WeekFields [SUNDAY] ,1]] = 2016,NanoOfSecond = 0,SecondOfMinute = 0},ISO,America / New_York,类型为java.time.format.Parsed       at java.time.LocalDate.from(Unknown Source)       ......还有28个

请帮助!!

0 个答案:

没有答案