如何在Joda日期之间获得正确的小时数? [英] How to get correct number of hours between Joda dates?

查看:145
本文介绍了如何在Joda日期之间获得正确的小时数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在两个日期之间获得所有夏令时(夏令时)。

I want to get all the Daylight Saving Time (DST) hours between two dates.

这是我的示例代码:

public static void main(String[] args) {

    Date startDate = new Date();
    Calendar startCalendar = Calendar.getInstance();
    startCalendar.setTime(startDate);
    startCalendar.set(2014, 2, 1, 0, 0, 0);
    startCalendar.set(Calendar.MILLISECOND, 0);

    Date endDate = new Date();
    Calendar endCalendar = Calendar.getInstance();
    endCalendar.setTime(endDate);
    endCalendar.set(2014, 2, 31, 0, 0, 0);
    endCalendar.set(Calendar.MILLISECOND, 0);

    DateTime startDateTime = new DateTime(startCalendar);
    DateTime endDateTime = new DateTime(endCalendar).plusDays(1);

    Hours hours = Hours.hoursBetween(startDateTime, endDateTime);

    // actual is 744
    System.out.println("Expected: 743, actual: " + hours.getHours());
}

我显然遗漏了一些东西,但我无法发现错误。

Well I am obviously missing something but I can't spot my error.

推荐答案

主要问题是你未能指定时区

当我在西雅图运行您的代码时,我在2014年3月份获得 743 小时。为什么?因为我的默认时区。在美国西海岸,夏令时将于2014年3月9日星期日开始,凌晨2点。请参阅此页面, 2014年的时间更改日期。因此,第9天,实际上是23小时,而不是24小时。

When I run your code here in Seattle, I get 743 hours in the month of March 2014. Why? Because of my default time zone. Here on the west coast of the United States, Daylight Saving Time begins March 9, 2014, Sunday, at 02:00. See this page, Time change dates in 2014. So that day, the 9th, is effectively 23 hours long instead of 24.

但如果冰岛有人运行完全相同的代码,她将获得 744 。为什么?因为冰岛人太聪明了,不能理解夏令时的废话。

But if someone in Iceland ran that exact same code, she would get 744. Why? Because the people of Iceland are too smart to bother with the nonsense of Daylight Saving Time.

另外,作为一个好习惯,你应该调用Joda-Time方法 withTimeAtStartOfDay( ) 尝试使用几天时。以前我们使用Joda-Time的午夜方法,但这些方法已被弃用,因为在某些日历中有些日子没有午夜

Also, as a good habit, you should call the Joda-Time method withTimeAtStartOfDay() when trying to work with days. Formerly we used Joda-Time's midnight methods, but those are deprecated because some days in some calendars do not have a midnight.

提示:请注意以标准命名的Joda-Time方法,文档解释的意思是24小时的假设。换句话说,这些方法忽略了夏令时变换。

Tip: Be aware of Joda-Time methods named with standard, which the doc explains means an assumption of 24-hour days. In other words, those methods ignore Daylight Saving Time shifts.

下面是一些在Java 7中使用Joda-Time 2.3的示例代码。

Here is some example code using Joda-Time 2.3 in Java 7.

// © 2013 Basil Bourque. This source code may be used freely forevery by anyone taking full responsibility for doing so.

// Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 7 and earlier.
// http://www.joda.org/joda-time/

// Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8.
// JSR 310 was inspired by Joda-Time but is not directly based on it.
// http://jcp.org/en/jsr/detail?id=310

// By default, Joda-Time produces strings in the standard ISO 8601 format.
// https://en.wikipedia.org/wiki/ISO_8601

// Time Zone list: http://joda-time.sourceforge.net/timezones.html
org.joda.time.DateTimeZone seattleTimeZone = org.joda.time.DateTimeZone.forID("America/Los_Angeles");
org.joda.time.DateTimeZone icelandTimeZone = org.joda.time.DateTimeZone.forID("Atlantic/Reykjavik");

// Switch between using 'seattleTimeZone' and 'icelandTimeZone' to see different results (23 vs 24).
org.joda.time.DateTime theNinth = new org.joda.time.DateTime( 2014, 3, 9, 0, 0, seattleTimeZone ) ; // Day when DST begins.
org.joda.time.DateTime theTenth = theNinth.plusDays( 1 ); // Day after DST begins.

// Using "hoursBetween()" method with a pair of DateTimes.
org.joda.time.Hours hoursObject = org.joda.time.Hours.hoursBetween( theNinth.withTimeAtStartOfDay(), theTenth.withTimeAtStartOfDay() );
int hoursInt = hoursObject.getHours();
System.out.println( "Expected 23 from hoursInt, got: " + hoursInt );

// Using an Interval.
org.joda.time.Interval interval = new Interval( theNinth.withTimeAtStartOfDay(), theTenth.withTimeAtStartOfDay() );
System.out.println( "Expected 23 from interval, got: " + org.joda.time.Hours.hoursIn(interval).getHours() );

// Using a Period with Standard days.
org.joda.time.Period period = new org.joda.time.Period( theNinth.withTimeAtStartOfDay(), theTenth.withTimeAtStartOfDay() );
org.joda.time.Hours standardHoursObject = period.toStandardHours();
System.out.println( "Expected 24 from standardHoursObject, got: " + standardHoursObject.getHours() );

这篇关于如何在Joda日期之间获得正确的小时数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