Java Calendar.clear(int field) method example

 Posted On  | Pooja Lakkihal 

In this java tutorial, You will learn how to set the calendar field values and the time value.

Sets the given calendar field value and the time value (millisecond offset from the Epoch) of this Calendar undefined.

syntax

public final void clear(int field)

Parameters

field - the calendar field to be cleared.

Java Calendar clear(int field) example

  • Create CalendarApp instance which extends GregorianCalendar new CalendarApp();
  • calendar.getTime(); This will get the calendar time value.
  • calendar.clear(calendar.YEAR); It will clear the calendar field value.
  • calendar.set(GregorianCalendar.YEAR, 1993); It will calendar date to new value.
  • calendar.computeTime(); It will compute time according to the new date.
  • Print the new date by calendar.getTime()
package com.technicalkeeda;

import java.util.GregorianCalendar;

public class CalenderApp extends GregorianCalendar {

    public static void main(String[] args) {

        CalenderApp calendar = new CalenderApp();
        System.out.println("The current date is :- " + calendar.getTime());

        calendar.clear(calendar.YEAR);
        calendar.clear(calendar.MONTH);

        System.out.println("New Date is :- " + calendar.getTime());

    }
}

output

The current date is :- Thu Jan 25 15:59:01 IST 2018
New Date is :- Sun Jan 25 15:59:01 IST 1970


© technicalkeeda.com 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0449 seconds.