简体   繁体   English

Java中的日历...我得到两个不同值的相同结果

[英]Calendar in Java… I get the same result for two different values

    Calendar cal = Calendar.getInstance();
    long now = cal.getTimeInMillis();
    int year = 2014;
    int month = 3;
    int date = 8;
    cal.set(year, month - 1, date);
    long dday = cal.getTimeInMillis();
    long count = (dday - now) / (1000 * 24 * 60 * 60);
    System.out.println((dday - now));
    System.out.println(count);

This code figures out how many days between now and a date. 此代码计算出从现在到日期之间的天数。 However, I get the same result for Mar. 8 and Mar. 9. Please help! 但是,我在3月8日和3月9日得到了相同的结果。请帮忙! Thank you in advance. 先感谢您。


More detailed source code example, by Basil Bourque. 更详细的源代码示例,作者:Basil Bourque。

Calendar cal = Calendar.getInstance();
long nowInMillis = cal.getTimeInMillis();

cal.set( 2014, Calendar.MARCH, 8 );  // March 8, 2014   // year, month, day
long march_8_2014_inMillis = cal.getTimeInMillis();

cal.set( 2014,  Calendar.MARCH, 9 );  // March 9, 2014   // year, month, day
long march_9_2014_inMillis = cal.getTimeInMillis();

long daysTil8th = ( march_8_2014_inMillis - nowInMillis ) / ( 1000 * 24 * 60 * 60 );
long daysTil9th = ( march_9_2014_inMillis - nowInMillis ) / ( 1000 * 24 * 60 * 60 );

System.out.println( "( march_8_2014_inMillis - nowInMillis ) in artificial days: " + daysTil8th + ", in milliseconds: " + ( march_8_2014_inMillis - nowInMillis ) );
System.out.println( "( march_9_2014_inMillis - nowInMillis ) in artificial days: " + daysTil9th + ", in milliseconds: " + ( march_9_2014_inMillis - nowInMillis ) );

System.out.println( "( march_9_2014_inMillis - nowInMillis ) - ( march_8_2014_inMillis - nowInMillis ): " + ( ( march_9_2014_inMillis - nowInMillis ) - ( march_8_2014_inMillis - nowInMillis ) ) ) ;
System.out.println( "( march_9_2014_inMillis - nowInMillis ) - ( march_8_2014_inMillis - nowInMillis ) / ( 1000 * 60 * 60 ): " + ( ( march_9_2014_inMillis - nowInMillis ) - ( march_8_2014_inMillis - nowInMillis ) ) / ( 1000 * 60 * 60 ) ) ;

When run in Seattle, Java 7, Mac OS X 10.8.5 at 2013-11-20T17:35:01.119-08:00 (Pacific Standard Time)… 在西雅图,Java 7,Mac OS X 10.8.5上运行时2013-11-20T17:35:01.119-08:00(太平洋标准时间)......

( march_8_2014_inMillis - nowInMillis ) in artificial days: 108, in milliseconds: 9331200000
( march_9_2014_inMillis - nowInMillis ) in artificial days: 108, in milliseconds: 9414000000
( march_9_2014_inMillis - nowInMillis ) - ( march_8_2014_inMillis - nowInMillis ): 82800000
( march_9_2014_inMillis - nowInMillis ) - ( march_8_2014_inMillis - nowInMillis ) / ( 1000 * 60 * 60 ): 23

NOTE: For my time zone, DST begins 2:00 AM on Sunday, March 9, 2014. 注意:对于我的时区, 夏令时开始于2014年3月9日星期日凌晨2:00。

QUESTION: Why 108 in both lines of output above? 问题:为什么上面的两行输出都是108 The answer is DST, but how exactly? 答案是DST,但究竟是怎么回事?

3月9日是DST将于2014年转换的日子。因此,计算结果是正确的,3月9日的差异是108天23小时,即108天。

As Jim Garrison said in his correct answer , your integer-based math (1000 * 24 * 60 * 60) assumes 24-hours in a day. 正如Jim Garrison在他的正确答案中所说,基于整数的数学运算(1000 * 24 * 60 * 60)假设一天24小时。 Not true for those of us in a time zone with the irrational practice of Daylight Saving Time (DST). 对于我们这些在夏令时 (DST)的非理性练习的时区而言,情况并非如此。 Between the 8th and 9th in United States' time zones is 23 hours, not 24. 在美国的时区8日至9日之间是23小时,而不是24小时。

