Home core java How to compare dates in Java 8

How to compare dates in Java 8

In this article you will see several examples on how to compare dates in Java 8 and prior to Java 8. A new date time api added in Java 8 in java.time package.

1. Compare dates without time

LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day. LocalDate has following methods to compare Dates in java.

  1. boolean isBefore(ChronoLocalDate other) – Checks if this date is before the specified date.
  2. boolean isAfter(ChronoLocalDate other) – Checks if this date is after the specified date.
  3. boolean isEqual(ChronoLocalDate other) – Checks if this date is equal to the specified date.
  4. int compareTo(ChronoLocalDate other) – Compares this date to another date.

1.1. An Example to LocalDate.isBefore(), LocalDate.isAfter() , LocalDate.isEqual()

public class Java8CompareDatesDemo {

	public static void main(String[] args) throws ParseException {

		LocalDate date1 = LocalDate.parse("2020-03-20");
		LocalDate date2 = LocalDate.of(2020, 03, 21);
		LocalDate date3 = LocalDate.parse("2020-03-21");

		System.out.println("date1 : " + date1);
		System.out.println("date2 : " + date2);
		System.out.println("date3 : " + date3 + "\n");

		System.out.println("Q1: is " + date1 + " older than " + date2 + "?");
		System.out.println("A1 " + date1.isBefore(date2));

		System.out.println("Q2: is " + date2 + " newer than " + date1 + "?");
		System.out.println("A2 " + date2.isAfter(date1));

		System.out.println("Q3: is " + date1 + " equals to " + date2 + "?");
		System.out.println("A3 " + date1.isEqual(date2));

		System.out.println("Q4: is " + date2 + " equals to " + date3 + "?");
		System.out.println("A4 " + date2.isEqual(date3));

	}
}

Output :

- Advertisement -
date1 : 2020-03-20
date2 : 2020-03-21
date3 : 2020-03-21

Q1: is 2020-03-20 older than 2020-03-21?
A1 true
Q2: is 2020-03-21 newer than 2020-03-20?
A2 true
Q3: is 2020-03-20 equals to 2020-03-21?
A3 false
Q4: is 2020-03-21 equals to 2020-03-21?
A4 true

1.2. An Example to LocalDate.compareTo()

date1.compareTo(date2) returns positive value if date1 comes after date2, returns negative value if date1 comes before date2, returns zero (‘0’) if date1 and date2 are same.

public class Java8CompareDatesDemo2 {

	public static void main(String[] args) throws ParseException {

		LocalDate date1 = LocalDate.of(2020, 03, 20);
		LocalDate date2 = LocalDate.of(2020, 03, 21);
		LocalDate date3 = LocalDate.of(2020, 03, 21);

		System.out.println("date1 : " + date1);
		System.out.println("date2 : " + date2);
		System.out.println("date3 : " + date3 + "\n");

		System.out.println("Q1: is " + date1 + " older than " + date2 + "?");
		System.out.println("A1 " + (date1.compareTo(date2) < 0));

		System.out.println("Q2: is " + date2 + " newer than " + date1 + "?");
		System.out.println("A2 " + (date2.compareTo(date1) > 0));

		System.out.println("Q3: is " + date1 + " equals to " + date2 + "?");
		System.out.println("A3 " + (date1.compareTo(date2) == 0));

		System.out.println("Q4: is " + date2 + " equals to " + date3 + "?");
		System.out.println("A4 " + (date2.compareTo(date3) == 0));
	}
}

Output :

date1 : 2020-03-20
date2 : 2020-03-21
date3 : 2020-03-21

Q1: is 2020-03-20 older than 2020-03-21?
A1 true
Q2: is 2020-03-21 newer than 2020-03-20?
A2 true
Q3: is 2020-03-20 equals to 2020-03-21?
A3 false
Q4: is 2020-03-21 equals to 2020-03-21?
A4 true

2. Compare dates with time

LocalDateTime LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. LocalDateTime also provide same methods like LocalDate to compare dates. Following example demonstrates how to compare dates with time in Java 8.

2.1. An Example to LocalDateTime.isBefore(), LocalDateTime.isAfter() , LocalDateTime.isEqual()

public class Java8CompareDatesWithTimeDemo {

