java – 无法解析DateTime – 由于时区偏移过渡而导致的非法时刻(欧洲/柏林)

前端之家收集整理的这篇文章主要介绍了java – 无法解析DateTime – 由于时区偏移过渡而导致的非法时刻(欧洲/柏林)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么我不能解析以下日期?
DateTime.parse("2015-03-29 02:35:00",DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));

结果:

org.joda.time.IllegalInstantException: Cannot parse "2015-03-29 02:35:00": Illegal instant due to time zone offset transition (Europe/Berlin)
    at org.joda.time.format.DateTimeParserBucket.computeMillis(DateTimeParserBucket.java:471)
    at org.joda.time.format.DateTimeParserBucket.computeMillis(DateTimeParserBucket.java:411)
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:882)
    at org.joda.time.DateTime.parse(DateTime.java:160)
    at Testasd.test(Testasd.java:9)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

这有什么问题?
如果我将年份更改为2014-03-29 02:35:00它的工作原理!

解决方法

正如 link解释的那样:

Joda-Time only allows the key classes to store valid date-times. For
example,31st February is not a valid date so it can’t be stored
(except in Partial). The same principle of valid date-times applies to
daylight savings time (DST). In many places DST is used,where the
local clock moves forward by an hour in spring and back by an hour in
autumn/fall. This means that in spring,there is a “gap” where a local
time does not exist. The error “Illegal instant due to time zone
offset transition” refers to this gap. It means that your application
tried to create a date-time inside the gap – a time that did not
exist. Since Joda-Time objects must be valid,this is not allowed.

您正在引用由于夏令时更改而不存在的日期时间.

解决方案是使用parseLocalDateTime.

猜你在找的Java相关文章