DateTimeFormatInfo.Calendar Właściwość
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Pobiera lub ustawia kalendarz do użycia dla bieżącej kultury.
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
Wartość właściwości
Kalendarz do użycia dla bieżącej kultury. Wartością InvariantInfo domyślną dla jest GregorianCalendar obiekt .
Wyjątki
Właściwość jest ustawiana na null
wartość .
Właściwość jest ustawiana na Calendar obiekt, który nie jest prawidłowy dla bieżącej kultury.
Właściwość jest ustawiana, a DateTimeFormatInfo obiekt jest tylko do odczytu.
Przykłady
W poniższym przykładzie zdefiniowano metodę, która zmienia bieżący kalendarz kultury na określony kalendarz, chyba że jest to już bieżący kalendarz lub jeśli nie jest on obsługiwany ChangeCalendar
przez kulturę. Kod, który wywołuje metodę, inaucjuje obiekt reprezentujący kulturę arabski (Wdowie) i najpierw próbuje zmienić swój kalendarz na CultureInfo kalendarz japoński. Ponieważ kalendarz japoński nie jest obsługiwany, metoda sprawia, że kalendarz kultury nie jest zmieniany. Jednak ponieważ kalendarz um al-Qura jest członkiem kolekcji, metoda pomyślnie czyni go bieżącym kalendarzem dla kultury CultureInfo.OptionalCalendars ar-EG.
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
Uwagi
Właściwość Calendar akceptuje tylko kalendarze, które są prawidłowe dla kultury, która jest skojarzona z DateTimeFormatInfo obiektem. Właściwość określa kalendarze, które mogą być używane przez określoną kulturę, a właściwość CultureInfo.OptionalCalendars określa kalendarz domyślny dla CultureInfo.Calendar kultury.
Ważne
Ery w kalendarzach japońskich są oparte na suchem rzeszy i w związku z tym oczekuje się, że zmienią się. Na przykład 1 maja 2019 r. oznaczał początek ery Reiwa w i JapaneseCalendar JapaneseLunisolarCalendar . Taka zmiana ery ma wpływ na wszystkie aplikacje, które używają tych kalendarzy. Aby uzyskać więcej informacji i określić, czy dotyczy to aplikacji, zobacz Obsługa nowej ery w kalendarzu japońskim na platformie .NET. Aby uzyskać informacje na temat testowania aplikacji w Windows w celu zapewnienia ich gotowości na zmianę ery, zobacz Przygotowanie aplikacji na zmianę ery japońskiej. Aby uzyskać informacje o funkcjach na platformie .NET, które obsługują kalendarze z wieloma erami, oraz o najlepszych rozwiązaniach podczas pracy z kalendarzami, które obsługują wiele er, zobacz Praca z erami.
Zmiana wartości tej właściwości ma również wpływ na następujące właściwości: MonthNames , , , , , , , , , AbbreviatedMonthNames i DayNames AbbreviatedDayNames CalendarWeekRule FirstDayOfWeek FullDateTimePattern LongDatePattern ShortDatePattern YearMonthPattern MonthDayPattern .
Jeśli na przykład kultura bieżącego wątku to japoński, ta właściwość akceptuje JapaneseCalendar wartości Localized GregorianCalendar , lub USEnglish GregorianCalendar . Gdy jest używany, domyślny specyfikator JapaneseCalendar daty długiej to "gg y'\x5e74'M'\x6708'd'\x65e5'". Gdy używany jest znak , domyślny specyfikator daty długiej Localized GregorianCalendar to "yyyy'\x5e74'M'\x6708'd'\x65e5'".