javajava-8jodatimejava-6

Issue with DateTime after migration from jdk6 to jdk8


I found out this annoying exception after migration from jdk1.6 to jdk1.8. For 1.6 it works fine, but 1.8 returns null:

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Test;

public class JodaTest {


    public static final String strDate = "Nov 2 2010 12:27AM";
    private static final String ISSUED_DATE_PATTERN = "MMM dd yyyy hh:mmaa";
    private static final DateTimeZone TIMEZONE = DateTimeZone.forID("America/Chicago");
    private static DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(ISSUED_DATE_PATTERN).withZone(TIMEZONE);

    @Test
    public void testName() throws Exception {
        DateTime dateTime = dateTimeFormatter.parseDateTime(strDate); //Exception - Invalid format
        System.out.println(dateTime);
    }
}

output:

java.lang.IllegalArgumentException: Invalid format: "Nov 2 2010 12:27AM"
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:866)
    at com.nxsystems.processor.kokard.client.JodaTest.testName(JodaTest.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

local US

TimeZone +11

JAVA_HOME=c:\Java\jdk1.8.0_25\

What other surprises I can expect with DdateTime after migration?


Solution

  • I had to specify locale explicitly

    DateTime dateTime = dateTimeFormatter.withLocale(new Locale("en_EN")).parseDateTime(strDate);
    

    otherwise it doesn't work on jdk>=7