栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

当我在西雅图的此处运行您的代码时,我会

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 pre 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.htmlorg.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() );


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/574680.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号