DateTimeFormatInfo.Calendar Propriété
Définition
Obtient ou définit le calendrier à utiliser pour la culture en cours.Gets or sets the calendar to use for the current culture.
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
Valeur de propriété
Calendrier à utiliser pour la culture en cours.The calendar to use for the current culture. La valeur par défaut de InvariantInfo est un objet GregorianCalendar.The default for InvariantInfo is a GregorianCalendar object.
Exceptions
La propriété est définie sur null
.The property is being set to null
.
Un objet Calendar, qui n’est pas valide pour la culture actuelle, est assigné à la propriété.The property is being set to a Calendar object that is not valid for the current culture.
La propriété est définie et l’objet DateTimeFormatInfo est en lecture seule.The property is being set and the DateTimeFormatInfo object is read-only.
Exemples
L’exemple suivant définit une ChangeCalendar
méthode qui modifie le calendrier actuel d’une culture en un calendrier spécifié, sauf s’il s’agit déjà du calendrier actuel ou s’il n’est pas pris en charge par la culture.The following example defines a ChangeCalendar
method that changes a culture's current calendar to a specified calendar unless it is already the current calendar or if it is not supported by the culture. Le code qui appelle la méthode instancie un CultureInfo objet qui représente la culture arabe (Égypte) et tente d’abord de remplacer son calendrier par le calendrier japonais.The code that calls the method instantiates a CultureInfo object that represents the Arabic (Egypt) culture and first attempts to change its calendar to the Japanese calendar. Étant donné que le calendrier japonais n’est pas pris en charge, la méthode ne modifie pas le calendrier de la culture.Because the Japanese calendar is not supported, the method makes not change the culture's calendar. Toutefois, étant donné que le calendrier Um al-Qura est membre de CultureInfo.OptionalCalendars la collection, la méthode parvient à en faire le calendrier actuel pour la culture ar-eg.However, because the Umm al-Qura calendar is a member of the CultureInfo.OptionalCalendars collection, the method does succeed in making it the current calendar for the ar-EG culture.
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
Remarques
La Calendar propriété accepte uniquement les calendriers qui sont valides pour la culture associée à DateTimeFormatInfo l’objet.The Calendar property accepts only calendars that are valid for the culture that is associated with the DateTimeFormatInfo object. La CultureInfo.OptionalCalendars propriété spécifie les calendriers qui peuvent être utilisés par une culture particulière, CultureInfo.Calendar et la propriété spécifie le calendrier par défaut pour la culture.The CultureInfo.OptionalCalendars property specifies the calendars that can be used by a particular culture, and the CultureInfo.Calendar property specifies the default calendar for the culture.
Important
Les ères des calendriers japonais sont basées sur le règne de l’empereur et sont donc amenées à changer.Eras in the Japanese calendars are based on the emperor's reign and are therefore expected to change. Par exemple, le 1 mai 2019 a marqué le début de l’ère Reiwa dans JapaneseCalendar et JapaneseLunisolarCalendar.For example, May 1, 2019 marked the beginning of the Reiwa era in the JapaneseCalendar and JapaneseLunisolarCalendar. Ce changement d’ère affecte toutes les applications qui utilisent ces calendriers.Such a change of era affects all applications that use these calendars. Pour plus d’informations et pour déterminer si vos applications sont affectées, consultez Handling a new era in the Japanese calendar in .NET.See Handling a new era in the Japanese calendar in .NET for more information and to determine whether your applications are affected. Pour plus d’informations sur le test de vos applications sur les systèmes Windows pour garantir leur préparation au changement d’ère, consultez Préparer votre application au changement de l’ère japonaise.See Prepare your application for the Japanese era change for information on testing your applications on Windows systems to ensure their readiness for the era change. Consultez Utilisation des ères pour connaître les fonctionnalités de .NET qui prennent en charge les calendriers avec plusieurs ères et les bonnes pratiques d’utilisation de ce type de calendrier.See Working with eras for features in .NET that support calendars with multiple eras and for best practices when working with calendars that support multiple eras.
La modification de la valeur de cette propriété affecte également les propriétés suivantes MonthNames: AbbreviatedMonthNames, DayNames, AbbreviatedDayNames, CalendarWeekRule, FirstDayOfWeek, FullDateTimePattern, LongDatePattern, ShortDatePattern, YearMonthPattern , , et MonthDayPattern.Changing the value of this property affects the following properties as well: MonthNames, AbbreviatedMonthNames, DayNames, AbbreviatedDayNames, CalendarWeekRule, FirstDayOfWeek, FullDateTimePattern, LongDatePattern, ShortDatePattern, YearMonthPattern, and MonthDayPattern.
Par exemple, si la culture du thread actuel est japonaise, cette propriété accepte JapaneseCalendar, Localized GregorianCalendarou USEnglish. GregorianCalendarFor example, if the culture of the current thread is Japanese, this property accepts JapaneseCalendar, LocalizedGregorianCalendar, or USEnglishGregorianCalendar. JapaneseCalendar Lorsque est utilisé, le spécificateur de date longue par défaut est « GG y' \x5e74 ' \x6708 ' \x65e5 ' ».When the JapaneseCalendar is used, the default long date specifier is "gg y'\x5e74'M'\x6708'd'\x65e5'". Lorsque la Localized GregorianCalendarvaleur est utilisée, le spécificateur de date longue par défaut est « yyyy » \x5e74 « \x6708 ' \x65e5 ».When the LocalizedGregorianCalendar, is used, the default long date specifier is "yyyy'\x5e74'M'\x6708'd'\x65e5'".