Java.util.Calendar.set() Method



Description

The java.util.Calendar.set(int, int, int, int, int) method sets the values for the calendar fields YEAR, MONTH,DAY_OF_MONTH,HOUR_OF_DAY and MINUTE.

Declaration

Following is the declaration for java.util.Calendar.set() method

public final void set(int year,int month,int day,int hourOfDay, int minute)

Parameters

  • year − Value to be used for YEAR field

  • month − Value to be used for MONTH field. 0 is January

  • day − Value to be used for DAY field

  • hourOfDay − Value to be used for HOUR_OF_DAY field

  • minute − Value to be used for MINUTE field

Return Value

This method does not return a value.

Exception

NA

Example

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

package com.tutorialspoint;

import java.util.Calendar;

public class CalendarDemo {
   public static void main(String[] args) {

      // create a calendar
      Calendar cal = Calendar.getInstance();

      // print current time
      System.out.println("Current year is :" + cal.getTime());

      // set the year,month and day to something else
      cal.set(1995, 5, 25, 04, 15);

      // print the result
      System.out.println("Altered year is :" + cal.getTime());
   }
}

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

Current year is :Wed May 02 19:14:41 EEST 2012
Altered year is :Sun Jun 25 04:15:41 EEST 1995
java_util_calendar.htm
Advertisements