Sdílet prostřednictvím


DateTimeFormatInfo.Calendar Vlastnost

Definice

Získá nebo nastaví kalendář 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 je InvariantInfoGregorianCalendar objekt.

Výjimky

Vlastnost je nastavena na nullhodnotu .

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

Vlastnost je nastavena 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 ještě není aktuálním kalendářem nebo není podporován jazykovou verzí. Kód, který volá metodu, vytvoří CultureInfo instanci objektu, který představuje arabskou jazykovou verzi (Egypt) a nejprve se pokusí změnit svůj 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ář Um al-Qura členem CultureInfo.OptionalCalendars kolekce, metoda úspěšně vytvoří aktuální kalendář pro ar-EG jazykovou verzi.

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 platné pro jazykovou verzi přidruženou k objektu DateTimeFormatInfo . Vlastnost CultureInfo.OptionalCalendars určuje kalendáře, které lze použít pro konkrétní jazykovou verzi, 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ětna 2019 označilo začátek období Reiwa v JapaneseCalendar a JapaneseLunisolarCalendar. Taková změna éry ovlivňuje všechny aplikace, které tyto kalendáře používají. Další informace a určení, zda jsou ovlivněny vaše aplikace, naleznete v tématu Zpracování nové éry v japonském kalendáři v .NET. Informace o testování aplikací v systémech Windows za účelem zajištění připravenosti na změnu v éře najdete v tématu Příprava aplikace na změnu japonské éry. Funkce v .NET, které podporují kalendáře s více obdobími a osvědčené postupy při práci s kalendáři, které podporují více období, najdete v tématu Práce s obdobími.

Změna hodnoty této vlastnosti má vliv také na následující vlastnosti: , , , , AbbreviatedDayNames, CalendarWeekRuleFirstDayOfWeek, FullDateTimePattern, LongDatePatternShortDatePattern, YearMonthPattern, a MonthDayPattern. DayNamesAbbreviatedMonthNamesMonthNames

Pokud je například jazyková verze aktuálního vlákna japonština, 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". Localized GregorianCalendarPokud se použije , výchozí specifikátor dlouhého data je "yyyy'\x5e74'M'\x6708'd'\x65e5".

Platí pro

Viz také