Lessons learned: 得到教训:

  • Never do your own Date and Time math. 永远不要做自己的日期和时间数学。
  • Never use java.util.Date & .Calendar bundled in Java. 切勿使用Java中捆绑的java.util.Date和.Calendar。
  • Always use a decent library such as Joda-Time or the new JSR 310 in Java 8. 总是使用像Joda-Time这样不错的库或Java 8中的新JSR 310

Here's the same problem solved in Java 7 with Joda-Time 2.3, and correct output. 这是使用Joda-Time 2.3在Java 7中解决的相同问题,以及正确的输出。

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

// Using US west coast time zone because March 9, 2014 at 02:00 is the start of DST Daylight Saving Time.
org.joda.time.DateTimeZone californiaTimeZone = org.joda.time.DateTimeZone.forID("America/Los_Angeles");

org.joda.time.DateTime now = new org.joda.time.DateTime(californiaTimeZone);

// Note the arbitrary choice of times (14 & 21 hours) assigned to new DateTimes.
org.joda.time.DateTime march_8_2014 = new org.joda.time.DateTime( 2014, 3, 8, 14, 0);
org.joda.time.DateTime march_9_2014 = new org.joda.time.DateTime( 2014, 3, 9, 21, 0);

int daysUntil8th = org.joda.time.Days.daysBetween( now , march_8_2014 ).getDays();
int daysUntil9th = org.joda.time.Days.daysBetween( now , march_9_2014 ).getDays();

// If you want whole days, call 'withTimeAtStartOfDate()' method.
// Don't get clever with trying to set a midnight time. Not every day in every time zone has a midnight.
int daysUntil8thStarts = org.joda.time.Days.daysBetween( now , march_8_2014.withTimeAtStartOfDay() ).getDays();
int daysUntil9thStarts = org.joda.time.Days.daysBetween( now , march_9_2014.withTimeAtStartOfDay() ).getDays();

System.out.println("Now in 'America/Los_Angeles' (Pacific Standard Time): " + now );

System.out.println("Days until " +  march_8_2014 + ": " + daysUntil8th );
System.out.println("Days until " + march_9_2014 + ": " + daysUntil9th );

System.out.println("Days until March 8, 2014 starts: " + daysUntil8thStarts );
System.out.println("Days until March 9, 2014 starts: " + daysUntil9thStarts );

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

Now in 'America/Los_Angeles' (Pacific Standard Time): 2013-11-20T18:25:06.424-08:00
Days until 2014-03-08T14:00:00.000-08:00: 107
Days until 2014-03-09T21:00:00.000-07:00: 109
Days until March 8, 2014 starts: 107
Days until March 9, 2014 starts: 108

NOTE: Time of day affects the count of days. 注意:时间会影响天数。 Notice 107 & 109 versus 107 & 108, a two day difference versus one day difference. 请注意107 109 107相对于108&进行了两天的差与1天差。

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

相关问题 我以相同的方式添加两个数字,但得到的值不同 - I add two numbers with the same way but get different values Java Calendar为两个不同的日期返回相同的毫秒值 - Java Calendar returns same millisecond value for two different dates JAVA:如何添加两个声明相同名称但值不同的处理值 - JAVA: How do I add two processed values with same name declared but different values Java - 同一视图上的两个不同的@ModelAttribute占用相同的值 - Java - Two different @ModelAttribute on the same view takes up same values 为什么在两个相同的查询之间会得到相同的结果? - Why did i get the same result between two same query? Java插入排序:“相同”变量得到不同的结果 - Java insertion sorting: "same" variable get different result 如何在同一个 Mock 上获得两个方法调用以返回不同的值? - How can I get two method calls on the same Mock to return different values? 如何获得 java 8 中两个不同类的内连接结果 - how to get the result of Inner join of two different classes in java 8 我如何在转换为西班牙语的同一个Java类中获得两个不同的属性文件? - how will i get two different property files in same java class which is transulated to spanish language? 无论如何在Java HashMap中有两个相同的键,但是值不同? - Anyway to have two of the same keys, but different values in Java HashMap?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM