LocalTime isAfter() method in Java


It can be checked if a particular LocalTime is after the other LocalTime in a timeline using the isAfter() method in the LocalTime class in Java. This method requires a single parameter i.e. the LocalTime object that is to be compared. It returns true if the LocalTime object is after the other LocalTime 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) {
      LocalTime lt1 = LocalTime.parse("11:37:12");
      LocalTime lt2 = LocalTime.parse("23:15:30");
      System.out.println("The LocalTime lt1 is: " + lt1);
      System.out.println("The LocalTime lt2 is: " + lt2);
      boolean flag = lt1.isAfter(lt2);
      if(flag)
         System.out.println("
LocalTime object lt1 is after LocalTime object lt2");       else          System.out.println("
LocalTime object lt1 is before LocalTime object lt2");    } }

output

The LocalTime lt1 is: 11:37:12
The LocalTime lt2 is: 23:15:30
LocalTime object lt1 is before LocalTime object lt2

Now let us understand the above program.

The two LocalTime objects lt1 and lt2 are displayed. It is checked if the LocalTime object lt1 is after the LocalTime object lt2 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:

LocalTime lt1 = LocalTime.parse("11:37:12");
LocalTime lt2 = LocalTime.parse("23:15:30");
System.out.println("The LocalTime lt1 is: " + lt1);
System.out.println("The LocalTime lt2 is: " + lt2);
boolean flag = lt1.isAfter(lt2);
if(flag)
   System.out.println("
LocalTime object lt1 is after LocalTime object lt2"); else    System.out.println("
LocalTime object lt1 is before LocalTime object lt2");

Updated on: 30-Jul-2019

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements