Date and Time

How to Compare Two String Dates in Java

Table of Contents

Program

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CompareDate {

	public static void main(String[] args) throws ParseException {
		// TODO Auto-generated method stub
		SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");
	      Date d1 = sdformat.parse("2019-04-15");
	      Date d2 = sdformat.parse("2019-08-10");
	      System.out.println("The date 1 is: " + sdformat.format(d1));
	      System.out.println("The date 2 is: " + sdformat.format(d2));
	      if(d1.compareTo(d2) > 0) {
	         System.out.println("Date 1 occurs after Date 2");
	      } else if(d1.compareTo(d2) < 0) {
	         System.out.println("Date 1 occurs before Date 2");
	      } else if(d1.compareTo(d2) == 0) {
	         System.out.println("Both dates are equal");
	      }
	}
}

Output

	The date 1 is: 2019-04-15
	The date 2 is: 2019-08-10
	Date 1 occurs before Date 2

Description

public Date parse​(String text, ParsePosition pos)

Parses text from a string to produce a Date.
The method attempts to parse text starting at the index given by pos. If parsing succeeds, then the index of pos is updated to the index after the last character used (parsing does not necessarily use all characters up to the end of the string), and the parsed date is returned. The updated pos can be used to indicate the starting point for the next call to this method. If an error occurs, then the index of pos is not changed, the error index of pos is set to the index of the character where the error occurred, and null is returned.
This parsing operation uses the calendar to produce a Date. All of the calendar’s date-time fields are cleared before parsing, and the calendar’s default values of the date-time fields are used for any missing date-time information. For example, the year value of the parsed Date is 1970 with GregorianCalendar if no year value is given from the parsing operation. The TimeZone value may be overwritten, depending on the given pattern and the time zone value in text. Any TimeZone value that has previously been set by a call to setTimeZone may need to be restored for further operations

Specified by:

parse in class DateFormat

Parameters:

text – A String, part of which should be parsed.
pos – A ParsePosition object with index and error index information as described above.

Returns:

A Date parsed from the string. In case of error, returns null.

Throws:

NullPointerException – if text or pos is null.

public int compareTo​(Date anotherDate)

Compares two Dates for ordering.

Specified by:

compareTo in interface Comparable<Date>

Parameters:

anotherDate – the Date to be compared.

Returns:

he value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.

Throws:

NullPointerException – if anotherDate is null.

Since:

1.2