當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java DateFormat setCalendar()用法及代碼示例


Java中的DateFormat類的setCalendar()方法用於設置與此日期/時間格式對象關聯的日曆。在初始階段,將使用日曆的默認設置以及默認語言環境。

用法:

public void setCalendar(Calendar new_calendar)



參數:該方法采用Calendar類型的一個參數new_calendar,表示要替換原始DateFormat對象的新Calendar。

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

以下程序說明了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/text/DateFormat.html#setCalendar(java.util.Calendar)



相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 DateFormat setCalendar() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。