DateTimeFormatInfo.Calendar Vlastnost

Definice

Získá nebo nastaví kalendář pro použití pro aktuální jazykovou verzi.

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

Hodnota vlastnosti

Kalendář, který se má použít pro aktuální jazykovou verzi. Výchozí hodnota pro InvariantInfo je GregorianCalendar objekt.

Výjimky

Vlastnost se nastavuje na null.

Vlastnost se nastavuje na Calendar objekt, který není platný pro aktuální jazykovou verzi.

Vlastnost se nastavuje a DateTimeFormatInfo objekt je jen pro čtení.

Příklady

Následující příklad definuje metodu ChangeCalendar , která změní aktuální kalendář jazykové verze na zadaný kalendář, pokud již není aktuální kalendář nebo pokud není podporován jazykovou verzí. Kód, který volá metodu, vytvoří instanci objektu CultureInfo , který představuje jazyk arabštiny (Egypt), a nejprve se pokusí změnit jeho kalendář na japonský kalendář. Vzhledem k tomu, že japonský kalendář není podporován, metoda nezmění kalendář jazykové verze. Protože je však kalendář Umm al-Qura členem CultureInfo.OptionalCalendars kolekce, metoda úspěšně vytvoří aktuální kalendář pro kulturu 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

Poznámky

Vlastnost Calendar přijímá pouze kalendáře, které jsou platné pro jazykovou verzi, která je přidružena k objektu DateTimeFormatInfo . Vlastnost CultureInfo.OptionalCalendars určuje kalendáře, které mohou být použity konkrétní jazykovou verzí, a CultureInfo.Calendar vlastnost určuje výchozí kalendář pro jazykovou verzi.

Důležité

Éry v japonských kalendářích jsou založeny na vládě císaře, a proto se očekává, že se změní. Například 1. květen 2019 označil začátek éry Reiwa v JapaneseCalendar éře a JapaneseLunisolarCalendar. Taková změna éry ovlivní všechny aplikace, které tyto kalendáře používají. Další informace a určení, jestli se to týká vašich aplikací, najdete v tématu Zpracování nové éry v japonském kalendáři v .NET. Informace o testování aplikací v systémech Windows pro zajištění jejich připravenosti na změnu éry najdete v tématu Příprava aplikace na změnu japonské éry. Informace o funkcích v .NET, které podporují kalendáře s více érami, a osvědčené postupy při práci s kalendáři, které podporují více ér, najdete v tématu Práce s érami.

Změna hodnoty této vlastnosti ovlivní také následující vlastnosti: MonthNames, AbbreviatedMonthNames, DayNames, AbbreviatedDayNames, FirstDayOfWeekCalendarWeekRule, , FullDateTimePattern, , LongDatePattern, , ShortDatePattern, YearMonthPattern, a MonthDayPattern.

Pokud je například jazyková verze aktuálního vlákna japonská, tato vlastnost přijímá JapaneseCalendar, LocalizedGregorianCalendarnebo USEnglishGregorianCalendar. JapaneseCalendar Při použití je výchozí specifikátor dlouhého data "gg y'\x5e74'M'\x6708'd'\x65e5". LocalizedGregorianCalendarPři použití je výchozí specifikátor dlouhého data "yyyy'\x5e74'M'\x6708'd'\x65e5'".

Platí pro

Viz také