gpt4 book ai didi

java - 夏令时持续时间

转载 作者:行者123 更新时间:2023-12-01 14:28:57 26 4
gpt4 key购买 nike

我有一个对象 Shift,它有两个字段:startDateTimeendDateTime 作为来自 Joda-Time 的 DateTime .

我的类次包括英国夏令时的变化。它开始于 25/03/2017 13:00 结束于 26/03/2017 02:00 (基本上应该结束于 26/03/2017 01 :00,但此日期不存在,并且 endDate 移动 +1 小时)。根据这个site :

When local standard time was about to reach Sunday, 26 March 2017, 01:00:00 clocks were turned forward 1 hour to Sunday, 26 March 2017, 02:00:00 local daylight time instead.

现在,如果我想跟踪员工的工作小时数 new Duration(startDateTime, endDateTime).toStandardHours().getHours() 会给我 13 个小时。

如何使用 Joda-Time 检测夏令时是在轮类期间开始还是结束?

最佳答案

您可以使用 org.joda.time.DateTimeZone类(class)。它包含有关世界上所有时区的 DST 更改的信息,因此您无需检测 DST 更改:如果您正确告知您正在使用的时区,API 会为您完成这项工作。

当您使用英国时区时,您可以直接使用 Europe/London时区 - 这些名称的格式为 Continent/City来自 IANA database ,它被 Joda-Time、Java 和许多其他 API 使用。您可以通过调用 DateTimeZone.getAvailableIDs() 获得所有时区的列表。 .

当您使用 DateTimeZone ,它已经包含历史期间的所有 DST 转换,因此 API 会为您完成所有数学计算。您只需要创建您的 DateTime此时区的实例:

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Duration;

// London timezone - it contains all info about DST shifts
DateTimeZone london = DateTimeZone.forID("Europe/London");

// Start on 25/03/2017 13:00
DateTime start = new DateTime(2017, 3, 25, 13, 0, london);
// ends on 26/03/2017 02:00
DateTime end = new DateTime(2017, 3, 26, 2, 0, london);

// get the difference between start and end
Duration duration = new Duration(start, end);
System.out.println(duration.getStandardHours()); // 12

输出将为 12 (API 使用 DateTime 的时区信息来计算时差,包括 DST 偏移)。

您还可以使用:

duration.toStandardHours().getHours()

或者类org.joda.time.Hours :

Hours.hoursBetween(start, end).getHours()

全部返回12 , 它们都是等价的。


Java 新日期/时间 API

Joda-Time 已停产并被新的 API 取代,因此如果您正在考虑迁移,则可以开始使用新的 Date/Time API,但如果您有大量使用 Joda 的代码库或不想现在迁移它,您可以不考虑其余的答案。

无论如何,即使在joda's website它说:“请注意,Joda-Time 被认为是一个基本“完成”的项目。没有计划进行重大增强。如果使用 Java SE 8,请迁移到 java.time (JSR-310)。” .*

如果您使用的是 Java 8,请考虑使用 new java.time API .这更容易,less bugged and less error-prone than the old APIs .我不确定它是否已适用于所有 Android 版本(但请参阅下面的备选方案)。

如果您使用的是 Java <= 7,您可以使用 ThreeTen Backport ,Java 8 的新日期/时间类的一个很好的反向移植。对于 Android,有一种方法可以使用它,使用 ThreeTenABP (更多关于如何使用它的信息 here)。

下面的代码适用于两者。唯一的区别是包名称(在 Java 8 中是 java.time,在 ThreeTen Backport(或 Android 的 ThreeTenABP)中是 org.threeten.bp),但是类和方法名称是相同的。

新 API 还使用 IANA 时区名称并包含有关 DST 转换的相同历史信息(以获取所有时区的列表:ZoneId.getAvailableZoneIds())。代码非常相似,而且获取差异的方法也不止一种:

import java.time.Duration;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

// London timezone
ZoneId london = ZoneId.of("Europe/London");

// Start on 25/03/2017 13:00
ZonedDateTime start = ZonedDateTime.of(2017, 3, 25, 13, 0, 0, 0, london);
// ends on 26/03/2017 02:00
ZonedDateTime end = ZonedDateTime.of(2017, 3, 26, 2, 0, 0, 0, london);

// get the difference in hours
System.out.println(ChronoUnit.HOURS.between(start, end)); // 12

// get the duration in hours
Duration duration = Duration.between(start, end);
System.out.println(duration.toHours()); // 12

// using until() method
System.out.println(start.until(end, ChronoUnit.HOURS)); // 12

以上所有三种方法( ChronoUnit.HOURS.between()duration.toHours()start.until() )都返回 12 .根据javadoc , betweenuntil是等价的,as the first just internally calls the second .使用 Duration也是等效的,因为它使用它们之间的纳秒并将其转换为小时。

关于java - 夏令时持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44617846/

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