gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:55:36 25 4
gpt4 key购买 nike

我从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 还会带来哪些其他惊喜?

最佳答案

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

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);
}
}

关于java - 从 jdk6 迁移到 jdk8 后出现 DateTime 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28467706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com