gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:34:06 25 4
gpt4 key购买 nike

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

此代码计算从现在到某个日期之间的天数。但是,我在 3 月 8 日和 3 月 9 日得到了相同的结果。请帮忙!提前谢谢你。


更详细的源代码示例,作者 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 ) ) ;

在 2013-11-20T17:35:01.119-08:00(太平洋标准时间)在西雅图、Java 7、Mac OS X 10.8.5 运行时......

( 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

注意:对于我所在的时区,DST 2014 年 3 月 9 日星期日凌晨 2:00 开始。

问题:为什么在上面的两行输出中都是 108?答案是 DST,但究竟如何?

最佳答案

3 月 9 日是 2014 年夏令时转换的日子。因此,计算正确,3 月 9 日相差 108 天 23 小时,四舍五入为 108 天。

关于java - Java 中的日历...我对两个不同的值得到相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20110352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com