我写了一个注释来检查日期是否在未来,这是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 ??
答案 0 :(得分:2)
您的date
变量实际上是Timestamp
。来自Timestamp
javadoc:
Timestamp.equals(Object)方法在传递a时永远不会返回true java.util.Date类型的值,因为日期的nanos组件是 未知的。
即使Timestamp
继承自Date
,javadoc也建议不要使用Timestamp
Date
,{{1}}。