我有:
ZoneId gmt = ZoneId.of("GMT");
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDateNow = localDateTime.toLocalDate();
Set<LocalDate> hollidays = new HashSet<>();
我可以像这样等于 LocalDate
吗?
if(hollidays.contains(localDateNow)){
...
}
最佳答案
是的,可以。
如果您查看 LocalDate::equals()
实现,您将能够看到:
int compareTo0(LocalDate otherDate) {
int cmp = (year - otherDate.year);
if (cmp == 0) {
cmp = (month - otherDate.month);
if (cmp == 0) {
cmp = (day - otherDate.day);
}
}
return cmp;
}
这看起来像是一个正确实现的日期equals
方法 - 这意味着它可以与HashSet
一起正常工作。
摘自 hashCode()
文档:
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
关于java-8 - Set<LocalDate> 包含 LocalDate 最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45813991/