LocalDate isAfter() method in Java

Java 8Object Oriented ProgrammingProgramming

It can be checked if a particular LocalDate is after the other LocalDate in a timeline using the isAfter() method in the LocalDate class in Java. This method requires a single parameter i.e. the LocalDate object that is to be compared. It returns true if the LocalDate object is after the other LocalDate object and false otherwise.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.*;
public class Main {
   public static void main(String[] args) {
      LocalDate ld1 = LocalDate.parse("2019-02-20");
      LocalDate ld2 = LocalDate.parse("2019-02-14");
      System.out.println("The LocalDate ld1 is: " + ld1);
      System.out.println("The LocalDate ld2 is: " + ld2);
      boolean flag = ld1.isAfter(ld2);
      if(flag)
         System.out.println("\nLocalDate object ld1 is after LocalDate object ld2");
      else
         System.out.println("\nLocalDate object ld1 is before LocalDate object ld2");
   }
}

Output

The LocalDate ld1 is: 2019-02-20
The LocalDate ld2 is: 2019-02-14
LocalDate object ld1 is after LocalDate object ld2

Now let us understand the above program.

The two LocalDate objects ld1 and ld2 are displayed. It is checked if the LocalDate object ld1 is after the LocalDate object ld2 in the timeline using the isAfter() method. The returned value is displayed using an if statement. A code snippet that demonstrates this is as follows −

LocalDate ld1 = LocalDate.parse("2019-02-20");
LocalDate ld2 = LocalDate.parse("2019-02-14");
System.out.println("The LocalDate ld1 is: " + ld1);
System.out.println("The LocalDate ld2 is: " + ld2);
boolean flag = ld1.isAfter(ld2);
if(flag)
   System.out.println("\nLocalDate object ld1 is after LocalDate object ld2");
else
   System.out.println("\nLocalDate object ld1 is before LocalDate object ld2");
raja
Published on 27-Mar-2019 04:56:04
Advertisements