	public static void main(String[] args) throws ParseException {
		
		LocalDateTime date1 = LocalDateTime.parse("2020-03-20T17:37:34");
		LocalDateTime date2 = LocalDateTime.of(2020, 03, 21, 17, 37, 34);
		LocalDateTime date3 = LocalDateTime.parse("2020-03-21T17:37:34");
		
		System.out.println("date1 : "+date1);
		System.out.println("date2 : "+date2);
		System.out.println("date3 : "+date3+"\n");
		
		System.out.println("Q1: is "+date1 +" older than "+date2+"?");
		System.out.println("A1 "+ date1.isBefore(date2));
		
		System.out.println("Q2: is "+date2 +" newer than "+date1+"?");
		System.out.println("A2 "+ date2.isAfter(date1));
		
		System.out.println("Q3: is "+date1 +" equals to "+date2+"?");
		System.out.println("A3 "+ date1.isEqual(date2));
		
		System.out.println("Q4: is "+date2 +" equals to "+date3+"?");
		System.out.println("A4 "+ date2.isEqual(date3));
	}
}

Output :

date1 : 2020-03-20T17:37:34
date2 : 2020-03-21T17:37:34
date3 : 2020-03-21T17:37:34

Q1: is 2020-03-20T17:37:34 older than 2020-03-21T17:37:34?
A1 true
Q2: is 2020-03-21T17:37:34 newer than 2020-03-20T17:37:34?
A2 true
Q3: is 2020-03-20T17:37:34 equals to 2020-03-21T17:37:34?
A3 false
Q4: is 2020-03-21T17:37:34 equals to 2020-03-21T17:37:34?
A4 true

2.2. An Example to LocalDateTime.compareTo()

dateTime1.compareTo(dateTime2) returns positive value if dateTime1 comes after dateTime2, returns negative value if dateTime1 comes before dateTime2, returns zero (‘0’) if dateTime1 and dateTime2 are same.

public class Java8CompareDatesWithTimeDemo2 {

	public static void main(String[] args) throws ParseException {
		
		LocalDateTime date1 = LocalDateTime.of(2020, 03, 20, 17, 37, 34);
		LocalDateTime date2 = LocalDateTime.of(2020, 03, 21, 17, 37, 34);
		LocalDateTime date3 = LocalDateTime.of(2020, 03, 21, 17, 37, 34);
		
		System.out.println("date1 : "+date1);
		System.out.println("date2 : "+date2);
		System.out.println("date3 : "+date3+"\n");
		
		System.out.println("Q1: is " + date1 + " older than " + date2 + "?");
		System.out.println("A1 " + (date1.compareTo(date2) < 0));

		System.out.println("Q2: is " + date2 + " newer than " + date1 + "?");
		System.out.println("A2 " + (date2.compareTo(date1) > 0));

		System.out.println("Q3: is " + date1 + " equals to " + date2 + "?");
		System.out.println("A3 " + (date1.compareTo(date2) == 0));

		System.out.println("Q4: is " + date2 + " equals to " + date3 + "?");
		System.out.println("A4 " + (date2.compareTo(date3) == 0));
	}
}

Output :

date1 : 2020-03-20T17:37:34
date2 : 2020-03-21T17:37:34
date3 : 2020-03-21T17:37:34

Q1: is 2020-03-20T17:37:34 older than 2020-03-21T17:37:34?
A1 true
Q2: is 2020-03-21T17:37:34 newer than 2020-03-20T17:37:34?
A2 true
Q3: is 2020-03-20T17:37:34 equals to 2020-03-21T17:37:34?
A3 false
Q4: is 2020-03-21T17:37:34 equals to 2020-03-21T17:37:34?
A4 true

3. Compare Zoned date time

ZonedDateTime is A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris, implements ChronoZonedDateTime which provides isBefore, isAfter, isEquals and compareTo methods like LocalDate and LocalDateTime classes to compare zoned date time. Let’s have a look into following example.

3.1. An Example to ZonedDateTime.isBefore(), ZonedDateTime.isAfter() , ZonedDateTime.isEqual()

public class Java8CompareZonedDateTimeDemo {

	public static void main(String[] args) throws ParseException {

		LocalDateTime date1 = LocalDateTime.parse("2020-03-20T17:37:34.000000500");
		LocalDateTime date2 = LocalDateTime.of(2020, 03, 21, 17, 37, 34, 500);
		
		ZonedDateTime zdt1 = ZonedDateTime.of(date1, ZoneId.of("America/Phoenix"));
		ZonedDateTime zdt2 = ZonedDateTime.of(date2, ZoneId.of("America/Phoenix"));
		ZonedDateTime zdt3 = ZonedDateTime.of(2020, 03, 21, 17, 37, 34, 500, ZoneId.of("America/Phoenix"));
		
	
		System.out.println("zoned date1 : " + zdt1);
		System.out.println("zoned date2 : " + zdt2);
		System.out.println("zoned date3 : " + zdt3 + "\n");

		System.out.println("Q1: is "+zdt1 +" older than "+zdt2+"?");
		System.out.println("A1 "+ zdt1.isBefore(zdt2));
		
		System.out.println("Q2: is "+zdt2 +" newer than "+zdt1+"?");
		System.out.println("A2 "+ zdt2.isAfter(zdt1));
		
		System.out.println("Q3: is "+zdt1 +" equals to "+zdt2+"?");
		System.out.println("A3 "+ zdt1.isEqual(zdt2));
		
		System.out.println("Q4: is "+zdt2 +" equals to "+zdt3+"?");
		System.out.println("A4 "+ zdt2.isEqual(zdt3));

	}
}

