- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Calendar after() Method
Description
The Java Calendar after() method returns whether this Calendar's time is after the time represented by the specified Object (when).
Declaration
Following is the declaration for java.util.Calendar.after() method
public boolean after(Object when)
Parameters
when − the Object of time that is about to be compared.
Return Value
true if the time represented by this Calendar is after the time represented by when Object; false otherwise.
Exception
NA
Comparing Two Calendar Instances being After One Another Example
The following example shows the usage of Java Calendar after() method. We're creating two Calendar instances of current date. One of the calendar is modified for future date and then compared using after() method.
package com.tutorialspoint; import java.util.Calendar; import java.util.Date; public class CalendarDemo { public static void main(String[] args) { // create calendar objects. Calendar cal = Calendar.getInstance(); Calendar future = Calendar.getInstance(); // print the current date System.out.println("Current date: " + cal.getTime()); // change year in future calendar future.set(Calendar.YEAR, 2025); System.out.println("Year is " + future.get(Calendar.YEAR)); // check if calendar date is after current date Date time = future.getTime(); if (future.after(cal)) { System.out.println("Date " + time + " is after current date."); } } }
Output
Let us compile and run the above program, this will produce the following result −
Current date: Fri Sep 23 14:35:06 IST 2022 Year is 2025 Date Tue Sep 23 14:35:06 IST 2025 is after current date.
Comparing Two Calendar Instances being Before One Another Example
The following example shows the usage of Java Calendar after() method. We're creating two Calendar instances of current date. One of the calendar is modified for past date and then compared using after() method.
package com.tutorialspoint; import java.util.Calendar; import java.util.Date; public class CalendarDemo { public static void main(String[] args) { // create calendar objects. Calendar cal = Calendar.getInstance(); Calendar past = Calendar.getInstance(); // print the current date System.out.println("Current date: " + cal.getTime()); // change year in past calendar past.set(Calendar.YEAR, 2015); System.out.println("Year is " + past.get(Calendar.YEAR)); // check if calendar date is after current date Date time = past.getTime(); if (past.after(cal)) { System.out.println("Date " + time + " is after current date."); }else{ System.out.println("Date " + time + " is before current date."); } } }
Output
Let us compile and run the above program, this will produce the following result −
Current date: Fri Sep 23 14:41:31 IST 2022 Year is 2015 Date Wed Sep 23 14:41:31 IST 2015 is before current date.
Comparing Current Calendar Instances being After Given Calendar Instance Example
The following example shows the usage of Java Calendar after() method. We're creating two Calendar instances of current date. One of the calendar is modified for past date and then compared using after() method on current dated calendar instance.
package com.tutorialspoint; import java.util.Calendar; import java.util.Date; public class CalendarDemo { public static void main(String[] args) { // create calendar objects. Calendar cal = Calendar.getInstance(); Calendar past = Calendar.getInstance(); // print the current date System.out.println("Current date: " + cal.getTime()); // change year in past calendar past.set(Calendar.YEAR, 2015); System.out.println("Year is " + past.get(Calendar.YEAR)); // check if calendar date is after current date Date time = past.getTime(); if (cal.after(past)) { System.out.println("Date " + time + " is before current date."); }else{ System.out.println("Date " + time + " is after current date."); } } }
Output
Let us compile and run the above program, this will produce the following result −
Current date: Fri Sep 23 14:43:14 IST 2022 Year is 2015 Date Wed Sep 23 14:43:14 IST 2015 is before current date.