java - 库 java.util.Date 中用于日期比较的 equals 方法

标签 java

方法 java.util.Date 方法 .equals 是否对 date format 无动于衷?我的意思是我已经尝试过使用不同格式的 date.beforedate.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 the getTime method returns the same long value for both.

关于java - 库 java.util.Date 中用于日期比较的 equals 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27899264/

相关文章:

java - 如何从图片中获取经纬度信息

java - iOS map 上的位置错误

java - 如何在 jtable 标题中添加复选框,以便只有我可以根据用户的意愿调用打印选中的列?

java - 获取上传输入类型自定义

java - 如何将 Xtend 模板与 C hello World 一起使用?

java - 在 Java (Swing) 中可以注册多个按键监听器事件吗?

java - 在 Java 中使用 Thread 进行 ArrayList<object> 操作

java - 一个简单的 Spark 应用程序,存在 MojoExecutionException 问题

javascript - 如何将在 javascript 中创建的数组传递给另一个 jsp,然后在该 jsp 的 java 函数中使用该数组?

java - 什么样的事情Java能做而Python不能做?