Java — How to compare Date and Time | Code Factory
4 min readJun 30, 2020
Donate : Link
WordPress Blog : Link
We use the DateTime API introduced in Java 8 like LocalDate, LocalTime, LocalDateTime, ZonedDateTime, as well as older classes like Date, and Calendar to demonstrate how to compare dates.
Compare dates in Java using LocalDate
package com.example.java.programming.datetime;import java.time.LocalDate;/**
* @author code.factory
*
*/
public class CompareLocalDateExample {
public static void main(String... args) {
LocalDate date1 = LocalDate.now();
LocalDate date2 = LocalDate.of(2020, 6, 30); // isAfter() method
if (date1.isAfter(date2)) {
System.out.println(date1 + " is after " + date2);
} // isBefore() method
if (date1.isBefore(date2)) {
System.out.println(date1 + " is before " + date2);
} // isEqual() method
if (date1.isEqual(date2)) {
System.out.println(date1 + " is equal to " + date2);
} // compareTo() method
int diff = date1.compareTo(date2);
if (diff > 0) {
System.out.println(date1 + " is greater than " + date2);
} else if (diff < 0) {
System.out.println(date1 + " is less than " + date2);
} else {
System.out.println(date1 + " is equal to " + date2);
}
}
}
Output :
2020-06-29 is before 2020-06-30
2020-06-29 is less than 2020-06-30
Compare date and time in Java using LocalDateTime
package com.example.java.programming.datetime;import java.time.LocalDateTime;/**
* @author code.factory
*
*/
public class CompareLocalDateTimeExample {
public static void main(String... args) {
LocalDateTime date1 = LocalDateTime.now();
LocalDateTime date2 = LocalDateTime.of(2020, 6, 30, 10, 15, 45); // isAfter() method
if (date1.isAfter(date2)) {
System.out.println(date1 + " is after " + date2);
} // isBefore() method
if (date1.isBefore(date2)) {
System.out.println(date1 + " is before " + date2);
} // isEqual() method
if (date1.isEqual(date2)) {
System.out.println(date1 + " is equal to " + date2);
} // compareTo() method
int diff = date1.compareTo(date2);
if (diff > 0) {
System.out.println(date1 + " is greater than " + date2);
} else if (diff < 0) {
System.out.println(date1 + " is less than " + date2);
} else {
System.out.println(date1 + " is equal to " + date2);
}
}
}
Output :
2020-06-29T17:18:22.490 is before 2020-06-30T10:15:45
2020-06-29T17:18:22.490 is less than 2020-06-30T10:15:45
Compare date and time with different TimeZones in Java using ZonedDateTime
package com.example.java.programming.datetime;import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;/**
* @author code.factory
*
*/
public class CompareZonedDateTimeExample {
public static void main(String... args) {
ZonedDateTime date1 = ZonedDateTime.of(LocalDateTime.of(2020, 6, 29, 17, 30, 45), ZoneId.of("Asia/Kolkata"));
ZonedDateTime date2 = ZonedDateTime.of(LocalDateTime.of(2020, 6, 29, 17, 30, 45), ZoneId.of("Europe/Paris")); // isAfter() method
if (date1.isAfter(date2)) {
System.out.println(date1 + " is after " + date2);
} // isBefore() method
if (date1.isBefore(date2)) {
System.out.println(date1 + " is before " + date2);
} // isEqual() method
if (date1.isEqual(date2)) {
System.out.println(date1 + " is equal to " + date2);
} // compareTo() method
int diff = date1.compareTo(date2);
if (diff > 0) {
System.out.println(date1 + " is greater than " + date2);
} else if (diff < 0) {
System.out.println(date1 + " is less than " + date2);
} else {
System.out.println(date1 + " is equal to " + date2);
}
}
}
Output :
2020-06-29T17:30:45+05:30[Asia/Kolkata] is before 2020-06-29T17:30:45+02:00[Europe/Paris]
2020-06-29T17:30:45+05:30[Asia/Kolkata] is less than 2020-06-29T17:30:45+02:00[Europe/Paris]
Compare time in Java using LocalTime
package com.example.java.programming.datetime;import java.time.LocalTime;/**
* @author code.factory
*
*/
public class CompareLocalTimeExample {
public static void main(String... args) {
LocalTime time1 = LocalTime.of(15, 20, 40);
LocalTime time2 = LocalTime.of(10, 30, 50); // isAfter() method
if (time1.isAfter(time2)) {
System.out.println(time1 + " is after " + time2);
} // isBefore() method
if (time1.isBefore(time2)) {
System.out.println(time1 + " is before " + time2);
} // compareTo() method
int diff = time1.compareTo(time2);
if (diff > 0) {
System.out.println(time1 + " is greater than " + time2);
} else if (diff < 0) {
System.out.println(time1 + " is less than " + time2);
} else {
System.out.println(time1 + " is equal to " + time2);
}
}
}
Output :
15:20:40 is after 10:30:50
15:20:40 is greater than 10:30:50
Compare date using Date class
package com.example.java.programming.datetime;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;/**
* @author code.factory
*
*/
public class CompareDateExample {
public static void main(String... args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = sdf.parse("2020-06-29 10:30:45");
Date date2 = sdf.parse("2020-06-30 14:20:00"); // after() method
if (date1.after(date2)) {
System.out.println(date1 + " is after " + date2);
} // before() method
if (date1.before(date2)) {
System.out.println(date1 + " is before " + date2);
} // equals() method
if (date1.equals(date2)) {
System.out.println(date1 + " is equal to " + date2);
} // compareTo() method
int diff = date1.compareTo(date2);
if (diff > 0) {
System.out.println(date1 + " is greater than " + date2);
} else if (diff < 0) {
System.out.println(date1 + " is less than " + date2);
} else {
System.out.println(date1 + " is equal to " + date2);
}
}
}
Output :
Mon Jun 29 10:30:45 IST 2020 is before Tue Jun 30 14:20:00 IST 2020
Mon Jun 29 10:30:45 IST 2020 is less than Tue Jun 30 14:20:00 IST 2020
Compare date using Calendar class
package com.example.java.programming.datetime;import java.util.Calendar;/**
* @author code.factory
*
*/
public class CompareCalendarExample {
public static void main(String... args) {
Calendar cal1 = Calendar.getInstance();
cal1.set(2020, 5, 30, 10, 30, 45); Calendar cal2 = Calendar.getInstance();
cal2.set(2020, 5, 29, 14, 22, 30); // after() method
if (cal1.after(cal2)) {
System.out.println(cal1.getTime() + " is after " + cal2.getTime());
} // before() method
if (cal1.before(cal2)) {
System.out.println(cal1.getTime() + " is before " + cal2.getTime());
} // equals() method
if (cal1.equals(cal2)) {
System.out.println(cal1.getTime() + " is equal to " + cal2.getTime());
} // compareTo() method
int diff = cal1.compareTo(cal2);
if (diff > 0) {
System.out.println(cal1.getTime() + " is greater than " + cal2.getTime());
} else if (diff < 0) {
System.out.println(cal1.getTime() + " is less than " + cal2.getTime());
} else {
System.out.println(cal1.getTime() + " is equal to " + cal2.getTime());
}
}
}
Output :
Tue Jun 30 10:30:45 IST 2020 is after Mon Jun 29 14:22:30 IST 2020
Tue Jun 30 10:30:45 IST 2020 is greater than Mon Jun 29 14:22:30 IST 2020