Pages

Sunday, December 6, 2020

Java LocalDate compareTo() Example

1. Overview

In this tutorial, We'll learn how to use compareTo() method of LocalDate class in java 8.

compareTo() method is part of new date time api. This method is used to check if the date1 is before date2 or date1 is after date 2 or date1 and date 2 are equals.

Syntax:
public int compareTo(ChronoLocalDate other)

This method takes any object that implements ChronoLocalDate interface. ChronoLocalDate interface implementations are HijrahDate, JapaneseDate, LocalDate, MinguoDate, ThaiBuddhistDate.

This method returns 0 if both the dates are equal.
This method returns positive value if “this date” is greater than the otherDate.
This method returns negative value if “this date” is less than the otherDate.

Java LocalDate compareTo() Example



2. Java 8 LocalDate.compareTo() Example


Example program on to understand the compareTo() method of LocalDate class.

package com.javaprogramto.java8.dates.localdate;

import java.time.LocalDate;

/**
 * Java 8 LocalDate.compareTo() method examples
 * 
 * @author javaprograto.com
 *
 */

public class LocalDateCompareToExample {

	public static void main(String[] args) {

		// Creating two LocalDate date objects using now() method
		LocalDate localDate1 = LocalDate.now();
		LocalDate localDate2 = LocalDate.now();

		// printing localDate1 and localDate2
		System.out.println("localDate1 : " + localDate1);
		System.out.println("localDate2 : " + localDate2);

		// calling compareTo() method on two local dates
		int compareToResult = localDate1.compareTo(localDate2);

		// LocalDate equals example
		if (compareToResult == 0) {
			System.out.println("localDate1 and localDate2 are same");
		} else if (compareToResult == 1) {
			System.out.println("localDate1 is after localDate2 ");
		} else {
			System.out.println("localDate1 is before localDate2 ");
		}

		// Creating another two LocalDate date objects using of() method with different
		// dates
		LocalDate localDate3 = LocalDate.of(2025, 01, 01);
		LocalDate localDate4 = LocalDate.of(2030, 01, 01);

		// printing localDate3 and localDate4
		System.out.println("\nlocalDate3 : " + localDate3);
		System.out.println("localDate4 : " + localDate4);

		// calling compareTo() method on two local dates
		compareToResult = localDate3.compareTo(localDate4);

		// LocalDate equals example
		if (compareToResult == 0) {
			System.out.println("localDate3 and localDate4 are same");
		} else if (compareToResult == 1) {
			System.out.println("localDate3 is after localDate4 ");
		} else {
			System.out.println("localDate3 is before localDate4 ");
		}
	}
}
Output:
localDate1 : 2020-12-06
localDate2 : 2020-12-06
localDate1 and localDate2 are same

localDate3 : 2025-01-01
localDate4 : 2030-01-01
localDate3 is before localDate4 

3. compareTo() method with different Date Time Type Objects


In the previous example compared two LocalDate objects with same and different values.

Now, compare LocalDate with MinguoDate objects using compareTo() objects.

package com.javaprogramto.java8.dates.localdate;

import java.time.LocalDate;
import java.time.chrono.MinguoDate;

/**
 * Java 8 LocalDate.compareTo() method examples with MinguoDate
 * 
 * @author javaprograto.com
 *
 */

public class LocalDateCompareToWithMinguoDate {

	public static void main(String[] args) {

		// Creating LocalDate date objects using now() method
		LocalDate localDate = LocalDate.now();
		
		// Creating MinguoDate date objects using now() method
		MinguoDate minguoDate = MinguoDate.now();

		// printing localDate and minguoDate
		System.out.println("localDate : " + localDate);
		System.out.println("minguoDate : " + minguoDate);

		// calling compareTo() method on two local dates
		int compareToResult = localDate.compareTo(minguoDate);

		// Printing output of compareTo() method
		System.out.println("compareToResult : "+compareToResult);
		
		// LocalDate equals example
		if (compareToResult == 0) {
			System.out.println("localDate and minguoDate are same");
		} else if (compareToResult > 0) {
			System.out.println("localDate is after minguoDate ");
		} else {
			System.out.println("localDate is before minguoDate ");
		}
	}
}
Output:
localDate : 2020-12-06
minguoDate : Minguo ROC 109-12-06
compareToResult : -4
localDate is before minguoDate 

From the above example, we have passed the MinguoDate object to the compareTo() method and returned negative value. localdate value is before minguo date.

LocalDate.compareTo() method works with the different type of date objects and sub implementation of ChronoLocalDate interface only example with MinguoDate.


4. Conclusion


In this article, we've seen the usage of compareTo() method of LocalDate class with passing the arguments LocalDate and MinguoDate.


No comments:

Post a Comment

Please do not add any spam links in the comments section.