简体   繁体   English

Java-检查两个日期是否相等

[英]Java - Check whether two Dates are Equal

I am trying to compare dates in two different formats: 我正在尝试比较两种不同格式的日期:

Tue Jul 01 00:12:14 EST 2014

which is created using the function: 使用以下功能创建的:

private Date getDate (int day, int month, int year){
    Calendar calendar = Calendar.getInstance();
    calendar.setLenient(false);
    calendar.set(year, month-1, day);
    Date date = calendar.getTime();
    return date;
}

and

2014-07-01

After comparing these two dates, I would like the output to show that they are equal. 比较这两个日期后,我希望输出显示它们相等。 However I BELIEVE, because of the timestamp in the 1st Date, they are not being determined as equal. 但是,我相信,由于第一天的timestamp ,它们的确定不相等。

Is my assumption correct? 我的假设正确吗?

If so, is there a way that I could convert the first date into the second? 如果是这样,是否可以将第一个日期转换为第二个日期? The second Date is being retrieved from an SQL database where the variable is DATE . 从变量为DATESQL数据库中检索第二个Date。

Thank you for your help. 谢谢您的帮助。

It sounds like you are comparing a java.util.Date (an instant in time) with a java.sql.Date (an instant in time whose time of day is midnight). 听起来您正在将java.util.Date (时间点)与java.sql.Date (一天中的午夜时间点)进行比较。

Arithmetic rounding must deal with the local timezones, making it more complex than you might first think. 算术舍入必须处理本地时区,使其比您最初想的要复杂。

The simplest way to compare the two would be to use a data formatter and compare the output: 比较两者的最简单方法是使用数据格式化程序并比较输出:

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
if (f.format(date1).equals(f.format(date2))) {
    // the two dates are on the same "day"
}

java.sql.Date Has Zero Time java.sql.Date的时间为零

The documentation explains that a java.sql.Date has its time portion set to zero (UTC), meaning midnight. 该文档解释说,java.sql.Date的时间部分设置为零(UTC),表示午夜。

So when comparing to a java.util.Date with a non-zero time-of-day, the two will not be equal. 因此,当与非零时间的java.util.Date比较时,两者将不相等。

LocalDate LOCALDATE的

So much easier using Joda-Time of the new java.time package in Java 8. Both offer a LocalDate class that ignores time-of-day. 在Java 8中使用新的java.time包的Joda-Time更加容易。两者都提供了忽略日期时间的LocalDate类。

LocalDate x = new LocalDate( 2014, 5, 6 );
LocalDate y = new LocalDate( 2014, 5, 6 );
boolean same = x.equals( y );

To convert your java.sql.Date to a Joda-Time LocalDate, pass it to the constructor of New LocalDate. 要将java.sql.Date转换为Joda-Time LocalDate,请将其传递给New LocalDate的构造函数。 You may need to also pass DateTimeZone.UTC to be sure it is not interpreted by your JVM's default time zone. 您可能还需要传递DateTimeZone.UTC ,以确保不会被JVM的默认时区解释。

Is my assumption correct? 我的假设正确吗?

Yes, your assumption is correct. 是的,您的假设是正确的。 Two Date instances are correct if both their getTime() results are the same 如果两个Date实例的两个getTime()结果相同,则它们是正确的

from Date.java Date.java

public boolean equals(Object obj) {
    return obj instanceof Date && getTime() == ((Date) obj).getTime();
}

Converting just assumes you need to set the hours,minutes and seconds to 0: 转换只是假设您需要将小时,分钟和秒设置为0:

calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);

Assuming that both dates are in the same timezone and also assuming that the date equality criteria here is the day of the year, I believe you can just compare the date as strings. 假设两个日期都在同一时区,并且假设此处的日期相等条件是一年中的某一天,那么我相信您可以将日期作为字符串进行比较。

To do that, you can use SimpleDateFormat to ensure both are in the same format. 为此,可以使用SimpleDateFormat来确保两者的格式相同。

我建议您将它们都转换为一种特定格式,并使用比较器进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM