Java Date Time - Java Calendar.clear(int field)








Syntax

Calendar.clear(int field) has the following syntax.

public final void clear(int field)

Example

In the following code shows how to use Calendar.clear(int field) method.

/*w  ww.  ja v  a  2 s  . c o  m*/
import java.util.Calendar;

public class Main {

   public static void main(String[] args) {

      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);

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

The code above generates the following result.