我在两个地方的组织工作。其中之一是智利,那里的夏时制非常不可预测,并且每年之间都有所不同。我们有一个依赖时间的应用程序,到目前为止,一直在使用joda time。当智利政府决定更改夏令时时,我们使用joda代码将偏移量设置为默认值DateTimeZone
:
/**
* Updates the default time zone, i.e. the time zone which is used as local time.
*/
private void updateDefaultTimeZone() {
if (isEmpty(Configuration.Value.TIME_ZONE_OFFSET)) {
// make the site's default time zone the current time zone for joda time,
// this should always be the case for the non-Chile site, and work for the Chile site during most of the year
DateTimeZone.setDefault(siteService.getSiteTimeZone());
} else {
// makes a user defined time zone the current time zone for joda time
// this will be used when Chile is delaying or pulling forward the transition from/to daylight
// saving time and the JVM is therefore not able to calculate the local times appropriately
Integer offset = getInteger(Configuration.Value.TIME_ZONE_OFFSET);
DateTimeZone.setDefault(DateTimeZone.forOffsetHours(offset));
}
}
我们正在尝试将代码库从joda时代过渡到Java 8时代,但是我真的不知道参与类似工作的类。我猜可能是ZoneRules
,但不确定。是否有人暗示如何最干净地完成此操作?
此外,Java 8时间转换单位的最佳方法是什么?假设我有24小时,我想将其转换为几天。 (我问,因为在这种情况下,似乎许多方法都可以毫秒操作,而我想使用小时,但是认为必须有比手动执行算术更好的内置方法。)
使用java.util.TimeZone.setDefault(TimeZone)
。由于JSR-310 java.util.TimeZone.setDefault(TimeZone)
与JDK集成在一起,因此不需要单独的方法。
((要转换单位,您可能会发现最简单的使用java.time.*
,但也许另外一个重点突出的问题会为您提供帮助。)