Java LocalDateTime Class | isBefore() Method with Example

LocalDateTime Class isBefore() method: Here, we are going to learn about the isBefore() method of LocalDateTime Class with its syntax and example.
Submitted by Preeti Jain, on June 06, 2020

LocalDateTime Class isBefore() method

  • isBefore() method is available in java.time package.
  • isBefore() method is used to check whether this date-time value comes before the given date-time (l_datetime) value or not.
  • isBefore() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • isBefore() method does not throw an exception at the time of checking the status.

Syntax:

    public boolean isBefore(ChronoLocalDateTime l_datetime);

Parameter(s):

  • ChronoLocalDateTime l_datetime – represents the ChronoLocalDateTime object to be compared with this LocalDateTime.

Return value:

The return type of this method is boolean, it returns true when this date-time value comes before the given date-time value otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of isBefore(ChronoLocalDateTime l_datetime) method 
// of LocalDateTime

import java.time.*;

public class IsBeforeOfLocalDateTime {
    public static void main(String args[]) {
        // Instantiates two LocalDateTime
        LocalDateTime da_ti1 = LocalDateTime.parse("2005-10-05T10:10:10.00");
        LocalDateTime da_ti2 = LocalDateTime.now();

        // Display da_ti1, da_ti2
        System.out.println("LocalDateTime da_ti1 and da_ti2: ");
        System.out.println("da_ti1: " + da_ti1);
        System.out.println("da_ti2: " + da_ti2);

        System.out.println();

        // Here, this method checks whether this
        // date-time object (da_ti1) comes before 
        // the given date-time object (da_ti2) or
        // not i.e. here it returns true because
        // da_ti1 comes before da_ti2
        boolean status = da_ti1.isBefore(da_ti2);

        // Display status
        System.out.print("da_ti1.isBefore(da_ti2): ");
        System.out.println(status);

        // Here, this method checks whether this
        // date-time object (da_ti2) comes before
        // the given date-time object (da_ti1) or
        // not i.e. here it returns false because
        // da_ti2 comes after da_ti1
        status = da_ti2.isBefore(da_ti1);

        // Display status
        System.out.print("da_ti2.isBefore(da_ti1): ");
        System.out.println(status);
    }
}

Output

LocalDateTime da_ti1 and da_ti2: 
da_ti1: 2005-10-05T10:10:10
da_ti2: 2020-06-06T21:06:23.203685

da_ti1.isBefore(da_ti2): true
da_ti2.isBefore(da_ti1): false



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.