DateTimeFormatInfo.Calendar 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定目前文化特性所使用的曆法。
public:
property System::Globalization::Calendar ^ Calendar { System::Globalization::Calendar ^ get(); void set(System::Globalization::Calendar ^ value); };
public System.Globalization.Calendar Calendar { get; set; }
member this.Calendar : System.Globalization.Calendar with get, set
Public Property Calendar As Calendar
屬性值
目前文化特性所使用的曆法。 InvariantInfo 的預設值是 GregorianCalendar 物件。
例外狀況
此屬性設定為 null
。
屬性正被設定為對目前文化特性而言並非有效的 Calendar 物件。
正在設定屬性,而且 DateTimeFormatInfo 物件為唯讀。
範例
下列範例會定義一個 ChangeCalendar
方法,將文化特性的目前行事曆變更為指定的行事曆,除非它已經是目前的行事曆或文化特性不支援。 呼叫方法的程式碼會具現化 CultureInfo 代表阿拉伯文 (埃及) 文化特性的物件,並且會先嘗試將它的行事曆變更為日文行事曆。 因為日文日曆不受支援,所以此方法不會變更文化特性的行事曆。 不過,因為 um al.exe 的行事曆是集合的成員,所以 CultureInfo.OptionalCalendars 此方法會成功使其成為 ar (即 ar)文化特性的目前行事曆。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo ci = CultureInfo.CreateSpecificCulture("ar-EG");
Console.WriteLine("The current calendar for the {0} culture is {1}",
ci.Name,
CalendarUtilities.ShowCalendarName(ci.DateTimeFormat.Calendar));
CalendarUtilities.ChangeCalendar(ci, new JapaneseCalendar());
Console.WriteLine("The current calendar for the {0} culture is {1}",
ci.Name,
CalendarUtilities.ShowCalendarName(ci.DateTimeFormat.Calendar));
CalendarUtilities.ChangeCalendar(ci, new UmAlQuraCalendar());
Console.WriteLine("The current calendar for the {0} culture is {1}",
ci.Name,
CalendarUtilities.ShowCalendarName(ci.DateTimeFormat.Calendar));
}
}
public class CalendarUtilities
{
private Calendar newCal;
private bool isGregorian;
public static void ChangeCalendar(CultureInfo ci, Calendar cal)
{
CalendarUtilities util = new CalendarUtilities(cal);
// Is the new calendar already the current calendar?
if (util.CalendarExists(ci.DateTimeFormat.Calendar))
return;
// Is the new calendar supported?
if (Array.Exists(ci.OptionalCalendars, util.CalendarExists))
ci.DateTimeFormat.Calendar = cal;
}
private CalendarUtilities(Calendar cal)
{
newCal = cal;
// Is the new calendar a Gregorian calendar?
isGregorian = cal.GetType().Name.Contains("Gregorian");
}
private bool CalendarExists(Calendar cal)
{
if (cal.ToString() == newCal.ToString()) {
if (isGregorian) {
if (((GregorianCalendar) cal).CalendarType ==
((GregorianCalendar) newCal).CalendarType)
return true;
}
else {
return true;
}
}
return false;
}
public static string ShowCalendarName(Calendar cal)
{
string calName = cal.ToString().Replace("System.Globalization.", "");
if (cal is GregorianCalendar)
calName += ", Type " + ((GregorianCalendar) cal).CalendarType.ToString();
return calName;
}
}
// The example displays the following output:
// The current calendar for the ar-EG culture is GregorianCalendar, Type Localized
// The current calendar for the ar-EG culture is GregorianCalendar, Type Localized
// The current calendar for the ar-EG culture is UmAlQuraCalendar
Imports System.Globalization
Module Example
Public Sub Main()
Dim ci As CultureInfo = CultureInfo.CreateSpecificCulture("ar-EG")
Console.WriteLine("The current calendar for the {0} culture is {1}",
ci.Name,
CalendarUtilities.ShowCalendarName(ci.DateTimeFormat.Calendar))
CalendarUtilities.ChangeCalendar(ci, New JapaneseCalendar())
Console.WriteLine("The current calendar for the {0} culture is {1}",
ci.Name,
CalendarUtilities.ShowCalendarName(ci.DateTimeFormat.Calendar))
CalendarUtilities.ChangeCalendar(ci, New UmAlQuraCalendar())
Console.WriteLine("The current calendar for the {0} culture is {1}",
ci.Name,
CalendarUtilities.ShowCalendarName(ci.DateTimeFormat.Calendar))
End Sub
End Module
Public Class CalendarUtilities
Private newCal As Calendar
Private isGregorian As Boolean
Public Shared Sub ChangeCalendar(ci As CultureInfo, cal As Calendar)
Dim util As New CalendarUtilities(cal)
' Is the new calendar already the current calendar?
If util.CalendarExists(ci.DateTimeFormat.Calendar) Then
Exit Sub
End If
' Is the new calendar supported?
If Array.Exists(ci.OptionalCalendars, AddressOf util.CalendarExists) Then
ci.DateTimeFormat.Calendar = cal
End If
End Sub
Private Sub New(cal As Calendar)
newCal = cal
' Is the new calendar a Gregorian calendar?
isGregorian = cal.GetType().Name.Contains("Gregorian")
End Sub
Private Function CalendarExists(cal As Calendar) As Boolean
If cal.ToString() = newCal.ToString Then
If isGregorian Then
If CType(cal, GregorianCalendar).CalendarType =
CType(newCal, GregorianCalendar).CalendarType Then
Return True
End If
Else
Return True
End If
End If
Return False
End Function
Public Shared Function ShowCalendarName(cal As Calendar) As String
Dim calName As String = cal.ToString().Replace("System.Globalization.", "")
If TypeOf cal Is GregorianCalendar Then
calName += ", Type " + CType(cal, GregorianCalendar).CalendarType.ToString()
End If
Return calName
End Function
End Class
' The example displays the following output:
' The current calendar for the ar-EG culture is GregorianCalendar, Type Localized
' The current calendar for the ar-EG culture is GregorianCalendar, Type Localized
' The current calendar for the ar-EG culture is UmAlQuraCalendar
備註
Calendar屬性只接受對與物件相關聯之文化特性有效的行事曆 DateTimeFormatInfo 。 CultureInfo.OptionalCalendars屬性指定可供特定文化特性使用的行事曆,而 CultureInfo.Calendar 屬性則指定文化特性的預設行事曆。
重要
日本曆法的紀元是以天皇的統治為基礎,因此有變更是正常的。 例如,2019 年 5 月 1 日之後,JapaneseCalendar 與 JapaneseLunisolarCalendar 中將開始使用「令和」。 此變更對使用這些日曆的所有應用程式都有影響。 如需詳細資訊,以及判斷您的應用程式是否受到影響,請參閱在 .net 的日文日曆中處理新的紀元。 如需在 Windows 系統上測試您的應用程式以確保其在年代的就緒狀態變更的相關資訊,請參閱準備您的應用程式以進行日本時代變更。 針對支援多個紀元日曆的 .NET 功能,以及在使用支援多個紀元的行事歷時的最佳作法,請參閱 使用紀元。
變更這個屬性的值也會影響下列屬性: MonthNames 、 AbbreviatedMonthNames 、 DayNames 、 AbbreviatedDayNames 、、、、、、 CalendarWeekRule FirstDayOfWeek FullDateTimePattern LongDatePattern ShortDatePattern YearMonthPattern 和 MonthDayPattern 。
例如,如果目前線程的文化特性是日文,則這個屬性會接受 JapaneseCalendar 、 Localized GregorianCalendar 或 USEnglish GregorianCalendar 。 使用時 JapaneseCalendar ,預設的完整日期規範為 "gg y ' \x5e74 ' \x6708 ' \x65e5 '"。 使用時 Localized GregorianCalendar ,預設的完整日期規範為 "yyyy ' \x5e74 ' \x6708 ' \x65e5 '"。