我写了一个注释来检查一个日期是否在未来,这是 ivValid 实现:
public boolean isValid( Date date, ConstraintValidatorContext constraintValidatorContext )
{
String message = null;
Date todayWithoutTime = DateUtils.truncate( Calendar.getInstance().getTime(), Calendar.DAY_OF_MONTH );
if ( ( date == null ) || ( !date.after( todayWithoutTime ) && !date.equals( todayWithoutTime ) ) )
{
message = "{validation.definition.notfutureDate}";
}
if ( message != null )
{
constraintValidatorContext.disableDefaultConstraintViolation();
constraintValidatorContext.buildConstraintViolationWithTemplate( message ).addConstraintViolation();
}
return message == null;
}
我有以下日期对象:
date Timestamp (id=157)
cdate Gregorian$Date (id=192)
cachedFixedDateJan1 734869
cachedFixedDateNextJan1 735234
cachedYear 2013
daylightSaving 3600000
dayOfMonth 30
dayOfWeek 3
era null
forceStandardTime false
fraction 0
hours 0
leapYear false
locale null
millis 0
minutes 0
month 4
normalized true
seconds 0
year 2013
zoneinfo ZoneInfo (id=212)
zoneOffset 7200000
fastTime 1367272800000
nanos 0
和
todayWithoutTime Date (id=173)
cdate Gregorian$Date (id=185)
cachedFixedDateJan1 734869
cachedFixedDateNextJan1 735234
cachedYear 2013
daylightSaving 3600000
dayOfMonth 30
dayOfWeek 3
era null
forceStandardTime false
fraction 0
hours 0
leapYear false
locale null
millis 0
minutes 0
month 4
normalized true
seconds 0
year 2013
zoneinfo ZoneInfo (id=212)
zoneOffset 7200000
fastTime 1367272800000
date.getTime() 给出 13672728000000,todayWithoutTime.getTime() 给出 13672728000000。
有人可以解释一下为什么检查!date.equals(todayWithoutTime)返回 true 吗?