Javaexercise.com

Java LocalDateTime compareTo() Method With Examples

Java comapreTo() method is used to compare a date-time to another date-time. It returns primitive int value as per the following:

  • It returns a negative integer if the date-time is less than other date-time

  • It returns a positive integer if the date-time is greater than other date-time

  • It returns zero if both date-time are equal.

The compareTo() method belongs to LocalDateTime class which is stored in java.time package.

This method takes a single argument of ChronoLocalDateTime type and returns an integer value. The syntax of compareTo() method is the following:

Syntax

public int compareTo(ChronoLocalDateTime<?> other)

 

Parameters

other - the other date-time to compare to.

Return Value

It returns an int value.

 

Example: How to compare LocalDateTime Objects

Here, we are comparing two date-time objects to check whether they are equal to each other or not. This example returns -3 as a difference between these two date-time objects.

import java.time.LocalDateTime;

/* 
 * Example to compare two date-time objects.
 */
public class JExercise {
	public static void main(String[] args) {
		
		// Create a LocalDateTime date
		LocalDateTime datetime = LocalDateTime.parse("2012-05-02T02:30:10");
		// Print Date and time
		System.out.println("Date and Time : "+datetime);
		// Create another LocalDateTime date
		LocalDateTime datetime2 = LocalDateTime.parse("2012-05-05T02:30:10");
		// Print Date and time2
		System.out.println("Date and Time2 : "+datetime2);
		// Comparing date-time
		int val = datetime.compareTo(datetime2);
		// Display difference 
		System.out.println("Zoned Date and Time : "+val);
	}
}

Output:

Date and Time : 2012-05-02T02:30:10
Date and Time2 : 2012-05-05T02:30:10
Zoned Date and Time : -3
 

Example: Comparing LocalDateTime

Here we are comparing greater date-time to lesser date-time by using compareTo() method that returns a positive integer. This example returns 3 as a difference of both date-time. See the below example.

import java.time.LocalDateTime;

/* 
 * Example to compare two date-time objects.
 */
public class JExercise {
	public static void main(String[] args) {
		
		// Create a LocalDateTime date
		LocalDateTime datetime = LocalDateTime.parse("2012-05-05T02:30:10");
		// Print Date and time
		System.out.println("Date and Time : "+datetime);
		// Create another LocalDateTime date
		LocalDateTime datetime2 = LocalDateTime.parse("2012-05-02T02:30:10");
		// Print Date and time2
		System.out.println("Date and Time2 : "+datetime2);
		// Comparing date-time
		int val = datetime.compareTo(datetime2);
		// Display difference 
		System.out.println("Zoned Date and Time : "+val);
		
	}
}

Output:

Date and Time : 2012-05-05T02:30:10
Date and Time2 : 2012-05-02T02:30:10
Zoned Date and Time : 3

 

 


Conclusion

Well, in this topic, we learnt to compare two localdatetime objects. It takes a single argument and returns an integer value that represents a difference of two dates.

If we missed something, you can suggest us at - info.javaexercise@gmail.com