Calendar set(int, int, int, int, int) in Java

set(int, int, int, int, int): This method is available in java.util.Calendar class of Java.

Syntax:

void java.util.Calendar.set(int year, int month, int date, int hourOfDay, int minute)

This method takes five arguments of type int as its parameters. This method sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE.

Parameters: Five parameters are required for this method.

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.

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.

Returns: NA

Exceptions: NA

Approach

Java

import java.util.Calendar;

public class Calendarset3 {
    public static void main(String[] args) {

        // create a calendar
        Calendar calendar = Calendar.getInstance();

        int year = 2022, month = 1, date = 12;
        int hourOfDay = 12, minute = 22;
        calendar.set(year, month, date, hourOfDay, minute);
        System.out.println(calendar);
    }
}

Output:

java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2022,MONTH=1,WEEK_OF_YEAR=5,WEEK_OF_MONTH=5,DAY_OF_MONTH=12,DAY_OF_YEAR=23,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=10,HOUR_OF_DAY=12,MINUTE=22,SECOND=17,MILLISECOND=221,ZONE_OFFSET=19800000,DST_OFFSET=0]


No comments:

Post a Comment