如何使用公历比较没有时间戳的日期?
在下面提供的示例中,两个结果都必须为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));

   }
}

最佳答案

假设cal1cal2具有相同的时区(即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)

关于java - 如何使用公历比较没有时间戳的日期?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32310692/

10-10 22:57