Java Calendar.set(int field, int value) method example

 Posted On  | Pooja Lakkihal 

In this java tutorial, You will learn how to set the value of the given calendar field. Java Calendar.set(int field, int value) method throws ArrayIndexOutOfBoundsException if the specified field is out of range.

Sets the given calendar field to the given value. The value is not interpreted by this method regardless of the leniency mode.


public void set(int field, int value)


field - the given calendar field.

value - the value to be set for the given calendar field.


ArrayIndexOutOfBoundsException - if the specified field is out of range (field < 0 || field >= FIELD_COUNT). in non-lenient mode.

Java Calendar set(int field, int value) example

  • Create Calendar instance by invoking static method Calendar.getInstance();
  • calendar.get(Calendar.YEAR) This will get the calendar field value.
  • calendar.set(Calendar.YEAR, 1993); This will Set the calendar field value.
package com.technicalkeeda;

import java.util.Calendar;

public class CalenderApp {

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();

        System.out.println("Current year is :- " + calendar.get(Calendar.YEAR));
        System.out.println("Current month is :- " + calendar.get(Calendar.MONTH));

        calendar.set(Calendar.YEAR, 1993);
        calendar.set(Calendar.MONTH, 4);

        System.out.println("Changed year is :- " + calendar.get(Calendar.YEAR));
        System.out.println("Changed month is :- " + calendar.get(Calendar.MONTH));


Current year is :- 2018
Current month is :- 0
Changed year is :- 1993
Changed month is :- 4

© 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0443 seconds.