在 Android 中比较两个日历日期时出现问题
我想将当前系统日期与修复日期进行比较,但得到错误的结果。通过尝试找出问题所在,我最终得到了以下 Java 代码:
// today, 18th of August 2010
long currSystem = System.currentTimeMillis();
Calendar calToday = Calendar.getInstance();
Calendar calFix = Calendar.getInstance();
long milliToday = calToday.getTimeInMillis();
long milliOlder = calFix.getTimeInMillis();
long deltaOlder = milliOlder - milliToday;
long deltaSystem = currSystem - milliToday;
// 1st of August 2010
calFix.set( 2010, 8, 1 );
long milliChanged1 = calFix.getTimeInMillis();
long deltaChanged1 = milliChanged1 - milliToday;
boolean isAfter1 = calToday.after( calFix );
boolean isBefore1 = calToday.before( calFix );
// 1st of October 2010
calFix.set( 2010, 10, 1 );
long milliChanged2 = calFix.getTimeInMillis();
long deltaChanged2 = milliChanged2 - milliToday;
boolean isAfter2 = calToday.after( calFix );
boolean isBefore2 = calToday.before( calFix );
调试器中的结果是:
currSystem: 1282160371775
增量更改1:1209600009
deltaChanged2:6480000009
Delta年龄:9
德尔塔系统:-25
isAfter1: false
isAfter2:假
isBefore1: true isBefore2: true 毫更改1:1283369971809
毫改变2:1288640371809
毫旧:1282160371809
milliToday:1282160371800
deltaOlder 和 deltaSystems 的微小差异是由于执行时间造成的,这没问题。 但在我看来,今天(8 月 18 日)和第一个设定日期(8 月 1 日)的比较结果是错误的。 isAfter1 应该为 true,isBefore1 应该为 false; 与第二个设定日期(10 月 1 日)相比是可以的。
有什么建议我在比较两个日期的简单事情中做错了什么吗?
I wanted to compare the current system date to a fix date, but get the wrong result. By trying to find out what's wrong, I ended with the following Java code:
// today, 18th of August 2010
long currSystem = System.currentTimeMillis();
Calendar calToday = Calendar.getInstance();
Calendar calFix = Calendar.getInstance();
long milliToday = calToday.getTimeInMillis();
long milliOlder = calFix.getTimeInMillis();
long deltaOlder = milliOlder - milliToday;
long deltaSystem = currSystem - milliToday;
// 1st of August 2010
calFix.set( 2010, 8, 1 );
long milliChanged1 = calFix.getTimeInMillis();
long deltaChanged1 = milliChanged1 - milliToday;
boolean isAfter1 = calToday.after( calFix );
boolean isBefore1 = calToday.before( calFix );
// 1st of October 2010
calFix.set( 2010, 10, 1 );
long milliChanged2 = calFix.getTimeInMillis();
long deltaChanged2 = milliChanged2 - milliToday;
boolean isAfter2 = calToday.after( calFix );
boolean isBefore2 = calToday.before( calFix );
The results in the debugger are:
currSystem: 1282160371775
deltaChanged1: 1209600009
deltaChanged2: 6480000009
deltaOlder: 9
deltaSystem: -25
isAfter1: false
isAfter2: false
isBefore1: true
isBefore2: true
milliChanged1: 1283369971809
milliChanged2: 1288640371809
milliOlder: 1282160371809
milliToday: 1282160371800
The small differences for deltaOlder and deltaSystems are due to the execution time, that's no problem.
But the results of the comparison of today (18th of August) and the first set date (1st of August) is wrong IMO. isAfter1 should be true and isBefore1 should be false;
The comparison with the second set date (1st of October) is ok.
Any suggenstion what I'm doing wrong in the simple thing to compare two dates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Calendar 对象中,月份是从 0 开始的,因此八月应该是 7,十月应该是 9。
因此,您只需更改“集”以获得正确的数字,它应该可以正常工作。
In a Calendar object, month is 0-based, so August should be 7 and October should be 9.
Therefore, you just need to change your "sets" to have the correct numbers and it should work fine.