Java Calendar.set(int year, int month, int date, int hourOfDay, int minute, int second) example

 Posted On  | Pooja Lakkihal 

In this java tutorial, You will learn how to set the value of the given calendar field values like YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, and SECOND.

Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, and SECOND.

syntax

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

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.

second - the value used to set the SECOND calendar field.

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

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

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

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

output

Current YEAR is :- 2018
Current MONTH is :- 0
Current DATE is :- 25
Current HOUR OF DAY is :- 15
Current MINUTE is :- 14
Current SECOND is :- 44

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


© technicalkeeda.com 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0247 seconds.