Java.util.Calendar.set() Method


The java.util.Calendar.set(int field,int value) method sets the given calendar field to the given value.


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

public void set(int field,int value)


  • field − The field to be altered.

  • amount − The value to be given to the calendar field.

Return Value

This method does not return a value.


ArrayIndexOutOfBoundsException − if the specified field is out of range


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 year
      System.out.println("Current year is :" + cal.get(Calendar.YEAR));

      // set the year to something else
      cal.set(Calendar.YEAR, 1997);

      // print the result
      System.out.println("Altered year is :" + cal.get(Calendar.YEAR));

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

Current year is :2012
Altered year is :1997
Kickstart Your Career

Get certified by completing the course

Get Started