Java LocalDateTime isBefore() Method Example in Java
In this tutorial, we'll see how we can use Java LocalDateTime isBefore() method with Example.
Here is the brief intro about what is LocalDateTime class in java.
LocalDateTime
- LocalDateTime is an instance of date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.
- LocalDateTime is an immutable date-time object that represents a date-time, often viewed as a year-month-day-hour-minute-second.
Method Declaration
public boolean isBefore(ChronoLocalDateTime<?> other)
This method checks if the first DateTime instance is before the specified DateTime instance.
Specified by:
isBefore method is present in interface ChronoLocalDateTime<LocalDate>
Parameters:
other - the other date-time to compare to, which should be not null.
Returns:
true if this first DateTime instance is before the specified DateTime instance otherwise false.
the LocalDateTime isBefore() method Example:
package com.javacodegeek.post7.localdatetimeisbefore;
import java.time.LocalDateTime;
public class LocalDateTimeIsBeforeExample {
public static void main(String[] args) {
// firstDateTime instance
LocalDateTime firstTime = LocalDateTime.of(2020, 5, 26, 12, 00);
// secondDateTime instance
LocalDateTime secondTime = LocalDateTime.of(2020, 6, 1, 12, 00);
// Checking firstDateTime instance is before the secondDateTime instance or not
boolean isFirstTimeBeforeSecondTime = firstTime.isBefore(secondTime);
System.out.println("isFirstTimeBeforeSecondTime : " + isFirstTimeBeforeSecondTime);
// firstTimeInstance
LocalDateTime time1 = LocalDateTime.parse("2020-05-26T10:15:30");
// secondTimeInstance
LocalDateTime time2 = LocalDateTime.parse("2019-04-12T10:15:30");
// Checking time1 instance is before the time2 Instance or not
boolean isTime1BeforeTime2 = time1.isBefore(time2);
System.out.println("isTime1BeforeTime2 : " + isTime1BeforeTime2);
}
}
Output:
Let us run the above program, this will produce the following output
isFirstTimeBeforeSecondTime : true
isTime1BeforeTime2 : false
Conclusion
In this quick tutorial, we have covered how we can use the LocalDateTime isBefore() method to check if the first DateTime instance is before the specified DateTime instance.
Happy Learning!!!
More Examples you may like
More Examples you may like
Comments
Post a Comment
If you have any doubts, Please let me know.