从jdk6迁移到jdk8后的DateTime问题

问题描述 投票:0回答:2

我从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)

美国当地

时区+11

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

迁移后 DdateTime 还会带来哪些其他惊喜?

java java-8 jodatime java-6
2个回答
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 java.util.Locale;

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 final DateTimeFormatter dateTimeFormatter =
        DateTimeFormatter.ofPattern(ISSUED_DATE_PATTERN);

    public static void main(String... args) {
        LocalDateTime localDateTime = dateTimeFormatter
            .withLocale(Locale.US)
            .parse(strDate, LocalDateTime::from);
        ZonedDateTime dateTime = localDateTime.atZone(TIMEZONE);
        System.out.println(dateTime);
    }
}

0
投票

我必须明确指定区域设置

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

否则在jdk>=7上不起作用

© www.soinside.com 2019 - 2024. All rights reserved.