Output :

zoned date1 : 2020-03-20T17:37:34.000000500-07:00[America/Phoenix]
zoned date2 : 2020-03-21T17:37:34.000000500-07:00[America/Phoenix]
zoned date3 : 2020-03-21T17:37:34.000000500-07:00[America/Phoenix]

Q1: is 2020-03-20T17:37:34.000000500-07:00[America/Phoenix] older than 2020-03-21T17:37:34.000000500-07:00[America/Phoenix]?
A1 true
Q2: is 2020-03-21T17:37:34.000000500-07:00[America/Phoenix] newer than 2020-03-20T17:37:34.000000500-07:00[America/Phoenix]?
A2 true
Q3: is 2020-03-20T17:37:34.000000500-07:00[America/Phoenix] equals to 2020-03-21T17:37:34.000000500-07:00[America/Phoenix]?
A3 false
Q4: is 2020-03-21T17:37:34.000000500-07:00[America/Phoenix] equals to 2020-03-21T17:37:34.000000500-07:00[America/Phoenix]?
A4 true

3.2. An Example to ZonedDateTime.compareTo()

zonedDateTime1.compareTo(zonedDateTime2) returns positive value if zonedDateTime1 comes after zonedDateTime2, returns negative value if zonedDateTime1 comes before zonedDateTime2, returns zero (‘0’) if zonedDateTime1 and zonedDdateTime2 are same.

public class Java8CompareZonedDateTimeDemo2 {

	public static void main(String[] args) throws ParseException {

		ZonedDateTime zdt1 = ZonedDateTime.of(2020, 03, 20, 17, 37, 34, 500, ZoneId.of("America/Phoenix"));
		ZonedDateTime zdt2 = ZonedDateTime.of(2020, 03, 21, 17, 37, 34, 500, ZoneId.of("America/Phoenix"));
		ZonedDateTime zdt3 = ZonedDateTime.of(2020, 03, 21, 17, 37, 34, 500, ZoneId.of("America/Phoenix"));
		
	
		System.out.println("zoned date1 : " + zdt1);
		System.out.println("zoned date2 : " + zdt2);
		System.out.println("zoned date3 : " + zdt3 + "\n");

		System.out.println("Q1: is " + zdt1 + " older than " + zdt2 + "?");
		System.out.println("A1 " + (zdt1.compareTo(zdt2) < 0));

		System.out.println("Q2: is " + zdt2 + " newer than " + zdt1 + "?");
		System.out.println("A2 " + (zdt2.compareTo(zdt1) > 0));

		System.out.println("Q3: is " + zdt1 + " equals to " + zdt2 + "?");
		System.out.println("A3 " + (zdt1.compareTo(zdt2) == 0));

		System.out.println("Q4: is " + zdt2 + " equals to " + zdt3 + "?");
		System.out.println("A4 " + (zdt2.compareTo(zdt3) == 0));

	}
}

Output :

zoned date1 : 2020-03-20T17:37:34.000000500-07:00[America/Phoenix]
zoned date2 : 2020-03-21T17:37:34.000000500-07:00[America/Phoenix]
zoned date3 : 2020-03-21T17:37:34.000000500-07:00[America/Phoenix]

Q1: is 2020-03-20T17:37:34.000000500-07:00[America/Phoenix] older than 2020-03-21T17:37:34.000000500-07:00[America/Phoenix]?
A1 true
Q2: is 2020-03-21T17:37:34.000000500-07:00[America/Phoenix] newer than 2020-03-20T17:37:34.000000500-07:00[America/Phoenix]?
A2 true
Q3: is 2020-03-20T17:37:34.000000500-07:00[America/Phoenix] equals to 2020-03-21T17:37:34.000000500-07:00[America/Phoenix]?
A3 false
Q4: is 2020-03-21T17:37:34.000000500-07:00[America/Phoenix] equals to 2020-03-21T17:37:34.000000500-07:00[America/Phoenix]?
A4 true

4. Conclusion

In this guide we have covered how to compare dates in Java 8, compare dates with time and with out time and comparing zoned date time. If you would like to compare OffsetDateTime, it also provides isAfter(), isBefore(), isEqual() and compareTo() methods to compare offset date time.

- Advertisement -
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles