Signup/Sign In

Java LocalDate isBefore() Method With Example

Java isBefore() method is used to check if this date is before the specified date. It returns true if this date is before the specified date, false otherwise. For example, if we have two dates 2016/15/20 and 2015/15/12, and use this method then the result will be false for the first date.

It takes a single argument of the ChronoLocalDate interface and returns either true or false. The syntax of the method is given below.

Syntax

public boolean isBefore(ChronoLocalDate other)

Parameters:

It takes a date as parameter to compare with another date.

Returns:

It returns true if this date is before the specified date

Time for an Example:

Let's take an example to check whether a date is before another date. Here, we use isBefore() method that returns false for the example. See the below example.

import java.time.LocalDate; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2016, 10, 21);
		System.out.println(localDate);
		// Date 2
		LocalDate localDate2 = LocalDate.of(2015, 10, 21);
		System.out.println(localDate2);
		boolean d  = localDate.isBefore(localDate2);
		System.out.println("is date1 before date2 : "+d);
	}
}


2016-10-21
2015-10-21
is date1 before date2 : false

Time for another Example:

Let's take another example to understand the isBefore() method. Here, the first date is before the second date that is why the method returns a true. See the below example.

import java.time.LocalDate; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2015, 10, 21);
		System.out.println(localDate);
		// Date 2
		LocalDate localDate2 = LocalDate.of(2016, 10, 21);
		System.out.println(localDate2);
		boolean d  = localDate.isBefore(localDate2);
		System.out.println("is date1 before date2 : "+d);
	}
}


2015-10-21
2016-10-21
is date1 before date2 : true

Live Example:

Try with a live example, execute the code instantly with our powerful Online Java Compiler.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.