使用日历

更新:2010 年 9 月

尽管日期和时间值表示时间中的某一刻,但其字符串表示形式区分区域性,并取决于特定区域性显示日期和时间值所用的约定以及该区域性使用的日历。 本主题研究 .NET Framework 中对日历的支持,并探讨在使用日期值时如何使用日历类。

.NET Framework 中的日历

.NET Framework 中的所有日历均派生自 System.Globalization.Calendar 类,该类提供了基本日历实现。 从 Calendar 类继承的类之一是 EastAsianLunisolarCalendar 类,该类是所有阴阳历的基类。 .NET Framework 包括以下日历实现:

可通过以下两种方法之一来使用日历:

  • 作为特定区域性使用的日历。 每个 CultureInfo 对象都有当前日历,这是该对象当前使用的日历。 所有日期和时间值的字符串表示形式会自动反映当前的区域性及其当前日历。 通常,当前的日历是该区域性的默认日历。 CultureInfo 对象还具有可选日历,其中包括该区域性可以使用的其他日历。

  • 作为与特定区域性无关的独立日历。 在这种情况下,Calendar 方法用于将日期表示为反映日历的值。

请注意,以下六个日历类只能用作独立日历:ChineseLunisolarCalendarJapaneseLunisolarCalendarJulianCalendarKoreanLunisolarCalendarPersianCalendarTaiwanLunisolarCalendar。 任何区域性都不将这些日历用作默认日历或可选日历。

日历和区域性

每个区域性都有默认日历,由 CultureInfo.Calendar 属性定义。 CultureInfo.OptionalCalendars 属性可返回 Calendar 对象的数组,该数组指定特定区域性支持的所有日历,包括该区域性的默认日历。

下面的示例演示 CultureInfo.CalendarCultureInfo.OptionalCalendars 属性。 该示例为泰语(泰国)和日语(日本)区域性创建 CultureInfo 对象,并显示它们的默认和可选日历。 请注意,在这两种情况下,区域性的默认日历也包括在 CultureInfo.OptionalCalendars 集合中。

Imports System.Globalization

Public Module Example
   Public Sub Main()
      ' Create a CultureInfo for Thai in Thailand.
      Dim th As CultureInfo = CultureInfo.CreateSpecificCulture("th-TH")
      DisplayCalendars(th)

      ' Create a CultureInfo for Japanese in Japan.
      Dim ja As CultureInfo = CultureInfo.CreateSpecificCulture("ja-JP")
      DisplayCalendars(ja)
   End Sub

   Sub DisplayCalendars(ci As CultureInfo)
      Console.WriteLine("Calendars for the {0} culture:", ci.Name)

      ' Get the culture's default calendar.
      Dim defaultCalendar As Calendar = ci.Calendar
      Console.Write("   Default Calendar: {0}", GetCalendarName(defaultCalendar))      

      If TypeOf defaultCalendar Is GregorianCalendar Then
         Console.WriteLine(" ({0})", 
                           CType(defaultCalendar, GregorianCalendar).CalendarType)
      Else
         Console.WriteLine()
      End If

      ' Get the culture's optional calendars.
      Console.WriteLine("   Optional Calendars:")      
      For Each optionalCalendar In ci.OptionalCalendars
         Console.Write("{0,6}{1}", "", GetCalendarName(optionalCalendar))
         If TypeOf optionalCalendar Is GregorianCalendar Then
            Console.Write(" ({0})", 
                          CType(optionalCalendar, GregorianCalendar).CalendarType)
         End If
         Console.WriteLine()
      Next
      Console.WriteLine()
   End Sub

   Function GetCalendarName(cal As Calendar) As String
      Return cal.ToString().Replace("System.Globalization.", "")
   End Function
End Module
' The example displays the following output:
'       Calendars for the th-TH culture:
'          Default Calendar: ThaiBuddhistCalendar
'          Optional Calendars:
'             ThaiBuddhistCalendar
'             GregorianCalendar (Localized)
'       
'       Calendars for the ja-JP culture:
'          Default Calendar: GregorianCalendar (Localized)
'          Optional Calendars:
'             GregorianCalendar (Localized)
'             JapaneseCalendar
'             GregorianCalendar (USEnglish)
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // Create a CultureInfo for Thai in Thailand.
      CultureInfo th = CultureInfo.CreateSpecificCulture("th-TH");
      DisplayCalendars(th);

      // Create a CultureInfo for Japanese in Japan.
      CultureInfo ja = CultureInfo.CreateSpecificCulture("ja-JP");
      DisplayCalendars(ja);
   }

   static void DisplayCalendars(CultureInfo ci)
   {
      Console.WriteLine("Calendars for the {0} culture:", ci.Name);

      // Get the culture's default calendar.
      Calendar defaultCalendar = ci.Calendar;
      Console.Write("   Default Calendar: {0}", GetCalendarName(defaultCalendar));      

      if (defaultCalendar is GregorianCalendar)
         Console.WriteLine(" ({0})", 
                           ((GregorianCalendar) defaultCalendar).CalendarType);
      else
         Console.WriteLine();

      // Get the culture's optional calendars.
      Console.WriteLine("   Optional Calendars:");      
      foreach (var optionalCalendar in ci.OptionalCalendars) {
         Console.Write("{0,6}{1}", "", GetCalendarName(optionalCalendar));
         if (optionalCalendar is GregorianCalendar)
            Console.Write(" ({0})", 
                          ((GregorianCalendar) optionalCalendar).CalendarType);

         Console.WriteLine();
      }
      Console.WriteLine();
   }

   static string GetCalendarName(Calendar cal)
   {
      return cal.ToString().Replace("System.Globalization.", "");
   }
}
// The example displays the following output:
//       Calendars for the th-TH culture:
//          Default Calendar: ThaiBuddhistCalendar
//          Optional Calendars:
//             ThaiBuddhistCalendar
//             GregorianCalendar (Localized)
//       
//       Calendars for the ja-JP culture:
//          Default Calendar: GregorianCalendar (Localized)
//          Optional Calendars:
//             GregorianCalendar (Localized)
//             JapaneseCalendar
//             GregorianCalendar (USEnglish)

特定 CultureInfo 对象当前正在使用的日历由区域性的 DateTimeFormatInfo.Calendar 属性定义。 区域性的 DateTimeFormatInfo 对象由 CultureInfo.DateTimeFormat 属性返回。 创建区域性时,其默认值与 CultureInfo.Calendar 属性的值相同。 但是,可将区域性的当前日历更改为 CultureInfo.OptionalCalendars 属性返回的数组中包含的任何日历。 如果尝试将当前日历设置为 CultureInfo.OptionalCalendars 属性值中不包括的日历,则会引发 ArgumentException

以下示例将更改阿拉伯(沙特阿拉伯)区域性使用的日历。 它首先实例化一个 DateTime 值并使用当前区域性(在此示例中,为英语(美国))和当前区域性的日历(在此示例中,为公历)显示该值。 接下来,该示例将当前区域性更改为阿拉伯语(沙特阿拉伯),并使用其默认的古兰经历显示该日期。 然后,该示例调用 CalendarExists 方法来确定阿拉伯语(沙特阿拉伯)区域性是否支持该回历。 因为该日历受支持,所以该示例将当前日历更改为回历并再次显示日期。 请注意,在每种情况下,均使用当前区域性的当前日历来显示日期。

Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      Dim date1 As Date = #6/20/2011#

      DisplayCurrentInfo()
      ' Display the date using the current culture and calendar.
      Console.WriteLine(date1.ToString("d"))       
      Console.WriteLine()

      Dim arSA As CultureInfo = CultureInfo.CreateSpecificCulture("ar-SA")

      ' Change the current culture to Arabic (Saudi Arabia).
      Thread.CurrentThread.CurrentCulture = arSA
      ' Display date and information about the current culture.
      DisplayCurrentInfo()
      Console.WriteLine(date1.ToString("d"))
      Console.WriteLine()

      ' Change the calendar to Hijri.
      Dim hijri As Calendar = New HijriCalendar()
      If CalendarExists(arSA, hijri) Then
         arSA.DateTimeFormat.Calendar = hijri
         ' Display date and information about the current culture.
         DisplayCurrentInfo()
         Console.WriteLine(date1.ToString("d"))
      End If       
   End Sub

   Private Sub DisplayCurrentInfo()
      Console.WriteLine("Current Culture: {0}", 
                        CultureInfo.CurrentCulture.Name)
      Console.WriteLine("Current Calendar: {0}", 
                        DateTimeFormatInfo.CurrentInfo.Calendar)
   End Sub

   Private Function CalendarExists(ByVal culture As CultureInfo, 
                                   cal As Calendar) As Boolean
      For Each optionalCalendar As Calendar In culture.OptionalCalendars
         If cal.ToString().Equals(optionalCalendar.ToString()) Then Return True
      Next   
      Return False
   End Function
End Module
' The example displays the following output:
'    Current Culture: en-US
'    Current Calendar: System.Globalization.GregorianCalendar
'    6/20/2011
'    
'    Current Culture: ar-SA
'    Current Calendar: System.Globalization.UmAlQuraCalendar
'    18/07/32
'    
'    Current Culture: ar-SA
'    Current Calendar: System.Globalization.HijriCalendar
'    19/07/32
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      DateTime date1 = new DateTime(2011, 6, 20);

      DisplayCurrentInfo();
      // Display the date using the current culture and calendar.
      Console.WriteLine(date1.ToString("d"));       
      Console.WriteLine();

      CultureInfo arSA = CultureInfo.CreateSpecificCulture("ar-SA");

      // Change the current culture to Arabic (Saudi Arabia).
      Thread.CurrentThread.CurrentCulture = arSA;
      // Display date and information about the current culture.
      DisplayCurrentInfo();
      Console.WriteLine(date1.ToString("d"));
      Console.WriteLine();

      // Change the calendar to Hijri.
      Calendar hijri = new HijriCalendar();
      if (CalendarExists(arSA, hijri)) {
         arSA.DateTimeFormat.Calendar = hijri;
         // Display date and information about the current culture.
         DisplayCurrentInfo();
         Console.WriteLine(date1.ToString("d"));
      }       
   }

   private static void DisplayCurrentInfo()
   {
      Console.WriteLine("Current Culture: {0}", 
                        CultureInfo.CurrentCulture.Name);
      Console.WriteLine("Current Calendar: {0}", 
                        DateTimeFormatInfo.CurrentInfo.Calendar);
   }

   private static bool CalendarExists(CultureInfo culture, Calendar cal)
   {
      foreach (Calendar optionalCalendar in culture.OptionalCalendars)
         if (cal.ToString().Equals(optionalCalendar.ToString())) 
            return true;

      return false;
   }
}
// The example displays the following output:
//    Current Culture: en-US
//    Current Calendar: System.Globalization.GregorianCalendar
//    6/20/2011
//    
//    Current Culture: ar-SA
//    Current Calendar: System.Globalization.UmAlQuraCalendar
//    18/07/32
//    
//    Current Culture: ar-SA
//    Current Calendar: System.Globalization.HijriCalendar
//    19/07/32

日期和日历

除包括 Calendar 类型参数并允许日期的元素(即月、日和年)以指定日历反映值的构造函数外,DateTimeDateTimeOffset 值始终基于公历。 例如,这意味着 DateTime.Year 属性返回公历的年,DateTime.Day 属性返回公历的当月日期。

重要说明重要事项

请记住,日期值与其字符串表示形式之间是有差异的,这一点很重要。前者基于公历,后者基于特定区域性的当前日历。

下面的示例演示了 DateTime 属性与其对应的 Calendar 方法之间的这一差异。 在此示例中,当前区域性为阿拉伯语(埃及),当前日历为古兰经历。 DateTime 值设置为 2011 年 7 月 15 日。 很显然,该值被解释为公历日期,因为这些相同的值是由 DateTime.ToString(String, IFormatProvider) 方法在使用不变区域性的约定时返回的。 使用当前区域性的约定设置格式后,该日期的字符串表示形式为 14/08/32,与古兰经历中的日期等效。 接下来,使用 DateTime 和 Calendar 的成员返回 DateTime 值的日、月和年。 在每种情况下,DateTime 成员返回的值反映公历中的值,而 UmAlQuraCalendar 成员返回的值反映古兰经历中的值。

Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      ' Make Arabic (Egypt) the current culture 
      ' and Umm al-Qura calendar the current calendar. 
      Dim arEG As CultureInfo = CultureInfo.CreateSpecificCulture("ar-EG")
      Dim cal As Calendar = New UmAlQuraCalendar()
      arEG.DateTimeFormat.Calendar = cal
      Thread.CurrentThread.CurrentCulture = arEG

      ' Display information on current culture and calendar.
      DisplayCurrentInfo()      

      ' Instantiate a date object.
      Dim date1 As Date = #07/15/2011#

      ' Display the string representation of the date.
      Console.WriteLine("Date: {0:d}", date1)
      Console.WriteLine("Date in the Invariant Culture: {0}", 
                        date1.ToString("d", CultureInfo.InvariantCulture))
      Console.WriteLine()

      ' Compare DateTime properties and Calendar methods.
      Console.WriteLine("DateTime.Month property: {0}", date1.Month)
      Console.WriteLine("UmAlQura.GetMonth: {0}", 
                        cal.GetMonth(date1))
      Console.WriteLine()

      Console.WriteLine("DateTime.Day property: {0}", date1.Day)
      Console.WriteLine("UmAlQura.GetDayOfMonth: {0}", 
                        cal.GetDayOfMonth(date1))                         
      Console.WriteLine()

      Console.WriteLine("DateTime.Year property: {0:D4}", date1.Year)
      Console.WriteLine("UmAlQura.GetYear: {0}", 
                        cal.GetYear(date1))                         
      Console.WriteLine()
   End Sub

   Private Sub DisplayCurrentInfo()
      Console.WriteLine("Current Culture: {0}", 
                        CultureInfo.CurrentCulture.Name)
      Console.WriteLine("Current Calendar: {0}", 
                        DateTimeFormatInfo.CurrentInfo.Calendar)
   End Sub
End Module
' The example displays the following output:
'    Current Culture: ar-EG
'    Current Calendar: System.Globalization.UmAlQuraCalendar
'    Date: 14/08/32
'    Date in the Invariant Culture: 07/15/2011
'    
'    DateTime.Month property: 7
'    UmAlQura.GetMonth: 8
'    
'    DateTime.Day property: 15
'    UmAlQura.GetDayOfMonth: 14
'    
'    DateTime.Year property: 2011
'    UmAlQura.GetYear: 1432
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      // Make Arabic (Egypt) the current culture 
      // and Umm al-Qura calendar the current calendar. 
      CultureInfo arEG = CultureInfo.CreateSpecificCulture("ar-EG");
      Calendar cal = new UmAlQuraCalendar();
      arEG.DateTimeFormat.Calendar = cal;
      Thread.CurrentThread.CurrentCulture = arEG;

      // Display information on current culture and calendar.
      DisplayCurrentInfo();      

      // Instantiate a date object.
      DateTime date1 = new DateTime(2011, 7, 15);

      // Display the string representation of the date.
      Console.WriteLine("Date: {0:d}", date1);
      Console.WriteLine("Date in the Invariant Culture: {0}", 
                        date1.ToString("d", CultureInfo.InvariantCulture));
      Console.WriteLine();

      // Compare DateTime properties and Calendar methods.
      Console.WriteLine("DateTime.Month property: {0}", date1.Month);
      Console.WriteLine("UmAlQura.GetMonth: {0}", 
                        cal.GetMonth(date1));
      Console.WriteLine();

      Console.WriteLine("DateTime.Day property: {0}", date1.Day);
      Console.WriteLine("UmAlQura.GetDayOfMonth: {0}", 
                        cal.GetDayOfMonth(date1));                         
      Console.WriteLine();

      Console.WriteLine("DateTime.Year property: {0:D4}", date1.Year);
      Console.WriteLine("UmAlQura.GetYear: {0}", 
                        cal.GetYear(date1));                         
      Console.WriteLine();
   }

   private static void DisplayCurrentInfo()
   {
      Console.WriteLine("Current Culture: {0}", 
                        CultureInfo.CurrentCulture.Name);
      Console.WriteLine("Current Calendar: {0}", 
                        DateTimeFormatInfo.CurrentInfo.Calendar);
   }
}
// The example displays the following output:
//    Current Culture: ar-EG
//    Current Calendar: System.Globalization.UmAlQuraCalendar
//    Date: 14/08/32
//    Date in the Invariant Culture: 07/15/2011
//    
//    DateTime.Month property: 7
//    UmAlQura.GetMonth: 8
//    
//    DateTime.Day property: 15
//    UmAlQura.GetDayOfMonth: 14
//    
//    DateTime.Year property: 2011
//    UmAlQura.GetYear: 1432

根据日历实例化日期

由于 DateTimeDateTimeOffset 值基于公历,因此如果要使用不同日历中的日、月或年的值,必须调用包括 Calendar 类型参数的重载构造函数来实例化日期值。 还可以调用特定日历的 Calendar.ToDateTime 方法的重载之一,以基于特定日历的值来实例化 DateTime 对象。

下面的示例通过向 DateTime 构造函数传递一个 HebrewCalendar 对象来实例化一个 DateTime 值,通过调用 HebrewCalendar.ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32) 方法来实例化另一个 DateTime 值。 由于这两个值是使用希伯来历中的相同值创建的,因此对 DateTime.Equals 方法的调用显示这两个 DateTime 值相等。

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim hc As New HebrewCalendar()

      Dim date1 As New Date(5771, 6, 1, hc)
      Dim date2 As Date = hc.ToDateTime(5771, 6, 1, 0, 0, 0, 0)

      Console.WriteLine("{0:d} (Gregorian) = {1:d2}/{2:d2}/{3:d4} ({4}): {5}",
                        date1, 
                        hc.GetMonth(date2),
                        hc.GetDayOfMonth(date2),
                        hc.GetYear(date2), 
                        GetCalendarName(hc),
                        date1.Equals(date2))
   End Sub

   Private Function GetCalendarName(cal As Calendar) As String
      Return cal.ToString().Replace("System.Globalization.", ""). 
                            Replace("Calendar", "")
   End Function
End Module
' The example displays the following output:
'   2/5/2011 (Gregorian) = 06/01/5771 (Hebrew): True
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      HebrewCalendar hc = new HebrewCalendar();

      DateTime date1 = new DateTime(5771, 6, 1, hc);
      DateTime date2 = hc.ToDateTime(5771, 6, 1, 0, 0, 0, 0);

      Console.WriteLine("{0:d} (Gregorian) = {1:d2}/{2:d2}/{3:d4} ({4}): {5}",
                        date1, 
                        hc.GetMonth(date2),
                        hc.GetDayOfMonth(date2),
                        hc.GetYear(date2), 
                        GetCalendarName(hc),
                        date1.Equals(date2));
   }

   private static string GetCalendarName(Calendar cal)
   {
      return cal.ToString().Replace("System.Globalization.", ""). 
                            Replace("Calendar", "");
   }
}
// The example displays the following output:
//    2/5/2011 (Gregorian) = 06/01/5771 (Hebrew): True

使用当前日历表示日期

在将日期转换为字符串时,日期和时间格式设置方法始终使用当前日历。 这意味着,年、月和当月日期的字符串表示形式反映当前日历,不一定反映公历。

下面的示例演示当前日历如何影响日期的字符串表示形式。 该示例将当前区域性更改为中文(繁体,台湾),并实例化日期值。 然后,该示例显示当前日历和日期,将当前日历更改为 TaiwanCalendar,并再次显示当前日历和日期。 初次显示日期时,它表示为公历日期。 第二次显示日期时,它表示为台湾日历中的日期。

Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      ' Change the current culture to zh-TW.
      Dim zhTW As CultureInfo = CultureInfo.CreateSpecificCulture("zh-TW")
      Thread.CurrentThread.CurrentCulture = zhTW
      ' Define a date.
      Dim date1 As Date = #1/16/2011#

      ' Display the date using the default (Gregorian) calendar.
      Console.WriteLine("Current calendar: {0}", 
                        zhTW.DateTimeFormat.Calendar)
      Console.WriteLine(date1.ToString("d"))

      ' Change the current calendar and display the date.
      zhTW.DateTimeFormat.Calendar = New TaiwanCalendar()      
      Console.WriteLine("Current calendar: {0}", 
                        zhTW.DateTimeFormat.Calendar)
      Console.WriteLine(date1.ToString("d"))
   End Sub
End Module
' The example displays the following output:
'    Current calendar: System.Globalization.GregorianCalendar
'    2011/1/16
'    Current calendar: System.Globalization.TaiwanCalendar
'    100/1/16
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      // Change the current culture to zh-TW.
      CultureInfo zhTW = CultureInfo.CreateSpecificCulture("zh-TW");
      Thread.CurrentThread.CurrentCulture = zhTW;
      // Define a date.
      DateTime date1 = new DateTime(2011, 1, 16);

      // Display the date using the default (Gregorian) calendar.
      Console.WriteLine("Current calendar: {0}", 
                        zhTW.DateTimeFormat.Calendar);
      Console.WriteLine(date1.ToString("d"));

      // Change the current calendar and display the date.
      zhTW.DateTimeFormat.Calendar = new TaiwanCalendar();      
      Console.WriteLine("Current calendar: {0}", 
                        zhTW.DateTimeFormat.Calendar);
      Console.WriteLine(date1.ToString("d"));
   }
}
// The example displays the following output:
//    Current calendar: System.Globalization.GregorianCalendar
//    2011/1/16
//    Current calendar: System.Globalization.TaiwanCalendar
//    100/1/16

使用非当前日历表示日期

若要使用特定区域性的当前日历之外的日历表示日期,必须调用该 Calendar 对象的方法。 例如,Calendar.GetYearCalendar.GetMonthCalendar.GetDayOfMonth 方法可将年、月和日转换为反映特定日历的值。

警告说明警告

由于某些日历不是任何区域性的可选日历,因此用这些日历表示日期始终需要调用日历方法。对于派生自 EastAsianLunisolarCalendarJulianCalendarPersianCalendar 类的所有日历而言,均为这种情况。

下面的示例使用 JulianCalendar 对象实例化罗马儒略历的日期 1905 年 1 月 9 日。 使用默认(公历)日历显示此日期时,该日期表示为 1905 年 1 月 22 日。 通过调用各种 JulianCalendar 方法,可用罗马儒略历表示日期。

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim julian As New JulianCalendar()
      Dim date1 As New Date(1905, 1, 9, julian)

      Console.WriteLine("Date ({0}): {1:d}", 
                        CultureInfo.CurrentCulture.Calendar,
                        date1)
      Console.WriteLine("Date in Julian calendar: {0:d2}/{1:d2}/{2:d4}",
                        julian.GetMonth(date1),
                        julian.GetDayOfMonth(date1),
                        julian.GetYear(date1))
   End Sub
End Module
' The example displays the following output:
'    Date (System.Globalization.GregorianCalendar): 1/22/1905
'    Date in Julian calendar: 01/09/1905
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      JulianCalendar julian = new JulianCalendar();
      DateTime date1 = new DateTime(1905, 1, 9, julian);

      Console.WriteLine("Date ({0}): {1:d}", 
                        CultureInfo.CurrentCulture.Calendar,
                        date1);
      Console.WriteLine("Date in Julian calendar: {0:d2}/{1:d2}/{2:d4}",
                        julian.GetMonth(date1),
                        julian.GetDayOfMonth(date1),
                        julian.GetYear(date1));
   }
}
// The example displays the following output:
//    Date (System.Globalization.GregorianCalendar): 1/22/1905
//    Date in Julian calendar: 01/09/1905

使用纪元

日历通常将日期划分为纪元。 但是,.NET Framework 中的 Calendar 类不支持日历定义的每个纪元,而且大多数 Calendar 类只支持单个纪元。 只有 JapaneseCalendarJapaneseLunisolarCalendar 类支持多个纪元。

纪元和纪元名称

在 .NET Framework 中,表示特定日历实现支持的纪元的整数以反向顺序存储在 Calendar.Eras 数组中。 当前纪元的索引为零,对于支持多个纪元的 Calendar 类,每个后续索引反映前一个纪元。 静态的 Calendar.CurrentEra 属性定义当前纪元在 Calendar.Eras 数组中的索引;它是一个值始终为零的常数。 各个 Calendar 类还包括可返回当前纪元值的静态字段。 下表中列出了这些字段。

日历类

当前纪元字段

ChineseLunisolarCalendar

ChineseEra

GregorianCalendar

ADEra

HebrewCalendar

HebrewEra

HijriCalendar

HijriEra

JapaneseLunisolarCalendar

JapaneseEra

JulianCalendar

JulianEra

KoreanCalendar

KoreanEra

KoreanLunisolarCalendar

GregorianEra

PersianCalendar

PersianEra

ThaiBuddhistCalendar

ThaiBuddhistEra

UmAlQuraCalendar

UmAlQuraEra

通过将特定纪元编号传递给 DateTimeFormatInfo.GetEraNameDateTimeFormatInfo.GetAbbreviatedEraName 方法,可检索与该纪元编号对应的名称。 下面的示例调用这些方法来检索有关 GregorianCalendar 类中的纪元支持信息。

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim year As Integer = 2
      Dim month As Integer = 1
      Dim day As Integer = 1
      Dim cal As New JapaneseCalendar()

      Console.WriteLine("Date instantiated without an era:")
      Dim date1 As New Date(year, month, day, 0, 0, 0, 0, cal)
      Console.WriteLine("{0}/{1}/{2} in Japanese Calendar -> {3:d} in Gregorian", 
                        cal.GetMonth(date1), cal.GetDayOfMonth(date1),
                        cal.GetYear(date1), date1)
      Console.WriteLine()

      Console.WriteLine("Dates instantiated with eras:")
      For Each era As Integer In cal.Eras
         Dim date2 As Date = cal.ToDateTime(year, month, day, 0, 0, 0, 0, era)
         Console.WriteLine("{0}/{1}/{2} era {3} in Japanese Calendar -> {4:d} in Gregorian", 
                           cal.GetMonth(date2), cal.GetDayOfMonth(date2),
                           cal.GetYear(date2), cal.GetEra(date2), date2)
      Next                        
   End Sub
End Module
' The example displays the following output:
'    Date instantiated without an era:
'    1/1/2 in Japanese Calendar -> 1/1/1990 in Gregorian
'    
'    Dates instantiated with eras:
'    1/1/2 era 4 in Japanese Calendar -> 1/1/1990 in Gregorian
'    1/1/2 era 3 in Japanese Calendar -> 1/1/1927 in Gregorian
'    1/1/2 era 2 in Japanese Calendar -> 1/1/1913 in Gregorian
'    1/1/2 era 1 in Japanese Calendar -> 1/1/1869 in Gregorian
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      int year = 2;
      int month = 1;
      int day = 1;
      Calendar cal = new JapaneseCalendar();

      Console.WriteLine("\nDate instantiated without an era:");
      DateTime date1 = new DateTime(year, month, day, 0, 0, 0, 0, cal);
      Console.WriteLine("{0}/{1}/{2} in Japanese Calendar -> {3:d} in Gregorian", 
                        cal.GetMonth(date1), cal.GetDayOfMonth(date1),
                        cal.GetYear(date1), date1);

      Console.WriteLine("\nDates instantiated with eras:");
      foreach (int era in cal.Eras) {
         DateTime date2 = cal.ToDateTime(year, month, day, 0, 0, 0, 0, era);
         Console.WriteLine("{0}/{1}/{2} era {3} in Japanese Calendar -> {4:d} in Gregorian", 
                           cal.GetMonth(date2), cal.GetDayOfMonth(date2),
                           cal.GetYear(date2), cal.GetEra(date2), date2);
      }                        
   }
}
// The example displays the following output:
//    Date instantiated without an era:
//    1/1/2 in Japanese Calendar -> 1/1/1990 in Gregorian
//    
//    Dates instantiated with eras:
//    1/1/2 era 4 in Japanese Calendar -> 1/1/1990 in Gregorian
//    1/1/2 era 3 in Japanese Calendar -> 1/1/1927 in Gregorian
//    1/1/2 era 2 in Japanese Calendar -> 1/1/1913 in Gregorian
//    1/1/2 era 1 in Japanese Calendar -> 1/1/1869 in Gregorian

此外,“g”自定义日期和时间格式字符串还包括一个日期和时间的字符串表示形式的日期纪元名称。 有关更多信息,请参见自定义日期和时间格式字符串

用纪元实例化日期

对于支持多个纪元的两个 Calendar 类,由特定年、月和当月日期值构成的日期可能不明确。例如,JapaneseCalendar 的所有四个纪元的年份编号均为 1 至 15。 通常,如果未指定纪元,则日期和时间以及日历方法都假定该值属于当前纪元。 若要在为支持多个纪元的 Calendar 类实例化日期时显式指定纪元,可调用 Calendar.ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32) 方法。 通过此方法可以显式指定纪元以及日期的年、月、日、小时、分钟、秒和毫秒。

下面的示例使用 Calendar.ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32) 方法在 JapaneseCalendar 类支持的每个纪元中实例化相同的日期:第二年的第一个月的第一天。 然后,该示例使用日本日历和公历显示该日期。 它还调用 DateTime 构造函数,以演示在不指定纪元时,创建日期的方法会以当前纪元创建日期。

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim year As Integer = 2
      Dim month As Integer = 1
      Dim day As Integer = 1
      Dim cal As New JapaneseCalendar()

      Console.WriteLine("Date instantiated without an era:")
      Dim date1 As New Date(year, month, day, 0, 0, 0, 0, cal)
      Console.WriteLine("{0}/{1}/{2} in Japanese Calendar -> {3:d} in Gregorian", 
                        cal.GetMonth(date1), cal.GetDayOfMonth(date1),
                        cal.GetYear(date1), date1)
      Console.WriteLine()

      Console.WriteLine("Dates instantiated with eras:")
      For Each era As Integer In cal.Eras
         Dim date2 As Date = cal.ToDateTime(year, month, day, 0, 0, 0, 0, era)
         Console.WriteLine("{0}/{1}/{2} era {3} in Japanese Calendar -> {4:d} in Gregorian", 
                           cal.GetMonth(date2), cal.GetDayOfMonth(date2),
                           cal.GetYear(date2), cal.GetEra(date2), date2)
      Next                        
   End Sub
End Module
' The example displays the following output:
'    Date instantiated without an era:
'    1/1/2 in Japanese Calendar -> 1/1/1990 in Gregorian
'    
'    Dates instantiated with eras:
'    1/1/2 era 4 in Japanese Calendar -> 1/1/1990 in Gregorian
'    1/1/2 era 3 in Japanese Calendar -> 1/1/1927 in Gregorian
'    1/1/2 era 2 in Japanese Calendar -> 1/1/1913 in Gregorian
'    1/1/2 era 1 in Japanese Calendar -> 1/1/1869 in Gregorian
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      int year = 2;
      int month = 1;
      int day = 1;
      Calendar cal = new JapaneseCalendar();

      Console.WriteLine("\nDate instantiated without an era:");
      DateTime date1 = new DateTime(year, month, day, 0, 0, 0, 0, cal);
      Console.WriteLine("{0}/{1}/{2} in Japanese Calendar -> {3:d} in Gregorian", 
                        cal.GetMonth(date1), cal.GetDayOfMonth(date1),
                        cal.GetYear(date1), date1);

      Console.WriteLine("\nDates instantiated with eras:");
      foreach (int era in cal.Eras) {
         DateTime date2 = cal.ToDateTime(year, month, day, 0, 0, 0, 0, era);
         Console.WriteLine("{0}/{1}/{2} era {3} in Japanese Calendar -> {4:d} in Gregorian", 
                           cal.GetMonth(date2), cal.GetDayOfMonth(date2),
                           cal.GetYear(date2), cal.GetEra(date2), date2);
      }                        
   }
}
// The example displays the following output:
//    Date instantiated without an era:
//    1/1/2 in Japanese Calendar -> 1/1/1990 in Gregorian
//    
//    Dates instantiated with eras:
//    1/1/2 era 4 in Japanese Calendar -> 1/1/1990 in Gregorian
//    1/1/2 era 3 in Japanese Calendar -> 1/1/1927 in Gregorian
//    1/1/2 era 2 in Japanese Calendar -> 1/1/1913 in Gregorian
//    1/1/2 era 1 in Japanese Calendar -> 1/1/1869 in Gregorian

使用带纪元的日历表示日期

如果 Calendar 对象支持纪元并且是 CultureInfo 对象的当前日历,则纪元将包括在完整日期和时间、长日期和短日期模式的日期和时间值的字符串表示形式中。 下面的示例显示当前区域性为日语(日本)并且当前日历为日本日历时的这些日期模式。

Imports System.Globalization
Imports System.IO
Imports System.Threading

Module Example
   Public Sub Main()
      Dim sw As New StreamWriter(".\eras.txt")
      Dim dt As Date = #05/01/2012#

      Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("ja-JP")
      Dim dtfi As DateTimeFormatInfo = culture.DateTimeFormat
      dtfi.Calendar = New JapaneseCalendar()
      Thread.CurrentThread.CurrentCulture = culture

      sw.WriteLine("{0,-43} {1}", "Full Date and Time Pattern:", dtfi.FullDateTimePattern)
      sw.WriteLine(dt.ToString("F"))
      sw.WriteLine()

      sw.WriteLine("{0,-43} {1}", "Long Date Pattern:", dtfi.LongDatePattern)
      sw.WriteLine(dt.ToString("D"))
      sw.WriteLine()

      sw.WriteLine("{0,-43} {1}", "Short Date Pattern:", dtfi.ShortDatePattern)
      sw.WriteLine(dt.ToString("d"))
      sw.WriteLine()
      sw.Close()
   End Sub
End Module
' The example writes the following output to a file:
'    Full Date and Time Pattern:                 gg y'年'M'月'd'日' H:mm:ss
'    平成 24年5月1日 0:00:00
'    
'    Long Date Pattern:                          gg y'年'M'月'd'日'
'    平成 24年5月1日
'    
'    Short Date Pattern:                         gg y/M/d
'    平成 24/5/1 
using System;
using System.Globalization;
using System.IO;
using System.Threading;

public class Example
{
   public static void Main()
   {
      StreamWriter sw = new StreamWriter(@".\eras.txt");
      DateTime dt = new DateTime(2012, 5, 1);

      CultureInfo culture = CultureInfo.CreateSpecificCulture("ja-JP");
      DateTimeFormatInfo dtfi = culture.DateTimeFormat;
      dtfi.Calendar = new JapaneseCalendar();
      Thread.CurrentThread.CurrentCulture = culture;

      sw.WriteLine("\n{0,-43} {1}", "Full Date and Time Pattern:", dtfi.FullDateTimePattern);
      sw.WriteLine(dt.ToString("F"));
      sw.WriteLine();

      sw.WriteLine("\n{0,-43} {1}", "Long Date Pattern:", dtfi.LongDatePattern);
      sw.WriteLine(dt.ToString("D"));

      sw.WriteLine("\n{0,-43} {1}", "Short Date Pattern:", dtfi.ShortDatePattern);
      sw.WriteLine(dt.ToString("d"));
      sw.Close();
    }
}
// The example writes the following output to a file:
//    Full Date and Time Pattern:                 gg y'年'M'月'd'日' H:mm:ss
//    平成 24年5月1日 0:00:00
//    
//    Long Date Pattern:                          gg y'年'M'月'd'日'
//    平成 24年5月1日
//    
//    Short Date Pattern:                         gg y/M/d
//    平成 24/5/1
警告说明警告

JapaneseCalendar 类是 .NET Framework 中既支持多个纪元中的日期又可为 CultureInfo 对象(具体而言,是表示日语(日本)区域性的 CultureInfo 对象)的当前日历的唯一日历类。

对于所有日历,“g”自定义格式说明符会在结果字符串中包括纪元。 下面的示例使用“MM-dd-yyyy g”自定义格式字符串,在当前日历为公历时在结果字符串中包括纪元。

Dim dat As Date = #05/01/2012#
Console.WriteLine("{0:MM-dd-yyyy g}", dat)
' The example displays the following output:
'     05-01-2012 A.D.      
   DateTime dat = new DateTime(2012, 5, 1);
   Console.WriteLine("{0:MM-dd-yyyy g}", dat);
// The example displays the following output:
//     05-01-2012 A.D.      

当日期的字符串表示形式用非当前日历来表示时,Calendar 类包括一个可与 Calendar.GetYearCalendar.GetMonthCalendar.GetDayOfMonth 方法一起使用的 Calendar.GetEra 方法,以明确指示日期及其所属纪元。 下面的示例使用 JapaneseLunisolarCalendar 类来提供演示。 但请注意,在结果字符串中包括有意义的名称或缩写(而不是表示纪元的整数)需要您实例化 DateTimeFormatInfo 对象并使 JapaneseCalendar 成为其当前日历。 (JapaneseLunisolarCalendar 日历不能为任何区域性的当前日历,但在此示例中,两个日历共享相同的纪元。)

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim date1 As Date = #8/28/2011#
      Dim cal As New JapaneseLunisolarCalendar()
      Console.WriteLine("{0} {1:d4}/{2:d2}/{3:d2}", 
                        cal.GetEra(date1),
                        cal.GetYear(date1),
                        cal.GetMonth(date1),
                        cal.GetDayOfMonth(date1)) 

      ' Display eras
      Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("ja-JP")
      Dim dtfi As DateTimeFormatInfo = culture.DateTimeFormat
      dtfi.Calendar = New JapaneseCalendar()

      Console.WriteLine("{0} {1:d4}/{2:d2}/{3:d2}", 
                        dtfi.GetAbbreviatedEraName(cal.GetEra(date1)),
                        cal.GetYear(date1),
                        cal.GetMonth(date1),
                        cal.GetDayOfMonth(date1)) 
   End Sub
End Module
' The example displays the following output:
'       4 0023/07/29
'       平 0023/07/29
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTime date1 = new DateTime(2011, 8, 28);
      Calendar cal = new JapaneseLunisolarCalendar();

      Console.WriteLine("{0} {1:d4}/{2:d2}/{3:d2}", 
                        cal.GetEra(date1),
                        cal.GetYear(date1),
                        cal.GetMonth(date1),
                        cal.GetDayOfMonth(date1)); 

      // Display eras
      CultureInfo culture = CultureInfo.CreateSpecificCulture("ja-JP");
      DateTimeFormatInfo dtfi = culture.DateTimeFormat;
      dtfi.Calendar = new JapaneseCalendar();

      Console.WriteLine("{0} {1:d4}/{2:d2}/{3:d2}", 
                        dtfi.GetAbbreviatedEraName(cal.GetEra(date1)),
                        cal.GetYear(date1),
                        cal.GetMonth(date1),
                        cal.GetDayOfMonth(date1)); 
   }
}
// The example displays the following output:
//       4 0023/07/29
//       平 0023/07/29

请参见

任务

如何:用非公历日历显示日期

修订记录

日期

修订记录

原因

2010 年 9 月

做了大范围修订。

信息补充。