Wednesday, June 10, 2026
HomeLanguagesJavaCalendar clear(int cal_field) Method in Java with Examples

Calendar clear(int cal_field) Method in Java with Examples

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)

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7018 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS