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.
java_util_calendar.htm
Advertisements