简体   繁体   English

Date对象和Calendar之间的日期计算不同

[英]Date calculation differs between a Date object and a Calendar

I am writing a code to add amount of days to current date. 我正在编写一个代码来添加当前日期的天数。 I have 2 solutions: 我有2个解决方案:

SOLUTION 1 解决方案1

Date current = new Date();

// Add 100 days
Date d = new Date ( current.getTime() + 100 * 24 * 60 * 60 * 1000 );

System.out.println(d);

SOLUTION 2 解决方案2

Date current = new Date();

GregorianCalendar c = new GregorianCalendar();
c.setTime(current);
c.add(Calendar.DATE, 100);

System.out.println(c.getTime());

RESULT 结果

SOLUTION 1: Sat Apr 05 14:56:33 CDT 2014

SOLUTION 2: Sat Apr 05 13:56:33 CDT 2014

The result of solution_2 is LESS than result of solution_1 ONE hour. solution_2的结果比solution_11小时的结果少。

Anyone has any ideas? 有人有什么想法吗? Thanks! 谢谢!

Solution 2 takes daylight savings into account, whereas solution 1 doesn't. 解决方案2考虑了夏令时,而解决方案1却没有。 Hence the difference. 因此差异。

On days when there is a transition from summer to winter time, or vice versa, the day isn't 24 hours long as the first method assumes. 在从夏季到冬季转换的日子,反之亦然,第一种方法假定,这一天不是24小时。

Daylight Saving Time 夏令时

Six months from now will cross the beginning of Daylight Saving Time in Lincoln, Nebraska US. 从现在开始的六个月将跨越美国内布拉斯加州林肯市夏令时的开始。

Sunday, March 9, 2014 at 2:00:00 AM clocks are turned forward 1 hour to 2014年3月9日星期日上午2:00:00时钟转发1小时到达
Sunday, March 9, 2014 at 3:00:00 AM local daylight time instead 2014年3月9日星期日,当地日光时间凌晨3:00:00

http://www.timeanddate.com/worldclock/clockchange.html?n=132&year=2014 http://www.timeanddate.com/worldclock/clockchange.html?n=132&year=2014

Daylight Saving Time (DST) is one very good reason to avoid doing your own date-time calculations. 夏令时(DST)是避免进行自己的日期时间计算的一​​个很好的理由。 Use a competent library instead. 请使用称职的图书馆。

Joda-Time 乔达时间

Neither Solution 1 nor Solution 2 are good. 解决方案1和解决方案2都不好。 The java.util.Date/Calendar classes are notoriously bad and should be avoided. java.util.Date/Calendar类非常糟糕,应该避免使用。 They are being supplanted in Java 8 by the new java.time.* classes , whose design was inspired by the Joda-Time library. 它们在Java 8中被新的java.time。*类取代,其设计灵感来自Joda-Time库。

My example code below uses Joda-Time 2.3 running in Java 7. 我下面的示例代码使用在Java 7中运行的Joda-Time 2.3。

Time Zone 时区

Your code fails to address the issue of time zones. 您的代码无法解决时区问题。 Specifying a time zone is a better practice than depending on the default time zone. 指定时区是一种比默认时区更好的做法。 My example code is using a time zone for Lincoln, NE, US ("America/Indiana/Knox"). 我的示例代码使用林肯,美国东北部的时区(“America / Indiana / Knox”)。

Example Code 示例代码

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

// Specify a time zone rather than rely on defaults.
DateTimeZone timeZone = DateTimeZone.forID( "America/Indiana/Knox" );

DateTime dateTime = new DateTime( timeZone );
DateTime dateTimeLater = dateTime.plusDays( 100 );

// If you are focusing on the day itself rather than a particular time of day, then use first moment of the day.
DateTime dateTimeStartOfDay = new DateTime( timeZone ).withTimeAtStartOfDay();
DateTime dateTimeLaterStartOfDay = dateTimeStartOfDay.plusDays( 100 ).withTimeAtStartOfDay();

Dump to console… 转储到控制台......

System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTimeLater: " + dateTimeLater );
System.out.println( "dateTimeStartOfDay: " + dateTimeStartOfDay );
System.out.println( "dateTimeLaterStartOfDay: " + dateTimeLaterStartOfDay );
System.out.println( "dateTimeLaterStartOfDay in UTC: " + dateTimeLaterStartOfDay.toDateTime( DateTimeZone.UTC ) );

When run… 跑的时候......

dateTime: 2013-12-26T23:12:33.527-06:00
dateTimeLater: 2014-04-05T23:12:33.527-05:00
dateTimeStartOfDay: 2013-12-26T00:00:00.000-06:00
dateTimeLaterStartOfDay: 2014-04-05T00:00:00.000-05:00
dateTimeLaterStartOfDay in UTC: 2014-04-05T05:00:00.000Z

Convert To juDate 转换为juDate

If you need to convert to a java.util.Date for use with other code, call the toDate method. 如果需要转换为java.util.Date以与其他代码一起使用,请调用toDate方法。

java.util.Date date = dateTime.toDate();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM