Java — How to compare Date and Time | Code Factory

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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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());
}
}
}
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

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Knightly Knews Episode 12

KMM — Publish Android / JVM SDK Binaries

Summer Is Here — Here’s a List of Fun and Challenging Coding Ideas to Keep You Busy

Redis cluster on Kubernetes

Visualizing, Designing, and Analyzing the Merge Sort Algorithm.

CSS-Modules

/e/OS and the Art of Remote Project Management

The Code That Controls Your Money

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Return nothing, with Java Optionals

Java virtual machine(JVM) Architecture (JVM Main Components)

Junit 5 @Nested

Java 16 Records