如何使用公历比较没有时间戳的日期?
How to compare dates without time stamp using Gregorian Calendar?
如何使用公历比较没有时间戳的日期?
在下面提供的示例中,两个结果都必须是 true
.
package com.tutorialspoint;
import java.util.*;
public class GregorianCalendarDemo {
public static void main(String[] args) {
// create a new calendar
GregorianCalendar cal1 =
(GregorianCalendar) GregorianCalendar.getInstance();
// print the current date and time
System.out.println("" + cal1.getTime());
// create a second calendar equal to first one
GregorianCalendar cal2 = (GregorianCalendar) (Calendar) cal1.clone();
// print cal2
System.out.println("" + cal2.getTime());
// compare the two calendars
System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));
// change cal 2 a bit
cal2.add(GregorianCalendar.MINUTE, 5);
// compare the two calendars
System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));
}
}
由于您使用 equals
比较两个实例,只有当它们表示完全相同的时间戳时它们才相等。请参阅 java.util.GregorianCalendar#equals Javadoc:
Compares this GregorianCalendar to the specified Object. The result is true if and only if the argument is a GregorianCalendar object that represents the same time value (millisecond offset from the Epoch) under the same Calendar parameters and Gregorian change date as this object.
因此,将 cal2
的值更改为其他值后,它永远不会等于 cal1
。
如果您想比较日期,请使用以下代码:
// compare the two dates
System.out.println("Date1 and date2 are equal:" + cal1.getTime().equals(cal2.getTime()));
假设 cal1
和 cal2
具有相同的时区(即 cal1.getTimeZone().equals(cal2.getTimeZone())
),您可以使用以下代码仅比较日期:
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)
如何使用公历比较没有时间戳的日期?
在下面提供的示例中,两个结果都必须是 true
.
package com.tutorialspoint;
import java.util.*;
public class GregorianCalendarDemo {
public static void main(String[] args) {
// create a new calendar
GregorianCalendar cal1 =
(GregorianCalendar) GregorianCalendar.getInstance();
// print the current date and time
System.out.println("" + cal1.getTime());
// create a second calendar equal to first one
GregorianCalendar cal2 = (GregorianCalendar) (Calendar) cal1.clone();
// print cal2
System.out.println("" + cal2.getTime());
// compare the two calendars
System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));
// change cal 2 a bit
cal2.add(GregorianCalendar.MINUTE, 5);
// compare the two calendars
System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));
}
}
由于您使用 equals
比较两个实例,只有当它们表示完全相同的时间戳时它们才相等。请参阅 java.util.GregorianCalendar#equals Javadoc:
Compares this GregorianCalendar to the specified Object. The result is true if and only if the argument is a GregorianCalendar object that represents the same time value (millisecond offset from the Epoch) under the same Calendar parameters and Gregorian change date as this object.
因此,将 cal2
的值更改为其他值后,它永远不会等于 cal1
。
如果您想比较日期,请使用以下代码:
// compare the two dates
System.out.println("Date1 and date2 are equal:" + cal1.getTime().equals(cal2.getTime()));
假设 cal1
和 cal2
具有相同的时区(即 cal1.getTimeZone().equals(cal2.getTimeZone())
),您可以使用以下代码仅比较日期:
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)