ZoneId Europe/Berlin NullPointerException
ZoneId Europe/Berlin NullPointerException
我写了一个 DateUtil class 将 java.time.LocalDateTime 转换为 java.util.Date 并向后转换。尝试获取系统默认时区或特定时区 (Europe/Berlin) 时出现异常。
我检查了 JVM TimeZone 是否配置正确,确实如此。
这是我的简单代码:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class DateUtil {
public static Date toDate(LocalDateTime dateTime) {
System.out.println(System.getProperty("user.timezone"));
ZoneId zone = ZoneId.of("Europe/Berlin");
return Date.from(dateTime.atZone(zone).toInstant());
}
public static Date toDate(LocalDate date) {
return Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
}
这是例外情况:
java.time.zone.ZoneRulesException: Invalid binary time-zone data: TZDB:Europe/Berlin, version: 2019c
at java.time.zone.TzdbZoneRulesProvider.provideRules(TzdbZoneRulesProvider.java:141)
at java.time.zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:227)
at java.time.ZoneRegion.ofId(ZoneRegion.java:120)
at java.time.ZoneId.of(ZoneId.java:411)
at java.time.ZoneId.of(ZoneId.java:359)
at de.swkbank.camunda.ks.smarta.util.DateUtil.toDate(DateUtil.java:11)
at de.swkbank.camunda.ks.smarta.delegate.WorkingHoursCalculator.getNextDate(WorkingHoursCalculator.java:16)
at de.swkbank.camunda.ks.smarta.delegate.WorkingHoursCalculatorTest.test(WorkingHoursCalculatorTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access0(ParentRunner.java:66)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:293)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.NullPointerException
at java.time.zone.ZoneOffsetTransition.getDateTimeAfter(ZoneOffsetTransition.java:287)
at java.time.zone.ZoneRules.<init>(ZoneRules.java:292)
at java.time.zone.ZoneRules.readExternal(ZoneRules.java:456)
at java.time.zone.Ser.readInternal(Ser.java:194)
at java.time.zone.Ser.read(Ser.java:188)
at java.time.zone.TzdbZoneRulesProvider.provideRules(TzdbZoneRulesProvider.java:136)
... 40 more
我的电脑配置有误吗?它是带有 Win 10 的 Microsoft Surface。JVM 是 RedHat JDK 1.8.0.242
不确定到底出了什么问题。但是在定义时区的 tzdb(时区数据库)中似乎存在问题,可能是损坏。
并且您的 tzdb 已过时,版本为 2019c。我建议您更新 JDK 中的 tzdb,或者将整个 JDK 替换为更高版本。无论哪种方式,您都可能会看到错误消失。
顺便说一下,您应该尽可能逐步停止使用 Date
和 Calendar
类。 java.time 类 旨在完全取代遗留的 类。在必须的地方来回转换,以与尚未更新为 java.time 的旧代码交互。但是,请尽可能继续前进。那些老类真是太坏了
模拟 LocalDateTime::now 时出现问题。这导致异常。我可以通过向 now-Call 注入 java.time.Clock 并在测试中返回固定时钟来解决模拟问题。
我写了一个 DateUtil class 将 java.time.LocalDateTime 转换为 java.util.Date 并向后转换。尝试获取系统默认时区或特定时区 (Europe/Berlin) 时出现异常。 我检查了 JVM TimeZone 是否配置正确,确实如此。
这是我的简单代码:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class DateUtil {
public static Date toDate(LocalDateTime dateTime) {
System.out.println(System.getProperty("user.timezone"));
ZoneId zone = ZoneId.of("Europe/Berlin");
return Date.from(dateTime.atZone(zone).toInstant());
}
public static Date toDate(LocalDate date) {
return Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
}
这是例外情况:
java.time.zone.ZoneRulesException: Invalid binary time-zone data: TZDB:Europe/Berlin, version: 2019c
at java.time.zone.TzdbZoneRulesProvider.provideRules(TzdbZoneRulesProvider.java:141)
at java.time.zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:227)
at java.time.ZoneRegion.ofId(ZoneRegion.java:120)
at java.time.ZoneId.of(ZoneId.java:411)
at java.time.ZoneId.of(ZoneId.java:359)
at de.swkbank.camunda.ks.smarta.util.DateUtil.toDate(DateUtil.java:11)
at de.swkbank.camunda.ks.smarta.delegate.WorkingHoursCalculator.getNextDate(WorkingHoursCalculator.java:16)
at de.swkbank.camunda.ks.smarta.delegate.WorkingHoursCalculatorTest.test(WorkingHoursCalculatorTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access0(ParentRunner.java:66)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:293)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.NullPointerException
at java.time.zone.ZoneOffsetTransition.getDateTimeAfter(ZoneOffsetTransition.java:287)
at java.time.zone.ZoneRules.<init>(ZoneRules.java:292)
at java.time.zone.ZoneRules.readExternal(ZoneRules.java:456)
at java.time.zone.Ser.readInternal(Ser.java:194)
at java.time.zone.Ser.read(Ser.java:188)
at java.time.zone.TzdbZoneRulesProvider.provideRules(TzdbZoneRulesProvider.java:136)
... 40 more
我的电脑配置有误吗?它是带有 Win 10 的 Microsoft Surface。JVM 是 RedHat JDK 1.8.0.242
不确定到底出了什么问题。但是在定义时区的 tzdb(时区数据库)中似乎存在问题,可能是损坏。
并且您的 tzdb 已过时,版本为 2019c。我建议您更新 JDK 中的 tzdb,或者将整个 JDK 替换为更高版本。无论哪种方式,您都可能会看到错误消失。
顺便说一下,您应该尽可能逐步停止使用 Date
和 Calendar
类。 java.time 类 旨在完全取代遗留的 类。在必须的地方来回转换,以与尚未更新为 java.time 的旧代码交互。但是,请尽可能继续前进。那些老类真是太坏了
模拟 LocalDateTime::now 时出现问题。这导致异常。我可以通过向 now-Call 注入 java.time.Clock 并在测试中返回固定时钟来解决模拟问题。