Java Calendar.set(int year, int month, int date, int hourOfDay, int minute) 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, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE.

syntax

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

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.

hourOfDay - the value used to set the HOUR_OF_DAY calendar field.

minute - the value used to set the MINUTE calendar field.

Java Calendar set(int year, int month, int date, int hourOfDay, int minute) 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, hourOfDay, minute); 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));
        System.out.println("Current HOUR OF DAY is :- " + calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println("Current MINUTE is :- " + calendar.get(Calendar.MINUTE));

        int year = 1993;
        int month = 8;
        int date = 27;
        int hourOfDay = 4;
        int minute = 37;

        calendar.set(year, month, date, hourOfDay, minute);

        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));
        System.out.println("Changed HOUR OF DAY is :- " + calendar.get(Calendar.HOUR_OF_DAY));
        System.out.println("Changed MINUTE is :- " + calendar.get(Calendar.MINUTE));
    }
}

output

Current YEAR is :- 2018
Current MONTH is :- 0
Current DATE is :- 25
Current HOUR OF DAY is :- 15
Current MINUTE is :- 6

Changed YEAR is :- 1993
Changed MONTH is :- 8
Changed DATE is :- 27
Changed HOUR OF DAY is :- 4
Changed MINUTE is :- 37


© technicalkeeda.com 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0352 seconds.