Introduction
This lab will introduce you to the isAfter()
method in the LocalDate
class of the java.time package. The method returns a boolean value indicating whether one date is after another.
This lab will introduce you to the isAfter()
method in the LocalDate
class of the java.time package. The method returns a boolean value indicating whether one date is after another.
In order to use the LocalDate
class and the isAfter()
method we need to import the following packages at the beginning of the file:
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
Create two LocalDate
objects representing two different dates. For example:
LocalDate firstDate = LocalDate.of(2021, 1, 1);
LocalDate secondDate = LocalDate.of(2021, 6, 1);
Use the isAfter()
method to check if the firstDate
is after the secondDate
. Print the result to the console. For example:
System.out.println(firstDate + " is after " + secondDate + ": " + firstDate.isAfter(secondDate));
Use the isAfter()
method to check if the secondDate
is after the firstDate
. Print the result to the console. For example:
System.out.println(secondDate + " is after " + firstDate + ": " + secondDate.isAfter(firstDate));
Create two LocalDate
objects representing the current date and tomorrow's date. You can do this using the now()
method and the plus()
method. For example:
LocalDate currentDate = LocalDate.now();
LocalDate tomorrowDate = currentDate.plus(1, ChronoUnit.DAYS);
Use the isAfter()
method to check if the currentDate
is after the tomorrowDate
. Print the result to the console. For example:
System.out.println(currentDate + " is after " + tomorrowDate + ": " + currentDate.isAfter(tomorrowDate));
Use the isAfter()
method to check if the tomorrowDate
is after the currentDate
. Print the result to the console. For example:
System.out.println(tomorrowDate + " is after " + currentDate + ": " + tomorrowDate.isAfter(currentDate));
Compile the code using the following command in the terminal:
javac LocalDateIsAfter.java
Run the code using the following command in the terminal:
java LocalDateIsAfter
In this lab, we learned about the isAfter()
method in the LocalDate
class of the java.time package. We saw how to use this method to check if one date is after another date.