方法 java.util.Date
方法 .equals
是否对 date format
无动于衷?我的意思是我已经尝试过使用不同格式的 date.before
和 date.after
并且它们表现正常,但是,我不确定 Date.equals
因为我担心它的行为与 java.lang.Object.equals
相同,所以我正在寻求一个坚定而明确的答案。
最佳答案
Is the method java.util.Date method .equals indifferent in what concerns the date format or not?
Date
对象没有 格式 - 它不知道信息来自何处。 只是自 Unix 纪元(1970 年 1 月 1 日午夜 UTC)以来的毫秒数。它也不知道时区。因此,您可以以非常不同的方式创建两个 Date
对象,它们最终表示相同的时间点 - 而 equals
将在那里返回 true
。
了解哪些信息是对象状态的一部分,哪些不是对象状态的一部分,这一点非常重要 - 特别是对于日期和时间 API,在某些地方它可能违反直觉。如有任何疑问,请仔细阅读文档。例如,来自 documentation for Date
:
The class Date represents a specific instant in time, with millisecond precision.
在 equals
中:
Compares two dates for equality. The result is
true
if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object.Thus, two
Date
objects are equal if and only if thegetTime
method returns the same long value for both.
关于java - 库 java.util.Date 中用于日期比较的 equals 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27899264/