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

时间:2015-02-12 01:23:55

标签: java java-8 jodatime jdk1.6

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

美国当地

TimeZone +11

JAVA_HOME = C:\爪哇\ jdk1.8.0_25 \

迁移后DdateTime还能带来什么惊喜?

2 个答案:

答案 0 :(得分:1)

我不确定问题是什么,因为我不能在这里重现它。我怀疑这是格式化程序:

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

您可能需要使用单个'd''a'作为日期和上午/下午。

或者,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);
    }
}

答案 1 :(得分:0)

我必须明确指定区域设置

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

否则它不适用于jdk> = 7