Share via


Utilisation de calendriers pour des cultures spécifiques

Mise à jour : novembre 2007

Une application globalisée doit être en mesure d'afficher et d'utiliser des calendriers en fonction de la culture en cours. .NET Framework fournit la classe Calendar aussi bien que les implémentations de classe suivantes :

La classe CultureInfo a une propriété Calendar qui spécifie le calendrier par défaut pour une culture. Certaines cultures prennent en charge plusieurs calendriers. La propriété OptionalCalendars spécifie les calendriers facultatifs pris en charge par cette culture.

L'exemple de code suivant crée des objets CultureInfo pour les cultures Thaï (Thaïlande) désignée « th-TH » et Japonais (Japon) désigné « ja-JP ». L'exemple affiche les calendriers par défaut et facultatifs pour chaque culture. Notez que l'objet GregorianCalendar se divise en sous-types. La propriété CalendarType spécifie le sous-type du calendrier grégorien. Dans cet exemple, la valeur de CalendarType est récupérée et affichée chaque fois que le calendrier est identifié comme grégorien. Pour une liste et des explications des valeurs possibles pour CalendarType, consultez GregorianCalendarTypes.

Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Public Class TestClass   
   
   Public Shared Sub Main()
      ' Creates a CultureInfo for Thai in Thailand.
      Dim th As New CultureInfo("th-TH")
      DisplayCalendars(th)

      ' Creates a CultureInfo for Japanese in Japan.
      Dim ja As New CultureInfo("ja-JP")
      DisplayCalendars(ja)
   End Sub

   Protected Shared Sub DisplayCalendars(cultureinfo As CultureInfo)
      Dim ci As New CultureInfo(cultureinfo.ToString())
      
      ' Displays the default calendar for the culture.
      If TypeOf ci.Calendar Is GregorianCalendar Then
         Console.WriteLine(ControlChars.Newline + ControlChars.Newline + _
         "The default calendar for the {0} culture is:" + _
         ControlChars.Newline + " {1}" + ControlChars.Newline + _
         ControlChars.Newline, ci.DisplayName.ToString(), _
         ci.Calendar.ToString() + " subtype" + CType(ci.Calendar, _
         GregorianCalendar).CalendarType.ToString())
      Else
         Console.WriteLine(ControlChars.Newline + ControlChars.Newline + _
            "The default calendar for the {0} culture is:" + _
            ControlChars.Newline + "{1}" + ControlChars.Newline + _
            ControlChars.Newline, ci.DisplayName.ToString(),_
            ci.Calendar.ToString())
      End If
      
      ' Displays the optional calendars for the culture.
      Console.WriteLine("The optional calendars for the {0} culture are: _
         ", ci.DisplayName.ToString())
      Dim i As Integer
      For i = ci.OptionalCalendars.GetLowerBound(0) To _
         ci.OptionalCalendars.GetUpperBound(0)

         If TypeOf ci.OptionalCalendars(i) Is GregorianCalendar Then
            ' Displays the calendar subtype.
            Dim CalStr As [String] = ci.OptionalCalendars(i).ToString() _
               + " subtype " + CType(ci.OptionalCalendars(i), _
               GregorianCalendar).CalendarType.ToString()
            Console.WriteLine(CalStr)
         Else
            Console.WriteLine(ci.OptionalCalendars(i).ToString())
         End If
      Next i 
   End Sub
End Class
using System;
using System.Globalization;

public class TestClass
{
   public static void Main()
   {
      // Creates a CultureInfo for Thai in Thailand.
      CultureInfo th= new CultureInfo("th-TH");
      DisplayCalendars(th);
      
      // Creates a CultureInfo for Japanese in Japan.
      CultureInfo ja= new CultureInfo("ja-JP");
      DisplayCalendars(ja);
      
   }

   protected static void DisplayCalendars(CultureInfo cultureinfo)
   {
      CultureInfo ci = new CultureInfo(cultureinfo.ToString());
      
      // Displays the default calendar for the culture.
      if (ci.Calendar is GregorianCalendar)   
         Console.WriteLine ("\n\nThe default calendar for the {0} culture 
            is:\n {1}\n\n", ci.DisplayName.ToString(), 
            ci.Calendar.ToString() + " subtype " + 
            ((GregorianCalendar)ci.Calendar).CalendarType.ToString());
      else
         Console.WriteLine ("\n\nThe default calendar for the {0} culture 
            is: \n{1}\n\n", ci.DisplayName.ToString(), 
            ci.Calendar.ToString());

      // Displays the optional calendars for the culture.
      Console.WriteLine ("The optional calendars for the {0} culture are: 
         ", ci.DisplayName.ToString());
         for (int i = ci.OptionalCalendars.GetLowerBound(0); i <= 
               ci.OptionalCalendars.GetUpperBound(0); i++ )
         {
            if (ci.OptionalCalendars[i] is GregorianCalendar)
            {
               // Displays the calendar subtype.
               String CalStr = (ci.OptionalCalendars[i].ToString() + " 
                  subtype " + ((GregorianCalendar)ci.OptionalCalendars[i]).CalendarType.ToString());
               Console.WriteLine(CalStr);
            }
            else 
               Console.WriteLine (ci.OptionalCalendars[i].ToString());
         }
   }
}

Ce code génère la sortie suivante :

The default calendar for the Thai (Thailand) culture is: 
System.Globalization.ThaiBuddhistCalendar

The optional calendars for the Thai (Thailand) culture are: 
System.Globalization.ThaiBuddhistCalendar
System.Globalization.GregorianCalendar subtype Localized

The default calendar for the Japanese (Japan) culture is:
System.Globalization.GregorianCalendar subtype Localized

The optional calendars for the Japanese (Japan) culture are: 
System.Globalization.JapaneseCalendar
System.Globalization.GregorianCalendar subtype USEnglish
System.Globalization.GregorianCalendar subtype Localized

L'exemple de code suivant montre comment des méthodes semblables de structure DateTime et de classe Calendar peuvent retourner des résultats différents pour une même culture. Pour le thread, la culture CurrentCulture a la valeur « he-IL » (hébreux en Israël), et le calendrier en cours est défini comme le calendrier hébreu. Un type DateTime est créé et initialisé. Ensuite, des membres de DateTime et de Calendar sont utilisés pour retourner le jour, le mois, l'année et le nombre de mois de l'année, et ces valeurs sont affichées. Seules les méthodes de classe Calendar retournent le jour, mois, année et nombre de mois dans l'année basée sur le calendrier hébreu. Les méthodes DateTime utilisent toujours le calendrier grégorien pour exécuter les calculs, indépendamment du calendrier actuel.

Imports System
Imports System.Threading
Imports System.Globalization
Imports Microsoft.VisualBasic

Public Class TestClass

   Public Shared Sub Main()
      ' Creates a CultureInfo for Hebrew in Israel.
      Dim he As New CultureInfo("he-IL")
      he.DateTimeFormat.Calendar = New HebrewCalendar()
      Console.WriteLine(ControlChars.Newline + ControlChars.Newline _
         + "The current calendar set for the {0} culture is:" + _
         ControlChars.Newline + " {1}", he.DisplayName.ToString(), _
         he.DateTimeFormat.Calendar.ToString())
      Dim dt As New DateTime(5760, 11, 4, he.DateTimeFormat.Calendar)
      Console.WriteLine(ControlChars.Newline + " The DateTime _
         returns the day as: {0}", dt.Day)
      Console.WriteLine(ControlChars.Newline + " The Calendar class _
         returns the day as: {0}", _
         he.DateTimeFormat.Calendar.GetDayOfMonth(dt))
      Console.WriteLine(ControlChars.Newline + " The DateTime _
         returns the month as: {0}", dt.Month)
      Console.WriteLine(ControlChars.Newline + " The Calendar class _
         returns the month as: {0}", _
         he.DateTimeFormat.Calendar.GetMonth(dt))
      Console.WriteLine(ControlChars.Newline + " The DateTime _
         returns the Year as: {0}", dt.Year)
      Console.WriteLine(ControlChars.Newline + " The Calendar class _
         returns the Year as: {0}", _
         he.DateTimeFormat.Calendar.GetYear(dt))
      Console.WriteLine(ControlChars.Newline + " The Calendar class _
         returns the number of months in the year {0} as: {1}", _
         he.DateTimeFormat.Calendar.GetYear(dt), _
         he.DateTimeFormat.Calendar.GetMonthsInYear _
        (he.DateTimeFormat.Calendar.GetYear(dt))
      Console.WriteLine(ControlChars.Newline + " The DateTime does _
         not return the number of months in a year " + _
         ControlChars.Newline + " because it uses the Gregorian _
         calendar, which always has twelve months.")
   End Sub
End Class
using System;
using System.Threading;
using System.Globalization;

public class TestClass
{
   public static void Main()
   {
      // Creates a CultureInfo for Hebrew in Israel.
      CultureInfo he= new CultureInfo("he-IL");
      Thread.CurrentThread.CurrentCulture = he;
      he.DateTimeFormat.Calendar = new HebrewCalendar();
      Console.WriteLine ("\n\nThe current calendar set for the {0} culture 
         is:\n {1}", he.DisplayName.ToString(), 
         he.DateTimeFormat.Calendar.ToString());
      DateTime dt = new DateTime(5760, 11, 4, he.DateTimeFormat.Calendar);
      Console.WriteLine ("\nThe DateTime returns the day as: {0}", 
            dt.Day);
      Console.WriteLine ("\nThe Calendar class returns the day as: {0}", 
            he.DateTimeFormat.Calendar.GetDayOfMonth(dt));
      Console.WriteLine ("\nThe DateTime returns the month as: 
            {0}", dt.Month);
      Console.WriteLine ("\nThe Calendar class returns the month as: 
            {0}", he.DateTimeFormat.Calendar.GetMonth(dt));
      Console.WriteLine ("\nThe DateTime returns the year as: {0}", 
            dt.Year);
      Console.WriteLine ("\nThe Calendar class returns the year as: {0}", 
            he.DateTimeFormat.Calendar.GetYear(dt));      
      Console.WriteLine ("\nThe Calendar class returns the number of 
            months in the year {0} as: 
            {1}",he.DateTimeFormat.Calendar.GetYear(dt), 
            he.DateTimeFormat.Calendar.GetMonthsInYear
            (he.DateTimeFormat.Calendar.GetYear(dt)));
      Console.WriteLine ("\nThe DateTime does not return the number 
            of months in a year \nbecause it uses the Gregorian calendar, 
            which always has twelve months.");
   }
}

Ce code génère la sortie suivante :

The current calendar set for the Hebrew (Israel) culture is:
 System.Globalization.HebrewCalendar

The DateTime returns the day as: 7

The Calendar class returns the day as: 4

The DateTime returns the month as: 7

The Calendar class returns the month as: 11

The DateTime returns the year as: 2000

The Calendar class returns the year as: 5760

The Calendar class returns the number of months in the year 5760 as: 13

The DateTime does not return the number of months in a year 
because it uses the Gregorian calendar, which always has twelve months.

L'exemple de code suivant montre comment les valeurs retournées pour le mois, le jour et l'année en cours peuvent différer selon le calendrier en cours défini pour une culture spécifiée. Pour le thread, la culture CurrentCulture a la valeur « ja-JP » et le calendrier comme JapaneseCalendar. Le jour, le mois et l'année sont retournés et affichés. Ensuite, le calendrier a la valeur GregorianCalendaret le jour, le mois et l'année sont retournés et affichés. Notez comment l'année diffère selon le calendrier actif. Le calendrier japonais retourne l'année 13, tandis que le calendrier grégorien retourne l'année 2001.

Imports System
Imports System.Threading
Imports System.Globalization
Imports Microsoft.VisualBasic 

Public Class TestClass   
   
   Public Shared Sub Main()
      Dim dt As DateTime = DateTime.Now
      
      ' Creates a CultureInfo for Japanese in Japan.
      Dim jp As New CultureInfo("ja-JP")
      
      jp.DateTimeFormat.Calendar = New JapaneseCalendar()
      Console.WriteLine(ControlChars.Newline + ControlChars.Newline + _
         "The current calendar set for the {0} culture is:" + _
         ControlChars.Newline + " {1}", jp.DisplayName.ToString(), _
         jp.DateTimeFormat.Calendar.ToString())
      Console.WriteLine(ControlChars.Newline + " The day is: {0}", _
         jp.DateTimeFormat.Calendar.GetDayOfMonth(dt))
      Console.WriteLine(ControlChars.Newline + " The month is: {0}", _
         jp.DateTimeFormat.Calendar.GetMonth(dt))
      Console.WriteLine(ControlChars.Newline + " The year is: {0}", _
         jp.DateTimeFormat.Calendar.GetYear(dt))

      jp.DateTimeFormat.Calendar = New GregorianCalendar()
      Console.WriteLine(ControlChars.Newline + ControlChars.Newline + _
         "The current calendar set for the {0} culture is:" + _
         ControlChars.Newline + " {1}", jp.DisplayName.ToString(), _
         jp.DateTimeFormat.Calendar.ToString())
      Console.WriteLine(ControlChars.Newline + " The day is: {0}", _
         jp.DateTimeFormat.Calendar.GetDayOfMonth(dt))
      Console.WriteLine(ControlChars.Newline + " The month is: {0}", _
         jp.DateTimeFormat.Calendar.GetMonth(dt))
      Console.WriteLine(ControlChars.Newline + " The year is: {0}", _
         jp.DateTimeFormat.Calendar.GetYear(dt))
   End Sub
End Class
using System;
using System.Threading;
using System.Globalization;

public class TestClass
{
   public static void Main()
   {
      DateTime dt = DateTime.Now;

      // Creates a CultureInfo for Japanese in Japan.
      CultureInfo jp = new CultureInfo("ja-JP");
      Thread.CurrentThread.CurrentCulture = jp;   

      jp.DateTimeFormat.Calendar = new JapaneseCalendar();
      Console.WriteLine ("\n\nThe current calendar set for the {0} culture 
            is:\n {1}", jp.DisplayName.ToString(), 
            jp.DateTimeFormat.Calendar.ToString());

      Console.WriteLine ("\nThe day is: {0}", 
            jp.DateTimeFormat.Calendar.GetDayOfMonth(dt));
      Console.WriteLine ("\nThe month is: {0}", 
            jp.DateTimeFormat.Calendar.GetMonth(dt));
      Console.WriteLine ("\nThe year is: {0}",
            jp.DateTimeFormat.Calendar.GetYear(dt));

      jp.DateTimeFormat.Calendar = new GregorianCalendar();
      Console.WriteLine ("\n\nThe current calendar set for the {0} culture 
            is:\n {1}", jp.DisplayName.ToString(), 
            jp.DateTimeFormat.Calendar.ToString());

      Console.WriteLine ("\nThe day is: {0}", 
            jp.DateTimeFormat.Calendar.GetDayOfMonth(dt));
      Console.WriteLine ("\nThe month is: {0}", 
            jp.DateTimeFormat.Calendar.GetMonth(dt));
      Console.WriteLine ("\nThe year is: {0}", 
            jp.DateTimeFormat.Calendar.GetYear(dt));
   }
}

Ce code génère la sortie suivante :

The current calendar set for the Japanese (Japan) culture is:
System.Globalization.JapaneseCalendar
The day is: 3
The month is: 8
The year is: 13

The current calendar set for the Japanese (Japan) culture is:
System.Globalization.GregorianCalendar
The day is: 3
The month is: 8
The year is: 2001

Voir aussi

Concepts

Mise en forme de la date et de l'heure pour une culture spécifique

Autres ressources

Codage et localisation