从 jdk6 迁移到 jdk8 后 DateTime 出现问题

Issue with DateTime after migration from jdk6 to jdk8

我从 jdk1.6 迁移到 jdk1.8 后发现了这个烦人的异常。对于 1.6 它工作正常,但是 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);
    }
}

输出:

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.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.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:53)
    at org.junit.runners.ParentRunner.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)

美国当地

时区 +11

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

迁移后 DdateTime 还能给我带来哪些惊喜?

我不确定问题出在哪里,因为我无法在此处重现。我怀疑这是这里的格式化程序:

private static final String ISSUED_DATE_PATTERN = "MMM dd yyyy hh:mmaa";

您可能需要使用单个 'd''a' 作为日期,并且 AM/PM。

或者,Java 8 中的 java.time.* API 将满足您的需要:

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import org.junit.Test;

public class JavaTimeTest {

    public static final String strDate = "Nov 2 2010 12:27AM";
    private static final String ISSUED_DATE_PATTERN = "MMM d yyyy hh:mma";
    private static final ZoneId TIMEZONE = ZoneId.of("America/Chicago");
    private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(ISSUED_DATE_PATTERN);

    @Test
    public void testName() throws Exception {
      LocalDateTime localDateTime = dateTimeFormatter.parse(strDate, LocalDateTime::from);
      ZonedDateTime dateTime = localDateTime.atZone(TIMEZONE);
      System.out.println(dateTime);
    }
}

我必须明确指定语言环境

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

否则在 jdk>=7

上不起作用