Java.util.Calendar.clear() Method



Description

The java.util.Calendar.clear() method sets the given calendar field valueand 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

Example

The following example shows the usage of java.util.calendar.clear() method.

package com.tutorialspoint;

import java.util.*;

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.
      cal.clear(Calendar.MONTH);

      // print the result
      System.out.println("The calendar shows : " + cal.getTime());
   }
}

Let us compile and run the above program, this will produce the following result −

Current Calendar Date: Sat May 05 15:33:32 EEST 2012
The calendar shows : Tue May 05 15:33:32 EET 1970
The calendar shows : Mon Jan 05 15:33:32 EET 1970
java_util_calendar.htm
Advertisements