Home » Java programming language

Java Calendar clear() Method with Example

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

Calendar Class clear() method

Syntax:

    public final void clear();
    public final void clear(int fi);
  • clear() method is available in java.util package.
  • clear() method is used to set the value of all calendar fields because no particular field is passed in the method and the value is not defined so the Calendar class uses the default field for date and time.
  • clear(int fi) method is used to set the value of the given calendar field because the particular field(fi) is passed in the method and value is not defined so the Calendar class uses default field for date & time.
  • These methods do not throw an exception at the time of setting the given field with the default value.
  • These are non-static methods, so it is accessible with the class object and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, clear() – it does not accept any parameter.
  • In the second case, clear(int fi) – represents the calendar field to set as not defined.

Return value:

In both the cases, the return type of the method is void, it returns nothing.

Example:

// Java Program to demonstrate the example of
// void clear() method of Calendar

import java.util.*;

public class ClearOfCalendar {
    public static void main(String[] args) {
        // Instantiating a Calendar object
        Calendar ca = Calendar.getInstance();

        // Display current calendar
        System.out.println("ca.getTime(): " + ca.getTime());

        // By using clear(int fi) method is to 
        // clear the current value and sets the
        // undefined value of the month field 
        // in current calendar
        ca.clear(Calendar.MONTH);

        // Display Calendar
        System.out.println("ca.clear(Calendar.MONTH): " + ca.getTime());

        // By using clear() method is to 
        // clear the current value of all the fields and 
        // sets the undefined value of the fields 
        // in current calendar
        ca.clear();

        // Display Calendar
        System.out.println("ca.clear(): " + ca.getTime());
    }
}

Output

ca.getTime(): Thu Jan 23 11:29:31 GMT 2020
ca.clear(Calendar.MONTH): Thu Jan 23 11:29:31 GMT 2020
ca.clear(): Thu Jan 01 00:00:00 GMT 1970


Comments and Discussions!

Load comments ↻





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