📌  相关文章
📜  Java的DateFormat setCalendar() 方法和示例

📅  最后修改于: 2022-05-13 01:57:13.214000             🧑  作者: Mango

Java的DateFormat setCalendar() 方法和示例

Java中DateFormat类setCalendar()方法用于设置与这个日期/时间格式对象关联的日历。在初始阶段,日历的默认设置与默认语言环境一起使用。

句法:

参数:该方法采用一个 Calendar 类型的参数new_calendar并引用要替换原始 DateFormat 对象的新日历。

返回值:该方法不返回任何值。



下面的程序说明了Java中 setCalendar() 方法的使用:
示例 1:

// 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());
    }
}
输出:
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]

示例 2:

// 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());
    }
}
输出:
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]

参考:https://docs.oracle.com/javase/7/docs/api/的Java Java的.util.Calendar)