DateFormat setCalendar() Method in Java with Examples

The setCalendar() Method of DateFormat class in Java is used to set the calendar associated with this date/time format object. In the initial stage the default settings of calendar is used along with the default locale.

Syntax:

public void setCalendar(Calendar new_calendar)



Parameter: The method takes one parameter new_calendar of Calendar type and refers the new Calendar with which the original DateFormat object is to be replaced.

Return Value: The method does not return any values.

Below programs illustrate the use of setCalendar() method in Java:
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate
// setCalendar() method
  
import java.text.*;
import java.util.*;
  
public class DateFormat_Demo {
    public static void main(String[] argv)
    {
        // Initializing the first formatter
        DateFormat DFormat
            = DateFormat.getDateTimeInstance();
  
        // String formatting
        String str = DFormat.format(new Date());
  
        // Displaying the Calendar
        System.out.println("Original Calendar: "
                           + DFormat.getCalendar());
  
        // Creating a calendar
        Calendar calndr = Calendar.getInstance();
  
        // Replacing with a new value
        calndr.set(Calendar.MONTH, 11);
  
        // Setting new Calendar
        DFormat.setCalendar(calndr);
  
        System.out.println();
  
        // Displaying the newCalendar
        System.out.println("New Calendar: "
                           + DFormat.getCalendar());
    }
}

chevron_right


Output:

Original Calendar: java.util.GregorianCalendar[time=1553847137520, areFieldsSet=true,
areAllFieldsSet=true, lenient=true, zone=sun.util.calendar.ZoneInfo[id="Etc/UTC",
offset=0, dstSavings=0, useDaylight=false, transitions=0, lastRule=null], firstDayOfWeek=1,
minimalDaysInFirstWeek=1, ERA=1, YEAR=2019, MONTH=2, WEEK_OF_YEAR=13, WEEK_OF_MONTH=5,
DAY_OF_MONTH=29, DAY_OF_YEAR=88, DAY_OF_WEEK=6, DAY_OF_WEEK_IN_MONTH=5, AM_PM=0, HOUR=8,
HOUR_OF_DAY=8, MINUTE=12, SECOND=17, MILLISECOND=520, ZONE_OFFSET=0, DST_OFFSET=0]

New Calendar: java.util.GregorianCalendar[time=?, areFieldsSet=false, areAllFieldsSet=true,
lenient=true, zone=sun.util.calendar.ZoneInfo[id="Etc/UTC", offset=0, dstSavings=0,
useDaylight=false, transitions=0, lastRule=null], firstDayOfWeek=1, minimalDaysInFirstWeek=1,
ERA=1, YEAR=2019, MONTH=11, WEEK_OF_YEAR=13, WEEK_OF_MONTH=5, DAY_OF_MONTH=29, DAY_OF_YEAR=88,
DAY_OF_WEEK=6, DAY_OF_WEEK_IN_MONTH=5, AM_PM=0, HOUR=8, HOUR_OF_DAY=8, MINUTE=12, SECOND=17,
MILLISECOND=521, ZONE_OFFSET=0, DST_OFFSET=0]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate
// setCalendar() method
  
import java.text.*;
import java.util.*;
  
public class DateFormat_Demo {
    public static void main(String[] argv)
    {
        // Initializing the first formatter
        DateFormat DFormat
            = DateFormat.getDateInstance();
  
        // String formatting
        String str = DFormat.format(new Date());
  
        // Displaying the Calendar
        System.out.println("Original Calendar: "
                           + DFormat.getCalendar());
  
        // Creating a calendar
        Calendar calndr = Calendar.getInstance();
  
        // Replacing with a new value
        calndr.set(Calendar.YEAR, 1996);
  
        // Setting new Calendar
        DFormat.setCalendar(calndr);
        System.out.println();
        // Displaying the newCalendar
        System.out.println("New Calendar: "
                           + DFormat.getCalendar());
    }
}

chevron_right


Output:

Original Calendar: java.util.GregorianCalendar[time=1553847144311, areFieldsSet=true,
areAllFieldsSet=true, lenient=true, zone=sun.util.calendar.ZoneInfo[id="Etc/UTC",
offset=0, dstSavings=0, useDaylight=false, transitions=0, lastRule=null], firstDayOfWeek=1,
minimalDaysInFirstWeek=1, ERA=1, YEAR=2019, MONTH=2, WEEK_OF_YEAR=13, WEEK_OF_MONTH=5,
DAY_OF_MONTH=29, DAY_OF_YEAR=88, DAY_OF_WEEK=6, DAY_OF_WEEK_IN_MONTH=5, AM_PM=0, HOUR=8,
HOUR_OF_DAY=8, MINUTE=12, SECOND=24, MILLISECOND=311, ZONE_OFFSET=0, DST_OFFSET=0]

New Calendar: java.util.GregorianCalendar[time=?, areFieldsSet=false, areAllFieldsSet=true,
lenient=true, zone=sun.util.calendar.ZoneInfo[id="Etc/UTC", offset=0, dstSavings=0,
useDaylight=false, transitions=0, lastRule=null], firstDayOfWeek=1, minimalDaysInFirstWeek=1,
ERA=1, YEAR=1996, MONTH=2, WEEK_OF_YEAR=13, WEEK_OF_MONTH=5, DAY_OF_MONTH=29, DAY_OF_YEAR=88,
DAY_OF_WEEK=6, DAY_OF_WEEK_IN_MONTH=5, AM_PM=0, HOUR=8, HOUR_OF_DAY=8, MINUTE=12, SECOND=24,
MILLISECOND=318, ZONE_OFFSET=0, DST_OFFSET=0]

Reference: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setCalendar(java.util.Calendar)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.