Compare Joda-Time DateTime using DateTimeComparator in Java
Tags: Joda Time Joda DateTime Joda DateTimeComparator Comparator
In this Joda-Time tutorial, we learn how to use Joda-Time’s DateTimeComparator class to compare DateTime objects. With different built-in comparators we will learn how to compare DateTime objects with different specified fields such as date, time or date and time.
Add Joda Time library to your Java project
To use Joda Time Java library in the Gradle build project, add the following dependency into the build.gradle file.
compile group: 'joda-time', name: 'joda-time', version: '2.10.9'
To use Joda Time Java library in the Maven build project, add the following dependency into the pom.xml file.
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.9</version>
</dependency>
To download the Joda Time .jar file you can visit Joda Time releases page at github.com/JodaOrg/joda-time
Compare Date and Time
In the following example Java programs, we use DateTimeComparator.getInstance() static method to get the DateTimeComparator object that allows comparison of the entire date and time value.
CompareDateTimeExample1.java
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
public class CompareDateTimeExample1 {
public static void main(String[] args) {
DateTime dateTime1 = DateTime.parse("2021-01-19T08:30:00.000");
DateTime dateTime2 = DateTime.parse("2021-01-20T09:00:00.000");
DateTimeComparator dateTimeComparator = DateTimeComparator.getInstance();
int compareResult = dateTimeComparator.compare(dateTime1, dateTime2);
System.out.println("dateTime1: " + dateTime1);
System.out.println("dateTime2: " + dateTime2);
System.out.println("Compare Result: " + compareResult);
}
}
dateTime1: 2021-01-19T08:30:00.000+07:00
dateTime2: 2021-01-20T09:00:00.000+07:00
Compare Result: -1
CompareDateTimeExample2.java
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
public class CompareDateTimeExample2 {
public static void main(String[] args) {
DateTime dateTime1 = DateTime.parse("2021-01-20T09:00:00.000");
DateTime dateTime2 = DateTime.parse("2021-01-20T09:00:00.000");
DateTimeComparator dateTimeComparator = DateTimeComparator.getInstance();
int compareResult = dateTimeComparator.compare(dateTime1, dateTime2);
System.out.println("dateTime1: " + dateTime1);
System.out.println("dateTime2: " + dateTime2);
System.out.println("Compare Result: " + compareResult);
}
}
dateTime1: 2021-01-20T09:00:00.000+07:00
dateTime2: 2021-01-20T09:00:00.000+07:00
Compare Result: 0
CompareDateTimeExample3.java
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
public class CompareDateTimeExample3 {
public static void main(String[] args) {
DateTime dateTime1 = DateTime.parse("2021-01-21T09:30:00.000");
DateTime dateTime2 = DateTime.parse("2021-01-20T09:00:00.000");
DateTimeComparator dateTimeComparator = DateTimeComparator.getInstance();
int compareResult = dateTimeComparator.compare(dateTime1, dateTime2);
System.out.println("dateTime1: " + dateTime1);
System.out.println("dateTime2: " + dateTime2);
System.out.println("Compare Result: " + compareResult);
}
}
dateTime1: 2021-01-21T09:30:00.000+07:00
dateTime2: 2021-01-20T09:00:00.000+07:00
Compare Result: 1
Compare Date Only
In the following example Java programs, we use DateTimeComparator.getDateOnlyInstance() static method to get the DateTimeComparator object that allows comparison of the date value only.
CompareDateExample1.java
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
public class CompareDateExample1 {
public static void main(String[] args) {
DateTime dateTime1 = DateTime.parse("2021-01-19T09:00:00.000");
DateTime dateTime2 = DateTime.parse("2021-01-20T08:00:00.000");
DateTimeComparator dateOnlyComparator = DateTimeComparator.getDateOnlyInstance();
int compareResult = dateOnlyComparator.compare(dateTime1, dateTime2);
System.out.println("dateTime1: " + dateTime1);
System.out.println("dateTime2: " + dateTime2);
System.out.println("Compare Result: " + compareResult);
}
}
dateTime1: 2021-01-19T09:00:00.000+07:00
dateTime2: 2021-01-20T08:00:00.000+07:00
Compare Result: -1
CompareDateExample2.java
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
public class CompareDateExample2 {
public static void main(String[] args) {
DateTime dateTime1 = DateTime.parse("2021-01-20T08:00:00.000");
DateTime dateTime2 = DateTime.parse("2021-01-20T09:30:00.000");
DateTimeComparator dateOnlyComparator = DateTimeComparator.getDateOnlyInstance();
int compareResult = dateOnlyComparator.compare(dateTime1, dateTime2);
System.out.println("dateTime1: " + dateTime1);
System.out.println("dateTime2: " + dateTime2);
System.out.println("Compare Result: " + compareResult);
}
}
dateTime1: 2021-01-20T08:00:00.000+07:00
dateTime2: 2021-01-20T09:30:00.000+07:00
Compare Result: 0
CompareDateExample3.java
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
public class CompareDateExample3 {
public static void main(String[] args) {
DateTime dateTime1 = DateTime.parse("2021-01-21T08:00:00.000");
DateTime dateTime2 = DateTime.parse("2021-01-20T09:00:00.000");
DateTimeComparator dateOnlyComparator = DateTimeComparator.getDateOnlyInstance();
int compareResult = dateOnlyComparator.compare(dateTime1, dateTime2);
System.out.println("dateTime1: " + dateTime1);
System.out.println("dateTime2: " + dateTime2);
System.out.println("Compare Result: " + compareResult);
}
}
dateTime1: 2021-01-21T08:00:00.000+07:00
dateTime2: 2021-01-20T09:00:00.000+07:00
Compare Result: 1
Compare Time Only
In the following example Java programs, we use DateTimeComparator.getTimeOnlyInstance() static method to get the DateTimeComparator object that allows comparison of the time value only.
CompareTimeExample1.java
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
public class CompareTimeExample1 {
public static void main(String[] args) {
DateTime dateTime1 = DateTime.parse("2021-01-20T08:00:00.000");
DateTime dateTime2 = DateTime.parse("2021-01-19T09:00:00.000");
DateTimeComparator timeOnlyComparator = DateTimeComparator.getTimeOnlyInstance();
int compareResult = timeOnlyComparator.compare(dateTime1, dateTime2);
System.out.println("dateTime1: " + dateTime1);
System.out.println("dateTime2: " + dateTime2);
System.out.println("Compare Result: " + compareResult);
}
}
dateTime1: 2021-01-20T08:00:00.000+07:00
dateTime2: 2021-01-19T09:00:00.000+07:00
Compare Result: -1
CompareTimeExample2.java
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
public class CompareTimeExample2 {
public static void main(String[] args) {
DateTime dateTime1 = DateTime.parse("2021-01-18T09:00:00.000");
DateTime dateTime2 = DateTime.parse("2021-01-19T09:00:00.000");
DateTimeComparator timeOnlyComparator = DateTimeComparator.getTimeOnlyInstance();
int compareResult = timeOnlyComparator.compare(dateTime1, dateTime2);
System.out.println("dateTime1: " + dateTime1);
System.out.println("dateTime2: " + dateTime2);
System.out.println("Compare Result: " + compareResult);
}
}
dateTime1: 2021-01-18T09:00:00.000+07:00
dateTime2: 2021-01-19T09:00:00.000+07:00
Compare Result: 0
CompareTimeExample3.java
import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;
public class CompareTimeExample3 {
public static void main(String[] args) {
DateTime dateTime1 = DateTime.parse("2021-01-18T09:00:00.000");
DateTime dateTime2 = DateTime.parse("2021-01-19T08:00:00.000");
DateTimeComparator timeOnlyComparator = DateTimeComparator.getTimeOnlyInstance();
int compareResult = timeOnlyComparator.compare(dateTime1, dateTime2);
System.out.println("dateTime1: " + dateTime1);
System.out.println("dateTime2: " + dateTime2);
System.out.println("Compare Result: " + compareResult);
}
}
dateTime1: 2021-01-18T09:00:00.000+07:00
dateTime2: 2021-01-19T08:00:00.000+07:00
Compare Result: 1
Happy Coding 😊
Related Articles
Convert Joda-Time DateTime into Date in Java
Joda-Time Create UTC DateTime in Java
Joda Time DateTime by Examples in Java
Joda-Time Create DateTime with Time Start Of the Day in Java
Convert SQL Timestamp into Joda-Time DateTime and Vice Versa in Java
Convert Joda-Time DateTime into String and Vice Versa in Java
Convert Joda-Time DateTime into Calendar and Vice Versa in Java
Format Joda-Time DateTime String using ISODateTimeFormat in Java
Joda-Time Create DateTime with Time End of the Day in Java
Get Date and Time fields value of Joda-Time DateTime object in Java