- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个日期范围(开始日期和结束日期)并且需要知道这是否属于夏令时转换。
是否有任何 Java API 可用于检查这个或任何 Java 代码来实现这个?
最佳答案
夏令时更改在每个国家/地区的不同日期发生,因此首先要知道您正在检查的时区的名称。
我正在使用 Joda-Time 和新的 Java 日期/时间 API 编写此答案,并且都使用 IANA's list of timezone names (格式为 Continent/City
)。这两个 API 也避免使用 3-letter names因为它们是 ambiguous and not standard .
对于下面的代码,我将使用 America/Sao_Paulo
(我住的时区,每年都有夏令时变化),但您可以将其替换为您想要的时区。
下面的代码向您展示了如何检查一个日期是否在 DST 中,以及如何找到下一个 DST 更改发生的日期。因此,如果您有开始日期和结束日期,并且想知道两者是否都在 DST 更改范围内,则可以检查两者是否都在 DST 范围内,还可以找到下一个和上一个 DST 更改(并检查日期是否介于这些更改 - 我不清楚应该如何进行检查)。
另请注意,Joda-Time 处于维护模式并且正在被新 API 取代,因此我不建议使用它启动新项目。即使在 joda's website它说:“请注意,Joda-Time 被认为是一个基本“完成”的项目。没有计划进行重大增强。如果使用 Java SE 8,请迁移到 java.time (JSR-310)。” .
您可以使用 org.joda.time.DateTimeZone
类(class)。要了解所有可用时区,请调用 DateTimeZone.getAvailableIDs()
.
下面的代码检查一个日期是否在 DST 中,并找到下一个 DST 更改发生的日期:
// create timezone object
DateTimeZone zone = DateTimeZone.forID("America/Sao_Paulo");
// check if a date is in DST
DateTime inDst = new DateTime(2017, 1, 1, 10, 0, zone);
// isStandardOffset returns false (it's in DST)
System.out.println(zone.isStandardOffset(inDst.getMillis()));
// check when it'll be the next DST change
DateTime nextDstChange = new DateTime(zone.nextTransition(inDst.getMillis()), zone);
System.out.println(nextDstChange); // 2017-02-18T23:00:00.000-03:00
// check if a date is in DST
DateTime noDst = new DateTime(2017, 6, 18, 10, 0, zone);
// isStandardOffset returns true (it's not in DST)
System.out.println(zone.isStandardOffset(noDst.getMillis()));
// check when it'll be the next DST change
nextDstChange = new DateTime(zone.nextTransition(noDst.getMillis()), zone);
System.out.println(nextDstChange); // 2017-10-15T01:00:00.000-02:00
如果您想查找上一个 DST 更改(而不是下一个),请调用 previousTransition()
而不是 nextTransition()
.
如果您使用的是 Java 8,则 new java.time API已经原生。
如果您使用的是 Java <= 7,您可以使用 ThreeTen Backport ,Java 8 的新日期/时间类的一个很好的反向移植。对于 Android,有 ThreeTenABP (更多关于如何使用它的信息 here)。
下面的代码适用于两者。唯一的区别是包名称(在 Java 8 中是 java.time
,在 ThreeTen Backport(或 Android 的 ThreeTenABP)中是 org.threeten.bp
),但是类和方法名称是相同的。
代码与 Joda-Time 的版本非常相似。主要区别:
isStandardOffset()
要检查日期是否不在 DST 中,新的 API 有 isDaylightSavings()
检查日期是否为夏令时。DateTimeZone
中提供方法类,但新 API 有一个专门用于 DST 规则的类 (java.time.zone.ZoneRules
)java.time.zone.ZoneOffsetTransition
而不是直接返回日期(此对象提供有关 DST 更改的更多信息,如下所示)。尽管存在所有这些差异,但想法非常相似:
// create timezone object
ZoneId zone = ZoneId.of("America/Sao_Paulo");
// get the timezone's rules
ZoneRules rules = zone.getRules();
// check if a date is in DST
ZonedDateTime inDST = ZonedDateTime.of(2017, 1, 1, 10, 0, 0, 0, zone);
// isDaylightSavings returns true (it's in DST)
System.out.println(rules.isDaylightSavings(inDST.toInstant()));
// check when it'll be the next DST change
ZoneOffsetTransition nextTransition = rules.nextTransition(inDST.toInstant());
// getInstant() returns the UTC instant; atZone converts to the specified timezone
System.out.println(nextTransition.getInstant().atZone(zone)); // 2017-02-18T23:00-03:00[America/Sao_Paulo]
// you can also check the date/time and offset before and after the DST change
// in this case, at 19/02/2017, the clock is moved 1 hour back (from midnight to 11 PM)
ZonedDateTime beforeDST = ZonedDateTime.of(nextTransition.getDateTimeBefore(), nextTransition.getOffsetBefore());
System.out.println(beforeDST); // 2017-02-19T00:00-02:00
ZonedDateTime afterDST = ZonedDateTime.of(nextTransition.getDateTimeAfter(), nextTransition.getOffsetAfter());
System.out.println(afterDST); // 2017-02-18T23:00-03:00
// check if a date is in DST
ZonedDateTime noDST = ZonedDateTime.of(2017, 6, 1, 10, 0, 0, 0, zone);
// isDaylightSavings returns false (it's not in DST)
System.out.println(rules.isDaylightSavings(noDST.toInstant()));
// check when it'll be the next DST change
nextTransition = rules.nextTransition(noDST.toInstant());
// getInstant() returns the UTC instant; atZone converts to the specified timezone
System.out.println(nextTransition.getInstant().atZone(zone)); // 2017-10-15T01:00-02:00[America/Sao_Paulo]
// you can also check the date/time and offset before and after the DST change
// in this case, at 15/10/2017, the clock is moved 1 hour forward (from midnight to 1 AM)
beforeDST = ZonedDateTime.of(nextTransition.getDateTimeBefore(), nextTransition.getOffsetBefore());
System.out.println(beforeDST); // 2017-10-15T00:00-03:00
afterDST = ZonedDateTime.of(nextTransition.getDateTimeAfter(), nextTransition.getOffsetAfter());
System.out.println(afterDST); // 2017-10-15T01:00-02:00
如果您想查找上一个 DST 更改而不是下一个,您可以调用 rules.previousTransition()
而不是 rules.nextTransition()
.
关于Java API 获得一年的夏令时界限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44586105/
举个例子,让我们使用以下 cron 表达式:“0 0 14 1 * ?” -> 在每月第一天的 14:00 触发。 我使用 Quartz CronScheduleBuilder 来构建表达式,但这无关
由于某种原因,夏令时开始时,我的一个网站在午夜 12:00 自动发布,从晚上 11:00 开始发布。有什么方法可以设置 MySQL 服务器变量以正确处理夏令时更改吗? 最佳答案 请提供... SHOW
如何检查夏令时是否生效? 最佳答案 您可以使用time.localtime并查看返回值中的 tm_isdst 标志。 >>> import time >>> time.localtime() (201
我有一个奇怪的问题: 我从 mysql 数据库中获取一些数据,我将这些数据用 strotime 转换为时间戳。然后我将这些时间戳与 for 循环进行比较: for ($i=$start;$imodif
如何检查夏令时是否有效? 最佳答案 您可以使用 time.localtime并查看返回值中的 tm_isdst 标志。 >>> import time >>> time.localtime() (20
我使用以下代码在文件中生成日期: echo "DTSTART;TZID=" . date_default_timezone_get() . ":" . strftime('%Y%m%d', strto
在下面,为什么 to_char() 中不包含夏令时?我怎样才能获得夏令时的时间? SELECT systimestamp AT TIME ZONE 'Australia/Adelaide' from
以下是连续两天之间的小时数计算: (AbsoluteTime[{2011, 3, 14}] - AbsoluteTime[{2011, 3, 13}]) / 3600 因此,您可能不会对 Mathem
function DateTimeToFileTime(FileTime: TDateTime): TFileTime; var LocalFileTime, Ft: TFileTime; S
我制作了一个小代码片段。告诉你几岁,精确到秒。 但是我发现了一些奇怪的事情。我知道时间和时区是工作的噩梦,但这是在我自己的国家和时区内,所以我有点困惑。 如果我输入日期。 27-12-2013(dd-
在 Jenkins 的夜间构建中,我们的一项测试在凌晨 2:00:12 失败。经过一段时间的调试和更改计算机的系统时间后,我感到非常困惑。然后我写了下面的测试(模拟问题),它失败了,但我不明白为什么。
linux如何处理夏令时(DST) 切换是否立即发生,例如 3 点钟立即切换到 2 点钟?或者变化缓慢我问你这个是因为我的服务器上有很大的日期基础,如果这个切换在数据库中的条目上立即发生一小时,将会
我正在使用 NSDate 格式化程序从 Facebook Graph API 请求(格林威治标准时间)获取日期并将其转换为用户的本地时区。我可以很好地读取日期并设置它的新时区,但是当我打印出新日期时,
我的 API 使用 ISO 8601 向客户端表示时间。我们有一个功能,我们希望显示它来自哪个时区。通过纯粹存储时区偏移,我们正在失去对细节的跟踪。 即,犹他州和亚利桑那州遵守 MST -06:00半
PHP代码 $t = strtotime( '2012-09-21T03:00:00+00:00 America/Chicago' ); $t2 = date('c',$t); echo $t2;
PHP代码 $t = strtotime( '2012-09-21T03:00:00+00:00 America/Chicago' ); $t2 = date('c',$t); echo $t2;
我有这个问题。我有这个格式的日期 var datestring = "2017-10-30T15:03:10.933044Z"; 如果我这样写代码 var d = new Date(datestrin
每当日光切换发生时,我都会触发事件。 我知道何时会发生 Spring /秋季切换 TimeZone.CurrentTimeZone.GetDaylightChanges(year) 这会返回 2 个日
我正在开发一个关心夏令时变化的关键应用程序。 我试图通过比较跨越夏令时变化的日期来手动模拟运行时可能发生的情况,因此我进行了以下测试。我当前所在的位置是意大利,因此今年从 CEST(中欧夏令时)更改为
我有这个应用程序,它将当前日期与来自 xml 文件的已解析日期进行比较: Date timeDiff = new Date(date.getTime() - new Date().getTime())
我是一名优秀的程序员,十分优秀!