我正在尝试哈希对象:
protected XMLGregorianCalendar startTime;
getStartTime()的值是 2018-06-21T12:04:10.000Z
XMLGregorianCalendar底部的hashCode方法无法处理毫秒,因此即使我使用具有不同毫秒值的 startTime (如 2018-06- 21T12:04:10.111Z
2018-06-21T12:04:10.000Z 和 2018-06-21T12:04:10.111Z 都会产生相同的hashCode,除非我更改秒数,分钟,小时等,我还需要考虑到毫秒。
是否存在其他可以处理此问题的hashCode()方法?我该怎么做?
public int hashCode() {
// Following two dates compare to EQUALS since in different timezones.
// 2000-01-15T12:00:00-05:00 == 2000-01-15T13:00:00-04:00
//
// Must ensure both instances generate same hashcode by normalizing
// this to UTC timezone.
int timezone = getTimezone();
if (timezone == DatatypeConstants.FIELD_UNDEFINED) {
timezone = 0;
}
XMLGregorianCalendar gc = this;
if (timezone != 0) {
gc = this.normalize();
}
return gc.getYear()
+ gc.getMonth()
+ gc.getDay()
+ gc.getHour()
+ gc.getMinute()
+ gc.getSecond(); //Where's milliseconds???
}