0

I am trying to convert EST date string to zonedDateTime object via below code -

@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());
}

but getting below exception -

java.time.format.DateTimeParseException: Text 'Fri Feb 26 00:00:00 EST 2016' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {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 of type java.time.format.Parsed at java.time.format.DateTimeFormatter.createError(Unknown Source) at java.time.format.DateTimeFormatter.parse(Unknown Source) at java.time.ZonedDateTime.parse(Unknown Source) 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) 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:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:670) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) Caused by: java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {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 of type java.time.format.Parsed at java.time.ZonedDateTime.from(Unknown Source) at java.time.format.Parsed.query(Unknown Source) ... 26 more Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {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 of type java.time.format.Parsed at java.time.LocalDate.from(Unknown Source) ... 28 more

Please help!!

  • @Tunaki: thanks for sharing the actual thread. I have gone through the thread, but not able to catch, what is wrong with my code. Please help!! – malviyarahuljayendra Jun 17 '16 at 11:26
  • In your pattern `EEE MMM dd hh:mm:ss zzz YYYY`, there are multiple issues: first, `"hh"` is AM/PM hour, not 24-hour. And `YYYY` is the week-year not the year (`yyyy`). Depending on your system locale, you will also need to specify `Locale.ENGLISH` since you're parsing Month written in English. – Tunaki Jun 17 '16 at 11:46
  • Thanks...i changed the YYYY to yyyy and HH to hh, but then too got the same exception. – malviyarahuljayendra Jun 17 '16 at 11:54
  • Check your locale, it needs to be English: `DateTimeFormatter.ofPattern(..., Locale.ENGLISH)`. – Tunaki Jun 17 '16 at 11:55
  • tried that too, but no luck :( – malviyarahuljayendra Jun 17 '16 at 12:01
  • This code works for me-------------------------------------------------------------------String valueDate = trade.get("valueDate").toString(); DateTimeFormatter parsingDateTimeFormatter = DateTimeFormatter.ofPattern("EEE MMM dd H:mm:ss z yyyy"); DateTimeFormatter desiredDateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss"); ZonedDateTime zoneDateTime = ZonedDateTime.parse(valueDate,parsingDateTimeFormatter); return desiredDateTimeFormatter.format(zoneDateTime); – malviyarahuljayendra Jun 17 '16 at 12:38

0 Answers0