Emmanuel Guiton
Emmanuel Guiton

Reputation: 1445

Failure while parsing ZonedDateTime#toString() with Joda time

With Elasticsearch 1.x (1.7.5 depends on Joda time 1.6), I could use the custom date time format "yyyy-MM-dd'T'HH:mm:ss.SSSZZ[ZZZ]" to parse serialized strings obtained with ZonedDateTime#toString() (such as '2016-11-29T18:47:21.766+01:00[Europe/Paris]') into timestamps.

With Elasticsearch 2.4.1 (which depends on Joda time 2.9.4), I cannot anymore. It fails with a "java.lang.IllegalArgumentException: Invalid format: "2016-11-29T18:47:21.766+01:00[Europe/Paris]" is malformed at "Europe/Paris]"" exception.

EDIT : this is about the Elasticsearch Java API.

EDIT2 : I simply would like to translate Java 8 DateTimeFormatter#ISO_ZONED_DATE_TIME into a format string that could build a working Jodatime FormatDateTimeFormatter using Joda#forPattern(String, Locale)

I reproduced the issue in a TestNG test case :

final ZonedDateTime now = ZonedDateTime.now();
final String strNow = now.toString();
final FormatDateTimeFormatter formatter0 = Joda.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ[ZZZ]", Locale.ROOT);
formatter0.parser().parseMillis(strNow);

That code snippet fails with the following exception :

FAILED: parseMillisTest java.lang.IllegalArgumentException: Invalid format: "2016-11-29T18:47:21.766+01:00[Europe/Paris]" is malformed at "Europe/Paris]" at org.joda.time.format.DateTimeParserBucket.doParseMillis(DateTimeParserBucket.java:187) at org.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:826) at org.joda.time.format.DateTimeFormatterTest.parseMillisTest(DateTimeFormatterTest.java:27) 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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100) at org.testng.internal.Invoker.invokeMethod(Invoker.java:646) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1137) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) at org.testng.TestRunner.privateRun(TestRunner.java:753) at org.testng.TestRunner.run(TestRunner.java:607) at org.testng.SuiteRunner.runTest(SuiteRunner.java:368) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:363) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:321) at org.testng.SuiteRunner.run(SuiteRunner.java:270) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1284) at org.testng.TestNG.runSuitesLocally(TestNG.java:1209) at org.testng.TestNG.runSuites(TestNG.java:1124) at org.testng.TestNG.run(TestNG.java:1096) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)

  1. Am I wrong to believe that Joda Time 2.9.4 is faulty in that respect ?
  2. In the hope I am indeed wrong, what could be the correct format to parse such '2016-11-29T18:47:21.766+01:00[Europe/Paris]' strings ?

Upvotes: 1

Views: 700

Answers (1)

Emmanuel Guiton
Emmanuel Guiton

Reputation: 1445

Upgrading from jodatime 2.9.4 to jodatime 2.9.5 solved the issue. With Elasticsearch 2.4.2 (which depends on jodatime 2.9.5) the problem is gone. I just ran again my tests and they pass.

I tried again the test with a dependency on jodatime 2.9.4 and it fails. 2.9.5 works fine. Do not consider my last comment, which is wrong. I must had done something wrong with the dependencies.

Upvotes: 1

Related Questions