Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaCompare Dates in Java

Compare Dates in Java

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: 

  1. Create an object for SimpleDateFormat class initialising it with the format yyyy-mm-dd.
  2. Initialize the date variables using the above objects.
  3. Use compareTo() function of the date class for the comparisons of dates
  4. 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));
    }
}


Output

0

Method – 2: Using Date.before(), Date.after() and Date.equals().

This method is simpler than the first one.
        Steps involved: 

  1. Create an object for SimpleDateFormat class initialising it with the format yyyy-mm-dd.
  2. Initialize the date variables using the above objects.
  3. Use after() and before functions of the date class for the comparisons of dates
  4. 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));
    }
}


Output

false
false
true

Method – 3: Using Calendar.before(), Calendar.after() and Calendar.equals().

        Steps involved: 

  1. Create an object for SimpleDateFormat class initialising it with the format yyyy-mm-dd.
  2. Initialize the date variables using the above objects.
  3. Initialize the Calendar class objects using the getinstance() functions.
  4. Using the setTime() function of calendar class assign the values to the calendar objects.
  5. Use after() and before functions of the Calendar class for the comparisons of dates
  6. 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));
    }
}


Output

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: 

  1. Create objects of LocalDate class.
  2. Use the isAfter(), isBefore() and isEqual() functions of date class to compare the dates.
  3. 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));
    }
}


Output

false
false
true

 

RELATED ARTICLES

Most Popular

Recent Comments