Java .after()
The .after() method determines whether one date or time occurs chronologically after another date or time. It takes two instances of date or time objects as input and returns a boolean value; true if the first instance is later in time than the second instance, and false otherwise. This method is commonly used to compare and order events, appointments, or other time-related data.
Syntax
result = myCalendar.after(time);
Note: Please note that the
Calendarclass has been replaced by theLocalDate,LocalTime, andLocalDateTimeclasses in the Java 8 and later Date and Time API. These classes provide a more modern and user-friendly API for working with dates and times in Java.
Example
In this example, calendar1 is set to August 1, 2023, and calendar2 is set to September 1, 2023. Then, the .after() method is used to compare these two instances.
import java.util.Calendar;public class CalendarExample {public static void main(String[] args) {// Create two instances of CalendarCalendar calendar1 = Calendar.getInstance();Calendar calendar2 = Calendar.getInstance();// Set different dates for each instancecalendar1.set(2023, Calendar.AUGUST, 1); // August 1, 2023calendar2.set(2023, Calendar.SEPTEMBER, 1); // September 1, 2023// Check if calendar1 is after calendar2boolean isAfter = calendar1.after(calendar2);if (isAfter) {System.out.println("calendar1 is after calendar2");} else {System.out.println("calendar1 is not after calendar2");}}}
The above code will output:
calendar1 is not after calendar2
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours