In this tutorial we'll see how to compare dates in Java using java.util.Date
, java.util.Calendar
and using the
new Date and Time API in Java which has classes in java.time
package.
Comparing java.util.Date
For comparing two dates method provided by java.util.Date class are-
- int compareTo(Date anotherDate)- Compares two Dates for ordering.
- equals(Object obj)- Compares two dates for equality.
- after(Date when)- Tests if this date is after the specified date.
- before(Date when)- Tests if this date is before the specified date.
Date.compareTo() Java example
compareTo() method returns one of the following three values.
- Returns 0 if the argument Date is equal to this Date.
- Returns a value less than 0 if this Date is before the Date argument
- Returns a value greater than 0 if this Date is after the Date argument
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateCompareTo { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MMMM-yyyy"); Date date1 = sdf.parse("16-08-2022"); Date date2 = sdf.parse("12-08-2022"); System.out.println("date1 is- " + sdf1.format(date1)); System.out.println("date2 is- " + sdf1.format(date2)); Date date3 = sdf.parse("10-08-2022"); Date date4 = sdf.parse("15-08-2022"); System.out.println("date3 is- " + sdf1.format(date3)); System.out.println("date4 is- " + sdf1.format(date4)); // Comparison for date1 and date2 int comp = date1.compareTo(date2); System.out.println("Comparison value is: " + comp); if(comp == 0) { System.out.println("Date1 is equal to Date2"); } else if(comp > 0) { System.out.println("Date1 comes after Date2"); }else if(comp < 0) { System.out.println("Date1 comes before Date2"); } // Comparison for date3 and date4 comp = date3.compareTo(date4); System.out.println("Comparison value is: " + comp); if(comp == 0) { System.out.println("Date3 is equal to Date4"); } else if(comp > 0) { System.out.println("Date3 comes after Date4"); }else if(comp < 0) { System.out.println("Date3 comes before Date4"); } } }
Output
date1 is- 16-August-2022 date2 is- 12-August-2022 date3 is- 10-August-2022 date4 is- 15-August-2022 Comparison value is: 1 Date1 comes after Date2 Comparison value is: -1 Date3 comes before Date4
Date.equals() Java example
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateEquals { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MMMM-yyyy"); Date date1 = sdf.parse("16-08-2022"); Date date2 = sdf.parse("12-08-2022"); System.out.println("date1 is- " + sdf1.format(date1)); System.out.println("date2 is- " + sdf1.format(date2)); if(date1.equals(date2)) { System.out.println("Date1 is equal to Date2"); }else { System.out.println("Date1 is not equal to Date2"); } Date date3 = sdf.parse("15-08-2022"); Date date4 = sdf.parse("15-08-2022"); System.out.println("date3 is- " + sdf1.format(date3)); System.out.println("date4 is- " + sdf1.format(date4)); if(date3.equals(date4)) { System.out.println("Date3 is equal to Date4"); }else { System.out.println("Date3 is not equal to Date4"); } } }
Output
date1 is- 16-August-2022 date2 is- 12-August-2022 Date1 is not equal to Date2 date3 is- 15-August-2022 date4 is- 15-August-2022 Date3 is equal to Date4
Date.after() and Date.before() Java example
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateAfterBefore { public static void main(String[] args) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MMMM-yyyy"); Date date1 = sdf.parse("16-08-2022"); Date date2 = sdf.parse("12-08-2022"); System.out.println("date1 is- " + sdf1.format(date1)); System.out.println("date2 is- " + sdf1.format(date2)); if(date1.after(date2)) { System.out.println("Date1 is after Date2"); } if(date1.before(date2)) { System.out.println("Date1 is before Date2"); } } }
Output
date1 is- 16-August-2022 date2 is- 12-August-2022 Date1 is after Date2
Comparing java.util.Calendar
A step ahead of java.util.date but still part of the old date and time API Calendar class also has a same set of methods as Date for comparison.
- after(Object when)- Returns whether this Calendar represents a time after the time represented by the specified Object
- before(Object when)- Returns whether this Calendar represents a time before the time represented by the specified Object.
- compareTo(Calendar anotherCalendar)- Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.
- equals(Object obj)- Compares this Calendar to the specified Object.
Calendar.compareTo Java Example
compareTo() method returns one of the following three values.
- Returns 0 if the if the time represented by the argument is equal to the time represented by this Calendar
- Returns a value less than 0 if the time of this Calendar is before the time represented by the argument
- Returns a value greater than 0 if the time of this Calendar is after the time represented by the argument
import java.util.GregorianCalendar; public class CalendarCompare { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("dd-MMMM-yyyy"); GregorianCalendar date1 = new GregorianCalendar(2022, Calendar.MAY, 20, 18, 9, 22); GregorianCalendar date2 = new GregorianCalendar(2022, Calendar.MAY, 22, 18, 9, 22); System.out.println("date1 is- " + sdf.format(date1.getTime())); System.out.println("date2 is- " + sdf.format(date2.getTime())); int comp = date1.compareTo(date2); System.out.println("Comparison value is: " + comp); if(comp == 0) { System.out.println("Date1 is equal to Date2"); } else if(comp > 0) { System.out.println("Date1 comes after Date2"); }else if(comp < 0) { System.out.println("Date1 comes before Date2"); } } }
Output
date1 is- 20-May-2022 date2 is- 22-May-2022 Comparison value is: -1 Date1 comes before Date2
Calendar.equals() Java example
public class CalendarCompare { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("dd-MMMM-yyyy"); GregorianCalendar date1 = new GregorianCalendar(2022, Calendar.MAY, 20, 18, 9, 22); GregorianCalendar date2 = new GregorianCalendar(2022, Calendar.MAY, 22, 18, 9, 22); System.out.println("date1 is- " + sdf.format(date1.getTime())); System.out.println("date2 is- " + sdf.format(date2.getTime())); if(date1.equals(date2)) { System.out.println("Date1 is equal to Date2"); }else { System.out.println("Date1 is not equal to Date2"); } } }
Output
date1 is- 20-May-2022 date2 is- 22-May-2022 Date1 is not equal to Date2
Calendar.after() and Calendar.before() Java example
public class CalendarCompare { public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("dd-MMMM-yyyy"); GregorianCalendar date1 = new GregorianCalendar(2022, Calendar.MAY, 20, 18, 9, 22); GregorianCalendar date2 = new GregorianCalendar(2022, Calendar.MAY, 22, 18, 9, 22); System.out.println("date1 is- " + sdf.format(date1.getTime())); System.out.println("date2 is- " + sdf.format(date2.getTime())); if(date1.after(date2)) { System.out.println("Date1 is after Date2"); } if(date1.before(date2)) { System.out.println("Date1 is before Date2"); } } }
Output
date1 is- 20-May-2022 date2 is- 22-May-2022 Date1 is before Date2
Comparing LocalDate in Java
Java 8 added a new Date and Time API which has classes like LocalDate, LocalTime and LocalDateTime to represent date, time and date-time.
For comparing two LocalDate instances methods are-
- isEqual(ChronoLocalDate other)- Checks if this date is equal to the specified date. Returns true if this date is equal to the specified date.
- isAfter(ChronoLocalDate other)- Checks if this date is after the specified date. Returns true if this date is after the specified date.
- isBefore(ChronoLocalDate other)- Checks if this date is before the specified date. Returns true if this date is before the specified date.
- equals(Object obj)- Checks if this date is equal to another date. Returns true if this date is equal to the other date.
- compareTo(ChronoLocalDate other)- Compares this date to another date. Returns a negative value if this Date is before the Date argument, a positive value if this Date is after the Date argument, value 0 if dates are equal.
import java.time.LocalDate; import java.time.Month; public class LocalDateCompare { public static void main(String[] args) { LocalDate date1 = LocalDate.of(2022, Month.APRIL, 28); LocalDate date2 = LocalDate.of(2022, Month.APRIL, 26); System.out.println("Date1- " + date1); System.out.println("Date2- " + date2); // Using compareTo() if(date1.compareTo(date2) == 0) { System.out.println("Date1 is equal to Date2"); }else if(date1.compareTo(date2) > 0) { System.out.println("Date1 comes after Date2"); }else if(date1.compareTo(date2) < 0) { System.out.println("Date1 comes before Date2"); } // Using equals if(date1.equals(date2)) { System.out.println("Date1 is equal to Date2"); }else { System.out.println("Date1 is not equal to Date2"); } // Using isAfter() if(date1.isAfter(date2)) { System.out.println("Date1 comes after Date2"); } // Using isBefore() if(date1.isBefore(date2)) { System.out.println("Date1 comes before Date2"); } // Using isEqual() if(date1.isEqual(date2)) { System.out.println("Date1 is equal to Date2"); } } }
Output
Date1- 2022-04-28 Date2- 2022-04-28 Date1 is equal to Date2 Date1 is equal to Date2 Date1 is equal to Date2
That's all for this topic Compare Dates in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Programs Page
Related Topics
You may also like-
No comments:
Post a Comment