The Date class represents a specific instant in time, with millisecond precision. The Date class of java.util package implements Serializable, Cloneable and Comparable interface. It provides constructors and methods to deal with date and time with java.
The following are the methods to compare dates in java
Method – 1: Using Date.compareTo():
Steps involved:
- Create an object for SimpleDateFormat class initialising it with the format yyyy-mm-dd.
- Initialize the date variables using the above objects.
- Use compareTo() function of the date class for the comparisons of dates
- Print the result
Java
import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; class GFG { public static void main (String[] args) throws ParseException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-mm-dd" ); Date date1 = simpleDateFormat.parse( "2022-12-06" ); Date date2 = simpleDateFormat.parse( "2022-12-06" ); System.out.println(date2.compareTo(date1)); } } |
0
Method – 2: Using Date.before(), Date.after() and Date.equals().
This method is simpler than the first one.
Steps involved:
- Create an object for SimpleDateFormat class initialising it with the format yyyy-mm-dd.
- Initialize the date variables using the above objects.
- Use after() and before functions of the date class for the comparisons of dates
- Print the result.
Java
import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; class GFG { public static void main (String[] args) throws ParseException { SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-mm-dd" ); Date date1 = simpleDateFormat.parse( "2022-12-06" ); Date date2 = simpleDateFormat.parse( "2022-12-05" ); System.out.println(date1.before(date2)); System.out.println(date1.equals(date2)); System.out.println(date1.after(date2)); } } |
false false true
Method – 3: Using Calendar.before(), Calendar.after() and Calendar.equals().
Steps involved:
- Create an object for SimpleDateFormat class initialising it with the format yyyy-mm-dd.
- Initialize the date variables using the above objects.
- Initialize the Calendar class objects using the getinstance() functions.
- Using the setTime() function of calendar class assign the values to the calendar objects.
- Use after() and before functions of the Calendar class for the comparisons of dates
- Print the result.
Java
import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; class GFG { public static void main (String[] args) throws ParseException{ SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-mm-dd" ); Date date1 = simpleDateFormat.parse( "2022-12-06" ); Date date2 = simpleDateFormat.parse( "2022-12-05" ); Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); calendar1.setTime(date1); calendar2.setTime(date2); System.out.println(calendar1.before(calendar2)); System.out.println(calendar1.equals(calendar2)); System.out.println(calendar1.after(calendar2)); } } |
false false true
Method – 4 : Using Java 8 isBefore(), isAfter(), isEqual() and compareTo() methods: In Java 8, the isBefore(), isAfter(), isEqual() and compareTo() are used to compare LocalDate, LocalTime and LocalDateTime.
Steps involved:
- Create objects of LocalDate class.
- Use the isAfter(), isBefore() and isEqual() functions of date class to compare the dates.
- Print the result.
Java
import java.io.*; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; class GFG { public static void main (String[] args) throws ParseException { LocalDate date1 = LocalDate.now(); LocalDate date2 = date1.minusDays( 1 ); System.out.println(date1.isBefore(date2)); System.out.println(date1.isEqual(date2)); System.out.println(date1.isAfter(date2)); } } |
false false true