- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Calendar clear() Method
Description
The Java Calendar clear() method sets all the calendar field values and the time value of this Calendar undefined.A Calendar implementation class may use default field values for date and time calculations.
Declaration
Following is the declaration for java.util.Calendar.clear() method
public final void clear()
Parameters
NA
Return Value
This method does not return any value.
Exception
NA
Java Calendar clear(int field) Method
Description
The Java Calendar clear() method sets the given calendar field value and the time value of this Calendar undefined. A Calendar implementation class may use default field values for date and time calculations.
Declaration
Following is the declaration for java.util.Calendar.clear() method
public final void clear(int field)
Parameters
field − calendar field to set as undefined.
Return Value
This method does not return any value.
Exception
NA
Clearing a Current Dated Calendar Instance Example
The following example shows the usage of Java Calendar clear() method. We're creating a Calendar instance of current date. We're clearing it using clear() method and printing it.
package com.tutorialspoint; import java.util.Calendar; public class CalendarDemo { public static void main(String[] args) { // create calendar object Calendar cal = Calendar.getInstance(); // displays the current date and time System.out.println("Current Calendar Date: " + cal.getTime()); // use clear method to set all field values and time value as undefined. cal.clear(); // print the result System.out.println("The calendar shows : " + cal.getTime()); } }
Output
Let us compile and run the above program, this will produce the following result −
Current Calendar Date: Fri Sep 23 14:56:04 IST 2022 The calendar shows : Thu Jan 01 00:00:00 IST 1970
Clearing Year and Month of a Current Dated Calendar Instance Example
The following example shows the usage of Java Calendar clear(index) method. We're creating a Calendar instance of current date. We're clearing the Year and then Month fields using clear(index) method and printing it.
package com.tutorialspoint; import java.util.Calendar; public class CalendarDemo { public static void main(String[] args) { // create a calendar Calendar cal = Calendar.getInstance(); // display the current date and time System.out.println("Current Calendar Date: " + cal.getTime()); // use clear method to set year as undefined. cal.clear(Calendar.YEAR); // print the result System.out.println("The calendar shows : " + cal.getTime()); // use clear method to set month as undefined as well. cal.clear(Calendar.MONTH); // print the result System.out.println("The calendar shows : " + cal.getTime()); } }
Output
Let us compile and run the above program, this will produce the following result −
Current Calendar Date: Fri Sep 23 15:02:55 IST 2022 The calendar shows : Wed Sep 23 15:02:55 IST 1970 The calendar shows : Fri Jan 23 15:02:55 IST 1970
Clearing a Current Dated Calendar Instance Example
The following example shows the usage of Java Calendar clear() method. We're creating a Gregorian Calendar instance of current date. We're clearing it using clear() method and printing it.
package com.tutorialspoint; import java.util.Calendar; import java.util.GregorianCalendar; public class CalendarDemo { public static void main(String[] args) { // create calendar object Calendar cal = GregorianCalendar.getInstance(); // displays the current date and time System.out.println("Current Calendar Date: " + cal.getTime()); // use clear method to set all field values and time value as undefined. cal.clear(); // print the result System.out.println("The calendar shows : " + cal.getTime()); } }
Output
Let us compile and run the above program, this will produce the following result −
Current Calendar Date: Fri Sep 23 14:58:29 IST 2022 The calendar shows : Thu Jan 01 00:00:00 IST 1970