Language

DateTimeFormatInfo.Calendar プロパティ

定義

現在のカルチャに使用するカレンダーを取得または設定します。

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

プロパティ値

現在のカルチャに使用するカレンダー。 InvariantInfoの既定値は、GregorianCalendar オブジェクトです。

例外

プロパティは nullに設定されています。

プロパティは、現在のカルチャに対して無効な Calendar オブジェクトに設定されています。

プロパティが設定されており、 DateTimeFormatInfo オブジェクトは読み取り専用です。

例

次の例では、カルチャの現在の予定表が既に現在の予定表であるか、カルチャでサポートされていない場合を除き、指定した予定表に変更する ChangeCalendar メソッドを定義します。 このメソッドを呼び出すコードは、アラビア語 (エジプト) カルチャを表す CultureInfo オブジェクトをインスタンス化し、最初にそのカレンダーを日本語カレンダーに変更しようとします。 日本語カレンダーはサポートされていないため、このメソッドはカルチャのカレンダーを変更しません。 ただし、um al-Qura カレンダーは CultureInfo.OptionalCalendars コレクションのメンバーであるため、メソッドは 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

注釈

Calendar プロパティは、DateTimeFormatInfo オブジェクトに関連付けられているカルチャに対して有効なカレンダーのみを受け入れます。 CultureInfo.OptionalCalendars プロパティは、特定のカルチャで使用できるカレンダーを指定し、CultureInfo.Calendar プロパティはカルチャの既定のカレンダーを指定します。

Important

日本暦の年号は天皇の治世に基づいているため、変化することが期待されます。 たとえば、2019 年 5 月 1 日は、 JapaneseCalendar と JapaneseLunisolarCalendarのレイワ時代の始まりを迎えています。 このような時代の変化は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細およびアプリケーションが影響を受けるかどうかを判断するには、「 .NET の日本語カレンダーでの新しい時代 (年号) の処理」を参照してください。 Windows システムでアプリケーションをテストし、時代 (年号) の変更に対する準備を行う方法については、「 日本の時代 (年号) に合わせてアプリケーションを準備する」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。

このプロパティの値を変更すると、 MonthNames、 AbbreviatedMonthNames、 DayNames、 AbbreviatedDayNames、 CalendarWeekRule、 FirstDayOfWeek、 FullDateTimePattern、 LongDatePattern、 ShortDatePattern、 YearMonthPattern、および MonthDayPatternも影響を受けます。

たとえば、現在のスレッドのカルチャが日本語の場合、このプロパティは JapaneseCalendar、 LocalizedGregorianCalendar、または USEnglishGregorianCalendarを受け入れます。 JapaneseCalendarを使用する場合、既定の長い日付指定子は "gg y'\x5e74'M'\x6708'd'\x65e5'" です。 Localized GregorianCalendarが使用されている場合、既定の長い日付指定子は "yyyy'\x5e74'M'\x6708'd'\x65e5'" です。

適用対象

こちらもご覧ください