Java Calendar set() Method



Description

The Java Calendar set(int field,int value) method sets the given calendar field to the given value.

Declaration

Following is the declaration for java.util.Calendar.set() method

public void set(int field,int value)

Parameters

  • field − The field to be altered.

  • amount − The value to be given to the calendar field.

Return Value

This method does not return a value.

Exception

ArrayIndexOutOfBoundsException − if the specified field is out of range

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

Description

The java.util.Calendar.set(int year, int month, int date) method sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.

Declaration

Following is the declaration for java.util.Calendar.set(int year, int month, int date) method

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

Parameters

  • year − Value to be used for YEAR field

  • month − Value to be used for MONTH field. 0 is January

  • day − Value to be used for DAY field

Return Value

This method does not return a value.

Exception

NA

Java Calendar set(int year,int month,int day,int hourOfDay, int minute) Method

Description

The java.util.Calendar.set(int year,int month,int day,int hourOfDay, int minute) method sets the values for the calendar fields YEAR, MONTH,DAY_OF_MONTH,HOUR_OF_DAY and MINUTE.

Declaration

Following is the declaration for java.util.Calendar.set() method

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

Parameters

  • year − Value to be used for YEAR field

  • month − Value to be used for MONTH field. 0 is January

  • day − Value to be used for DAY field

  • hourOfDay − Value to be used for HOUR_OF_DAY field

  • minute − Value to be used for MINUTE field

Return Value

This method does not return a value.

Exception

NA

Java Calendar set(int year,int month,int day,int hourOfDay, int minute,int second) Method

Description

The java.util.Calendar.set(int year,int month,int day,int hourOfDay, int minute,int second) method sets the values for the calendar fields YEAR, MONTH,DAY_OF_MONTH,HOUR_OF_DAY,MINUTE and SECOND.

Declaration

Following is the declaration for java.util.Calendar.set() method

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

Parameters

  • year − Value to be used for YEAR field

  • month − Value to be used for MONTH field. 0 is January

  • day − Value to be used for DAY field

  • hourOfDay − Value to be used for HOUR_OF_DAY field

  • minute − Value to be used for MINUTE field

  • second − Value to be used for SECOND field

Return Value

This method does not return a value.

Exception

NA

Setting Year to the Current Dated Calendar Instance Example

The following example shows the usage of Java Calendar set(field,value) method. We're creating an instance of a Calendar of current date and print the current year using get() method and set we set the year using set() method and then the updated year is printed.

package com.tutorialspoint;

import java.util.Calendar;

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

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

      // print current year
      System.out.println("Current year is :" + cal.get(Calendar.YEAR));

      // set the year to something else
      cal.set(Calendar.YEAR, 1997);

      // print the result
      System.out.println("Altered year is :" + cal.get(Calendar.YEAR));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Current year is :2022
Altered year is :1997

Setting Year, Month, Day to the Current Dated Calendar Instance Example

The following example shows the usage of Java Calendar set(year,month,day) method. We're creating an instance of a Calendar of current date and print the current year using get() method and set we set the year using set() method and then the updated date is printed.

package com.tutorialspoint;

import java.util.Calendar;

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

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

      // print current time
      System.out.println("Current date is :" + cal.getTime());

      // set the year,month and day to something else
      cal.set(1995, 5, 25);

      // print the result
      System.out.println("Altered date is :" + cal.getTime()); 
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Current date is :Tue Sep 27 20:31:15 IST 2022
Altered date is :Sun Jun 25 20:31:15 IST 1995

Setting Year,Month,Day, Hour and Minute to the Current Dated Calendar Instance Example

The following example shows the usage of Java Calendar set(year,month,day,hour,minute) method. We're creating an instance of a Calendar of current date and print the current year using get() method and set we set the year using set() method and then the updated date is printed.

package com.tutorialspoint;

import java.util.Calendar;

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

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

      // print current time
      System.out.println("Current date is :" + cal.getTime());

      // set the year, month, day, hour and minute to something else
      cal.set(1995, 5, 25, 04, 15);

      // print the result
      System.out.println("Altered date is :" + cal.getTime());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Current date is :Tue Sep 27 20:33:39 IST 2022
Altered date is :Sun Jun 25 04:15:39 IST 1995

Setting Year,Month,Day,Hour,Minute,Second to the Current Dated Calendar Instance Example

The following example shows the usage of Java Calendar set(year,month,day,hour,minute,second) method. We're creating an instance of a Calendar of current date and print the current year using get() method and set we set the year using set() method and then the updated date is printed.

package com.tutorialspoint;

import java.util.Calendar;

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

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

      // print current time
      System.out.println("Current date is :" + cal.getTime());

      // set the year, month, day, hour, minute and seconds to something else
      cal.set(1995, 5, 25, 04, 15, 20);

      // print the result
      System.out.println("Altered date is :" + cal.getTime());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Current date is :Tue Sep 27 20:37:02 IST 2022
Altered date is :Sun Jun 25 04:15:20 IST 1995
java_util_calendar.htm
Advertisements