1

我将 2 个日历设置为相同的日期,但 Cal1.CompareTo(Cal2) 的结果不是所需的零,因为 2 个日历的时间不同。有没有办法减少日历条目的时间?一种解决方案是将日历条目转换为“日期”类型。然而 Date 几乎被弃用(或者至少它的构造函数使用日历的日、月和年参数)还有其他想法吗?对 timeInMuliseconds 进行一些模计算听起来很难看。

TIA

4

3 回答 3

4

您可以在两个对象上将 MILLISECOND(或任何您不想要的字段)设置为零。

例子:

cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.HOUR , 0);
cal.set(Calendar.MINUTE , 0);
cal.set(Calendar.SECOND , 0);

这是日历的javadoc

于 2012-10-23T17:47:45.317 回答
2

Personally, I would strongly advise you to use Joda Time instead. At that point you can use LocalDate instead, which accurately represents the information you're interested in. Joda Time has a much richer set of types. It's generally a far better date/time API than the built-in Calendar and Date classes.

Note that you should also consider the time zone - the same instant in time can be different dates for two different people... (you'd have to consider this when constructing a LocalDate).

All this is assuming the two values are in the same calendar system, too - but that's pretty likely.

于 2012-10-23T17:49:10.080 回答
0

打印您的日历:

Log.d("Test","Time1: "+cal1.getTimeInMillis());
Log.d("Test","Time2: "+cal2.getTimeInMillis());

如果您没有得到相同的时间,并且您希望两者都相同,请执行以下操作:

cal2 = cal1.clone;
于 2012-10-23T18:38:25.207 回答