DateTimeFormatInfo.Calendar Właściwość

Definicja

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ść domyślna InvariantInfoGregorianCalendar to obiekt.

Wyjątki

Właściwość jest ustawiana na null.

Właściwość jest ustawiana na Calendar obiekt, który jest nieprawidł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ę ChangeCalendar , która zmienia bieżący kalendarz kultury na określony kalendarz, chyba że jest już bieżącym kalendarzem lub jeśli nie jest obsługiwana przez kulturę. Kod wywołujący metodę tworzy wystąpienie obiektu reprezentującego kulturę CultureInfo arabską (Egipt) i najpierw próbuje zmienić kalendarz na japoński kalendarz. Ponieważ kalendarz japoński nie jest obsługiwany, metoda nie zmienia kalendarza kultury. Jednak ponieważ kalendarz um al-Qura jest członkiem CultureInfo.OptionalCalendars kolekcji, metoda kończy się powodzeniem w tworzeniu bieżącego kalendarza dla kultury 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 skojarzonej z obiektem DateTimeFormatInfo . Właściwość CultureInfo.OptionalCalendars określa kalendarze, które mogą być używane przez określoną kulturę, a CultureInfo.Calendar właściwość określa domyślny kalendarz kultury.

Ważne

Era w japońskich kalendarzach opiera się na panowaniu cesarza i dlatego oczekuje się, że się zmieni. Na przykład 1 maja 2019 r. oznaczyła początek ery Reiwa w JapaneseCalendar erze i JapaneseLunisolarCalendar. Taka zmiana ery wpływa na wszystkie aplikacje korzystające z tych kalendarzy. Aby uzyskać więcej informacji i określić, czy twoje aplikacje mają wpływ, zobacz Obsługa nowej ery w kalendarzu japońskim na platformie .NET. Aby uzyskać informacje na temat testowania aplikacji w systemach Windows w celu zapewnienia gotowości do zmiany epoki, zobacz Przygotowywanie aplikacji do japońskiej zmiany epoki. Aby uzyskać informacje o funkcjach na platformie .NET, które obsługują kalendarze z wieloma erami i najlepsze rozwiązania dotyczące pracy z kalendarzami obsługującymi wiele epok, zobacz Praca z erami.

Zmiana wartości tej właściwości wpływa również na następujące właściwości: MonthNames, DayNamesCalendarWeekRuleFirstDayOfWeekAbbreviatedDayNamesAbbreviatedMonthNamesLongDatePatternShortDatePatternFullDateTimePatternYearMonthPatterni .MonthDayPattern

Jeśli na przykład kultura bieżącego wątku jest japońska, ta właściwość akceptuje JapaneseCalendarwartości , LocalizedGregorianCalendarlub USEnglishGregorianCalendar. JapaneseCalendar Gdy jest używany, domyślny specyfikator daty długiej to "gg y'\x5e74'M'\x6708'd'\x65e5'". LocalizedGregorianCalendarGdy parametr jest używany, domyślny specyfikator daty długiej to "yyyy'\x5e74'M'\x6708'd'\x65e5'".

Dotyczy

Zobacz też