Home » Java programming language

Java Calendar before() Method with Example

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

Calendar Class before() method

  • before() method is available in java.util package.
  • before() method is used to check whether this calendar time is before the time denoted by given Object's time or not.
  • before() 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.
  • before() method does not throw an exception at the time of checking this object with the given object.

Syntax:

    public boolean before(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 this calendar time is before the time denoted by the given Object otherwise it returns false.

Example:

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

import java.util.*;

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

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

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

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

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

Output

curr_ca.getTime(): Thu Jan 23 11:18:28 GMT 2020
before_ca.getTime(): Sat Mar 23 11:18:28 GMT 2019
before_ca.before(curr_ca): true


Comments and Discussions!

Load comments ↻





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