Home » Java programming language

Java Calendar after() Method with Example

Calendar Class after() method: Here, we are going to learn about the after() method of Calendar Class with its syntax and example.
Submitted by Preeti Jain, on January 23, 2020

Calendar Class after() method

  • after() method is available in java.util package.
  • after() method is used to check whether this calendar time is after the time denoted by the given Object's time or not.
  • after() method is a non-static method, it is accessible with the class object and if we try to access the method with the class name then we will get an error.
  • after() method does not throw an exception at the checking this calendar time with the given time.

Syntax:

    public boolean after(Object time);

Parameter(s):

  • Object time – represents the time to be compared with this calendar time.

Return value:

The return type of the method is boolean, it returns true when the given time is before this calendar time otherwise it returns false.

Example:

// Java Program to demonstrate the example of
// boolean after(Object) method of Calendar

import java.util.*;

public class AfterOfCalendar {
    public static void main(String[] args) {
        // Instantiating two Calendar object
        Calendar curr_ca = Calendar.getInstance();
        Calendar after_ca = Calendar.getInstance();

        // By using add() method is to add the 
        // 10 months to the current calendar
        after_ca.add(Calendar.MONTH, 10);

        // Display current and after calendar
        System.out.println("curr_ca.getTime(): " + curr_ca.getTime());
        System.out.println("after_ca.getTime(): " + after_ca.getTime());

        // By using after() method is to check
        // the after_ca time is after the curr_ca
        boolean status = after_ca.after(curr_ca);

        //Display Result
        System.out.println("after_ca.after(curr_ca): " + status);
    }
}

Output

curr_ca.getTime(): Thu Jan 23 11:12:36 GMT 2020
after_ca.getTime(): Mon Nov 23 11:12:36 GMT 2020
after_ca.after(curr_ca): true


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.