Java LocalDateTime Class | isAfter() Method with Example

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

LocalDateTime Class isAfter() method

  • isAfter() method is available in java.time package.
  • isAfter() method is used to check whether this date-time value comes after the given date-time (l_datetime) value or not.
  • isAfter() 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.
  • isAfter() method does not throw an exception at the time of checking the status.

Syntax:

    public boolean isAfter(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 after the given date-time value otherwise it returns false.

Example:

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

import java.time.*;

public class IsAfterOfLocalDateTime {
    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 after 
        // the given date-time object (da_ti2) or
        // not i.e. here it returns false because
        // da_ti1 comes before da_ti2
        boolean status = da_ti1.isAfter(da_ti2);

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

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

        // Display status
        System.out.print("da_ti2.isAfter(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:00:22.758196

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


Comments and Discussions!

Load comments ↻





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