当前位置: 首页 > 面试题库 >

如何获取Joda日期之间的正确小时数?

松智勇
2023-03-14
问题内容

我想获取两个日期之间的所有夏令时(DST)小时。

这是我的示例代码:

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());
}

好吧,我显然缺少一些东西,但是我无法发现我的错误。


问题答案:

主要问题是您 无法指定时区

当我在西雅图的此处运行您的代码时,我会743在2014年3月获得工作时间。为什么?由于我的默认时区。在美国西海岸的夏令时开始于2014年3月9日,星期日,02:00。参见本页,2014年时间更改日期。因此,第9天实际上是23小时而不是24小时。

但是,如果冰岛有人使用完全相同的代码,她会得到的744。为什么?因为冰岛人民太聪明了,以至于不会理会夏令时。

同样,作为一个好习惯,withTimeAtStartOfDay()当您尝试每天工作时,应调用Joda-
Time方法。以前我们使用Joda-
Time的midnight方法,但是不赞成使用这些方法,因为某些日历中的某些日子没有午夜。

提示:请注意standard文件名为的Joda-Time方法,该方法解释为每天24小时的假设。换句话说,这些方法忽略了夏时制。

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

// © 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 Time中的实际日期和时间?正确地说,我的意思是在我国的时间。我阅读了官方页面和一些教程- 关于语言环境和时区的很多事情,但我感到很困惑。我没有找到如何简单地获得它的任何示例。 我有两件事需要它: 要获取最新信息以供讨论, 要获取当前时间,我将其与出生日期进行“比较”并计算年龄。 当我拥有UTC + 1(布拉格-捷克共和国)时,如何设置当前时间? 问题答案: 这是

  • 在Symfony 2中有两个日期时间格式的字段。现在我想得到提交表单后的总小时数 实体php 显示html 这将抛出一个错误 在呈现模板期间引发异常(“DateTime::_construct()希望参数1为字符串,对象为给定的”)在。。。 你如何简单地得到同一天两个小时之间的小时数?

  • 问题内容: 假设这是如何获取乔达时间的当前时间: 您如何计算变量和的值? 我正在试图做的是产生一些SQL做的有间发生的所有交易记录的查询和。 问题答案: 我会用: 然后检查是否有任何特定时间。 当然,这部分取决于确切存储在数据库中的内容以及您感兴趣的时区。

  • 问题内容: 在我的应用程序中,用户应从选择日期。问题是生成此列​​表。例如,我需要 2010年至2013年 或 6月至8月 之间的所有日期(期间可能是 day , month , year )。是否有任何方法可以获取该数据? 范例:我需要 2013年1月1* 日 至2013年1月1 日之间的日期 * 2013年1月1日 2013年2月1日 2013年3月1日 2013年4月1日 2013年5月1日

  • 问题内容: 我有两个这样的表: 表格1 表2 我想从 Table1中 选择并插入 Table2中 。 例如: 在表1中,我有这个 在表2中,我想要这个 带有样本数据的表结构 问题答案: 感谢您的架构。它使处理您的问题变得容易。我对您的架构进行了一些更改以利用auto_increment 在这里,我在emp_leave_daywise表上添加了唯一约束,因为id整数上的主键不能确保记录不重复。 em

  • 问题内容: 如何从Python中的日期时间对象获取日期名称(例如星期一,星期二,星期三,星期四,星期五,星期六和星期日)? 因此,例如,应该给我。 问题答案: 有关datetime.now,datetime.strftime以及有关strftime的更多信息,请参见Python文档。