Open In App
Related Articles

LocalTime isBefore() method in Java with Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

The isBefore() method of a LocalTime class is used to check if this LocalTime timeline position is before the LocalTime passed as parameter or not. If this LocalTime timeline position is before the LocalTime passed as a parameter then the method will return true else false. The comparison is based on the time-line position of the instants.

Syntax:

public boolean isBefore(LocalTime other)

Parameters: This method accepts a single parameter other which is the other LocalTime object to compare to. It should not be null.

Return value: This method returns true if this time is before the specified time, else false.

Below programs illustrate the isBefore() method:

Program 1:




// Java program to demonstrate
// LocalTime.isBefore() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime object
        LocalTime time1
            = LocalTime.parse("19:34:50.63");
  
        // create other LocalTime
        LocalTime time2
            = LocalTime.parse("23:14:00.63");
  
        // print instances
        System.out.println("LocalTime 1: " + time1);
        System.out.println("LocalTime 2: " + time2);
  
        // check if LocalTime is before LocalTime
        // using isBefore()
        boolean value = time1.isBefore(time2);
  
        // print result
        System.out.println("Is LocalTime1 before LocalTime2: "
                           + value);
    }
}


Output:

LocalTime 1: 19:34:50.630
LocalTime 2: 23:14:00.630
Is LocalTime1 before LocalTime2: true

Program 2:




// Java program to demonstrate
// LocalTime.isBefore() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime object
        LocalTime time1
            = LocalTime.parse("23:59:11.98");
  
        // create other LocalTime
        LocalTime time2
            = LocalTime.parse("10:24:53.21");
  
        // print instances
        System.out.println("LocalTime 1: " + time1);
        System.out.println("LocalTime 2: " + time2);
  
        // check if LocalTime is before LocalTime
        // using isBefore()
        boolean value = time1.isBefore(time2);
  
        // print result
        System.out.println("Is LocalTime1 before LocalTime2: "
                           + value);
    }
}


Output:

LocalTime 1: 23:59:11.980
LocalTime 2: 10:24:53.210
Is LocalTime1 before LocalTime2: false

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#isBefore(java.time.LocalTime)


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Dec, 2018
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials