DateTimeFormatInfo.Calendar 属性

定义

获取或设置用于当前区域性的日历。

public System.Globalization.Calendar Calendar { get; set; }

属性值

用于当前区域性的日历。 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

注解

属性 Calendar 仅接受对与 DateTimeFormatInfo 对象关联的区域性有效的日历。 属性 CultureInfo.OptionalCalendars 指定特定区域性可以使用的日历,属性 CultureInfo.Calendar 指定区域性的默认日历。

重要

日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。 例如,2019 年 5 月 1 日在 JapaneseCalendarJapaneseLunisolarCalendar 中标志着令和年号的开始。 这种年号的变化会影响使用这些日历的所有应用程序。 有关详细信息并确定应用程序是否受到影响,请参阅 在 .NET 中处理日语日历中的新纪元。 有关在 Windows 系统上测试应用程序以确保其准备好进行纪元更改的信息,请参阅 准备应用程序应对日本纪元更改。 有关 .NET 中支持具有多个纪元的日历的功能以及使用支持多个纪元的日历时的最佳做法,请参阅 使用纪元

更改此属性的值也会影响以下属性:MonthNames、、、AbbreviatedMonthNamesFirstDayOfWeekFullDateTimePatternDayNamesAbbreviatedDayNamesCalendarWeekRuleLongDatePatternShortDatePattern、、 YearMonthPattern和 。MonthDayPattern

例如,如果当前线程的区域性为日语,则此属性接受 JapaneseCalendarGregorianCalendarLocalizedUSEnglishGregorianCalendarJapaneseCalendar使用 时,默认的长日期说明符为“gg y'\x5e74'M'\x6708'd'\x65e5'”。 Localized GregorianCalendar使用 时,默认的长日期说明符为“yyyy'\x5e74'M'\x6708'd'\x65e5'”。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

另请参阅