Open In App

Calendar clear(int cal_field) Method in Java with Examples

Last Updated : 27 Mar, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The clear(int cal_field) method in Calendar class is used to set a given calendar field value and the time value of this Calendar undefined.
Note: Implementation of calendar class may use default field values for date and time calculations.

Syntax:

public final void clear(int cal_field)

Parameters: The method takes one parameter cal_field of Integer type and refers to the field value that is to be made undefined.

Return Value: The method does not return any value.

Below programs illustrate the working of clear() Method of Calendar class:
Example 1:




// Java Code to illustrate clear() Method
  
import java.util.Calendar;
  
public class CalendarClassDemo {
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Displaying the Current Date
        System.out.println("Current Date&Time: "
                           + calndr.getTime());
  
        // Clear method for undefining the month
        // of the calendar
        calndr.clear(Calendar.MONTH);
  
        // Displaying the final result
        System.out.println("After clear method: "
                           + calndr.getTime());
    }
}


Output:

Current Date&Time: Wed Mar 27 09:03:26 UTC 2019
After clear method: Sun Jan 27 09:03:26 UTC 2019

Example 2:




// Java Code to illustrate clear() Method
  
import java.util.Calendar;
  
public class CalendarClassDemo {
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar calndr = Calendar.getInstance();
  
        // Displaying the Current Date
        System.out.println("Current Date&Time: "
                           + calndr.getTime());
  
        // Clear method for undefining the year
        // of the calendar
        calndr.clear(Calendar.YEAR);
  
        // Displaying the final result
        System.out.println("After clear method: "
                           + calndr.getTime());
    }
}


Output:

Current Date&Time: Wed Mar 27 09:03:32 UTC 2019
After clear method: Fri Mar 27 09:03:32 UTC 1970

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#clear(int)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads