Java Calendar.set(int year, int month, int date) method example

 Posted On  | Pooja Lakkihal 

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

Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.

syntax

public final void set(int year, int month, int date)

Parameters

year - the value used to set the YEAR calendar field.

month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.

date - the value used to set the DAY_OF_MONTH calendar field.

Java Calendar set(int year, int month, int date) example

  • Create Calendar instance by invoking static method Calendar.getInstance();
  • calendar.get(Calendar.YEAR) This will get the calendar field value.
  • calendar.set(year, month, date); 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));
        System.out.println("Current date is :- " + calendar.get(Calendar.DATE));

        int year = 1993;
        int month = 8;
        int date = 27;

        calendar.set(year, month, date);

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

output

Current year is :- 2018
Current month is :- 0
Current date is :- 25

Changed year is :- 1993
Changed month is :- 8
Changed date is :- 27


© technicalkeeda.com 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0237 seconds.