java.util.Calendar clear()

Description :

This java tutorial shows how to use the clear() method of Calendar class of java.util package. This method sets all the calendar field values and the time value (millisecond offset from the Epoch) of this Calendar undefined. This means that isSet() will return false for all the calendar fields, and the date and time calculations will treat the fields as if they had never been set.

Method Syntax :

public final void clear()

Parameter Input :

 

DataType Parameter Description
N/A N/A N/A

 

Method Returns :

This method returns void.

Compatibility Version :

Requires Java 1.1 and up

Exception :

N/A

Java Code Example :

This java example source code demonstrates the use of clear()  method of Calendar class.

package com.javatutorialhq.java.examples;

import java.util.Calendar; import java.util.Date;

/* * This example source code demonstrates the use of * clear() method of Calendar class */

public class CalendarClearExample {

public static void main(String

[] args) throws InterruptedException {

Calendar cal = Calendar.getInstance(); // Printing the current time System.out.println("Current time:" + cal.getTime());

// set the time using the 1st overloaded method cal.set(Calendar.YEAR, 2019); // set the year System.out.println("Time After set:"+cal.getTime());

// clear the calendar object cal.clear(); System.out.println(cal.getTime());

// set the year again cal.set(Calendar.YEAR, 2019); // set the year System.out.println("Time after clear and set:"+cal.getTime());

}

}

Sample Output :

Running the clear() method example source code of Calendar class will give you the following output:

Calendar clear() method example

Calendar clear() method example

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :