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
java_util_calendar.htm
Advertisements