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
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));
}
}
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));
}
}
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));
}
}
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));
}
}
Similar Reads
Java Integer compareTo() method The compareTo() method of Integer class of java.lang package compares two Integer objects numerically and returns the value 0 if this Integer is equal to the argument Integer; a value less than 0 if this Integer is numerically less than the argument Integer; and a value greater than 0 if this Intege
2 min read
Compare Two Arrays in Java In Java, comparing two arrays can be confusing, because the "==" operator only checks if the two arrays point to the same memory location. To compare the contents of arrays, we need to use other methods like Arrays.equals() or Arrays.deepEquals(). Example:Let us see in the below example, how to comp
5 min read
Compare two Strings in Java String in Java are immutable sequences of characters. Comparing strings is the most common task in different scenarios such as input validation or searching algorithms. In this article, we will learn multiple ways to compare two strings in Java with simple examples.Example:To compare two strings in
4 min read
Java Integer compare() method The compare() method of Integer class of java.lang package compares two integer values (x, y) given as a parameter and returns the value zero if (x==y), if (x < y) then it returns a value less than zero and if (x > y) then it returns a value greater than zero. Syntax : public static int compar
2 min read
Double compare() Method in Java with Examples The compare() method of Double Class is a built-in method in Java that compares the two specified double values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Syntax: public static int compare(double d1, double d2) Parameters:
2 min read
Double.compareTo() Method in Java with Examples The Double.compareTo() method is a built-in method in Java of java.lang package. This method is used to compare two Double objects numerically. This method returns 0 if this object is equal to the argument object, it returns less than 0 if this object is numerically less than the argument object, an
3 min read