Java Utility Library

Java Calendar - clear() Method



The java.util.Calendar.clear() method is used to set the given calendar field value and the time value (millisecond offset from the Epoch) of this Calendar undefined. A Calendar implementation class may use its specific default field values for date/time calculations.

Syntax

public final void clear(int field)

Parameters

field Specify the calendar field to be cleared.

Return Value

void type.

Exception

NA

Example:

In the example below, the java.util.Calendar.clear() method is used to set the given calendar field values and the time value of the given Calendar undefined.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Calendar with specified date
    Calendar Cal = new GregorianCalendar(2018, 10, 25);

    //printing the calendar
    System.out.println("The Calendar is: " + Cal.getTime());

    //using the clear() method to set month
    //field values to undefined
    Cal.clear(Calendar.MONTH);

    //printing the calendar
    System.out.println("New Calendar is: " + Cal.getTime());

    //using the clear() method to set year
    //field values to undefined
    Cal.clear(Calendar.YEAR);

    //printing the calendar
    System.out.println("New Calendar is: " + Cal.getTime());    
  }
}

The output of the above code will be:

The Calendar is: Sun Nov 25 00:00:00 UTC 2018
New Calendar is: Thu Jan 25 00:00:00 UTC 2018
New Calendar is: Sun Jan 25 00:00:00 UTC 1970

❮ Java.util - Calendar