Signup/Sign In

Java LocalDate isAfter() Method With Examples

The isAfter() method is used to check if this date is after the specified date. We can use it to check whether a date is older to the specified date or not.

This method belongs to LocalDate class which is located into java.time package. Syntax of the method is given below.

Syntax

public boolean isAfter(ChronoLocalDate other)

Parameters:

It takes a single parameter of ChronoLocalDate type.

Returns:

It returns a boolean value either true or false.

Example

Lets take an example to check if a date is older than the other specified date. Here we have two dates and checking for both using isAfter() mehtod.

import java.time.LocalDate;

public class Demo {  
	public static void main(String[] args){  
		
		// Take a date
		LocalDate localDate1 = LocalDate.of(2018, 2, 20);
		// Take another date
		LocalDate localDate2 = LocalDate.of(2018, 8, 10);
		// Displaying date
		System.out.println("is "+localDate1+" older than "+localDate2+": "+localDate1.isAfter(localDate2));
		// Displaying date
		System.out.println("is "+localDate2+" older than "+localDate1+": "+localDate2.isAfter(localDate1));
	}
}  


is 2018-02-20 older than 2018-08-10: false
is 2018-08-10 older than 2018-02-20: true

Example:

Take a scenario where two dates are same then using isAfter() method will return always false.

import java.time.LocalDate;

public class Demo {  
	public static void main(String[] args){  
		
		// Take a date
		LocalDate localDate1 = LocalDate.of(2018, 2, 20);
		// Take another date
		LocalDate localDate2 = LocalDate.of(2018, 2, 20);
		// Displaying date
		System.out.println("is "+localDate1+" older than "+localDate2+": "+localDate1.isAfter(localDate2));
		// Displaying date
		System.out.println("is "+localDate2+" older than "+localDate1+": "+localDate2.isAfter(localDate1));
	}
}  


is 2018-02-20 older than 2018-02-20: false
is 2018-02-20 older than 2018-02-20: false

Live Example:

Try with a live example, execute the code instantly with our power 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.