Calendar 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示部门中的时间(如周、月和年)。
public ref class Calendar abstractpublic ref class Calendar abstract : ICloneablepublic abstract class Calendarpublic abstract class Calendar : ICloneable[System.Serializable]
public abstract class Calendar[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Calendar : ICloneabletype Calendar = classtype Calendar = class
    interface ICloneable[<System.Serializable>]
type Calendar = class[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Calendar = class
    interface ICloneablePublic MustInherit Class CalendarPublic MustInherit Class Calendar
Implements ICloneable- 继承
- 
				Calendar
- 派生
- 属性
- 实现
示例
下面的代码示例演示 类的成员 Calendar 。
using namespace System;
using namespace System::Globalization;
void DisplayValues( Calendar^ myCal, DateTime myDT )
{
   Console::WriteLine( "   Era: {0}", myCal->GetEra( myDT ) );
   Console::WriteLine( "   Year: {0}", myCal->GetYear( myDT ) );
   Console::WriteLine( "   Month: {0}", myCal->GetMonth( myDT ) );
   Console::WriteLine( "   DayOfYear: {0}", myCal->GetDayOfYear( myDT ) );
   Console::WriteLine( "   DayOfMonth: {0}", myCal->GetDayOfMonth( myDT ) );
   Console::WriteLine( "   DayOfWeek: {0}", myCal->GetDayOfWeek( myDT ) );
   Console::WriteLine( "   Hour: {0}", myCal->GetHour( myDT ) );
   Console::WriteLine( "   Minute: {0}", myCal->GetMinute( myDT ) );
   Console::WriteLine( "   Second: {0}", myCal->GetSecond( myDT ) );
   Console::WriteLine( "   Milliseconds: {0}", myCal->GetMilliseconds( myDT ) );
   Console::WriteLine();
}
int main()
{
   
   // Sets a DateTime to April 3, 2002 of the Gregorian calendar.
   DateTime myDT = DateTime(2002,4,3,gcnew GregorianCalendar);
   
   // Uses the default calendar of the InvariantCulture.
   Calendar^ myCal = CultureInfo::InvariantCulture->Calendar;
   
   // Displays the values of the DateTime.
   Console::WriteLine( "April 3, 2002 of the Gregorian calendar:" );
   DisplayValues( myCal, myDT );
   
   // Adds 5 to every component of the DateTime.
   myDT = myCal->AddYears( myDT, 5 );
   myDT = myCal->AddMonths( myDT, 5 );
   myDT = myCal->AddWeeks( myDT, 5 );
   myDT = myCal->AddDays( myDT, 5 );
   myDT = myCal->AddHours( myDT, 5 );
   myDT = myCal->AddMinutes( myDT, 5 );
   myDT = myCal->AddSeconds( myDT, 5 );
   myDT = myCal->AddMilliseconds( myDT, 5 );
   
   // Displays the values of the DateTime.
   Console::WriteLine( "After adding 5 to each component of the DateTime:" );
   DisplayValues( myCal, myDT );
}
/*
This code produces the following output.
April 3, 2002 of the Gregorian calendar:
Era:          1
Year:         2002
Month:        4
DayOfYear:    93
DayOfMonth:   3
DayOfWeek:    Wednesday
Hour:         0
Minute:       0
Second:       0
Milliseconds: 0
After adding 5 to each component of the DateTime:
Era:          1
Year:         2007
Month:        10
DayOfYear:    286
DayOfMonth:   13
DayOfWeek:    Saturday
Hour:         5
Minute:       5
Second:       5
Milliseconds: 5
*/
using System;
using System.Globalization;
public class SamplesCalendar  {
   public static void Main()  {
      // Sets a DateTime to April 3, 2002 of the Gregorian calendar.
      DateTime myDT = new DateTime( 2002, 4, 3, new GregorianCalendar() );
      // Uses the default calendar of the InvariantCulture.
      Calendar myCal = CultureInfo.InvariantCulture.Calendar;
      // Displays the values of the DateTime.
      Console.WriteLine( "April 3, 2002 of the Gregorian calendar:" );
      DisplayValues( myCal, myDT );
      // Adds 5 to every component of the DateTime.
      myDT = myCal.AddYears( myDT, 5 );
      myDT = myCal.AddMonths( myDT, 5 );
      myDT = myCal.AddWeeks( myDT, 5 );
      myDT = myCal.AddDays( myDT, 5 );
      myDT = myCal.AddHours( myDT, 5 );
      myDT = myCal.AddMinutes( myDT, 5 );
      myDT = myCal.AddSeconds( myDT, 5 );
      myDT = myCal.AddMilliseconds( myDT, 5 );
      // Displays the values of the DateTime.
      Console.WriteLine( "After adding 5 to each component of the DateTime:" );
      DisplayValues( myCal, myDT );
   }
   public static void DisplayValues( Calendar myCal, DateTime myDT )  {
      Console.WriteLine( "   Era:          {0}", myCal.GetEra( myDT ) );
      Console.WriteLine( "   Year:         {0}", myCal.GetYear( myDT ) );
      Console.WriteLine( "   Month:        {0}", myCal.GetMonth( myDT ) );
      Console.WriteLine( "   DayOfYear:    {0}", myCal.GetDayOfYear( myDT ) );
      Console.WriteLine( "   DayOfMonth:   {0}", myCal.GetDayOfMonth( myDT ) );
      Console.WriteLine( "   DayOfWeek:    {0}", myCal.GetDayOfWeek( myDT ) );
      Console.WriteLine( "   Hour:         {0}", myCal.GetHour( myDT ) );
      Console.WriteLine( "   Minute:       {0}", myCal.GetMinute( myDT ) );
      Console.WriteLine( "   Second:       {0}", myCal.GetSecond( myDT ) );
      Console.WriteLine( "   Milliseconds: {0}", myCal.GetMilliseconds( myDT ) );
      Console.WriteLine();
   }
}
/*
This code produces the following output.
April 3, 2002 of the Gregorian calendar:
   Era:          1
   Year:         2002
   Month:        4
   DayOfYear:    93
   DayOfMonth:   3
   DayOfWeek:    Wednesday
   Hour:         0
   Minute:       0
   Second:       0
   Milliseconds: 0
After adding 5 to each component of the DateTime:
   Era:          1
   Year:         2007
   Month:        10
   DayOfYear:    286
   DayOfMonth:   13
   DayOfWeek:    Saturday
   Hour:         5
   Minute:       5
   Second:       5
   Milliseconds: 5
*/
Imports System.Globalization
Public Class SamplesCalendar   
   Public Shared Sub Main()
      ' Sets a DateTime to April 3, 2002 of the Gregorian calendar.
      Dim myDT As New DateTime(2002, 4, 3, New GregorianCalendar())
      ' Uses the default calendar of the InvariantCulture.
      Dim myCal As Calendar = CultureInfo.InvariantCulture.Calendar
      ' Displays the values of the DateTime.
      Console.WriteLine("April 3, 2002 of the Gregorian calendar:")
      DisplayValues(myCal, myDT)
      ' Adds 5 to every component of the DateTime.
      myDT = myCal.AddYears(myDT, 5)
      myDT = myCal.AddMonths(myDT, 5)
      myDT = myCal.AddWeeks(myDT, 5)
      myDT = myCal.AddDays(myDT, 5)
      myDT = myCal.AddHours(myDT, 5)
      myDT = myCal.AddMinutes(myDT, 5)
      myDT = myCal.AddSeconds(myDT, 5)
      myDT = myCal.AddMilliseconds(myDT, 5)
      ' Displays the values of the DateTime.
      Console.WriteLine("After adding 5 to each component of the DateTime:")
      DisplayValues(myCal, myDT)
   End Sub
   Public Shared Sub DisplayValues(myCal As Calendar, myDT As DateTime)
      Console.WriteLine("   Era:          {0}", myCal.GetEra(myDT))
      Console.WriteLine("   Year:         {0}", myCal.GetYear(myDT))
      Console.WriteLine("   Month:        {0}", myCal.GetMonth(myDT))
      Console.WriteLine("   DayOfYear:    {0}", myCal.GetDayOfYear(myDT))
      Console.WriteLine("   DayOfMonth:   {0}", myCal.GetDayOfMonth(myDT))
      Console.WriteLine("   DayOfWeek:    {0}", myCal.GetDayOfWeek(myDT))
      Console.WriteLine("   Hour:         {0}", myCal.GetHour(myDT))
      Console.WriteLine("   Minute:       {0}", myCal.GetMinute(myDT))
      Console.WriteLine("   Second:       {0}", myCal.GetSecond(myDT))
      Console.WriteLine("   Milliseconds: {0}", myCal.GetMilliseconds(myDT))
      Console.WriteLine()
   End Sub
End Class
'This code produces the following output.
'
'April 3, 2002 of the Gregorian calendar:
'   Era:          1
'   Year:         2002
'   Month:        4
'   DayOfYear:    93
'   DayOfMonth:   3
'   DayOfWeek:    Wednesday
'   Hour:         0
'   Minute:       0
'   Second:       0
'   Milliseconds: 0
'
'After adding 5 to each component of the DateTime:
'   Era:          1
'   Year:         2007
'   Month:        10
'   DayOfYear:    286
'   DayOfMonth:   13
'   DayOfWeek:    Saturday
'   Hour:         5
'   Minute:       5
'   Second:       5
'   Milliseconds: 5
注解
日历将时间划分为多个单位,例如周、月和年。 除法的数量、长度和开始因每个日历而异。
注意
有关在 .NET 中使用日历类的信息,请参阅 使用日历。
任何时刻都可以使用特定日历表示为一组数值。 例如,公历 (1999 年 3、20、8、46、0、0、0.0) ,即 1999 年 3 月 20 日(C.E. 的 8:46:00:0.0) 的Calendar实现可以将特定日历范围内的任何日期映射到一组类似的数值,并且DateTime可以使用 和 DateTimeFormatInfo中的信息Calendar将此类数值集映射到文本表示形式。 文本表示形式可以区分区域性,例如,“1999 年 3 月 20 日上午 8:46 AD”表示 en-US 区域性,或者不区分区域性,例如 ISO 8601 格式的“1999-03-20T08:46:00”。
实现 Calendar 可以定义一个或多个纪元。 类 Calendar 将纪元标识为枚举整数,其中当前纪元 (CurrentEra) 的值为 0。
重要
日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。 例如,2019 年 5 月 1 日在 JapaneseCalendar 和 JapaneseLunisolarCalendar 中标志着令和年号的开始。 这种年号的变化会影响使用这些日历的所有应用程序。 有关详细信息并确定应用程序是否受到影响,请参阅 在 .NET 中处理日语日历中的新纪元。 有关在 Windows 系统上测试应用程序以确保其准备好进行纪元更改的信息,请参阅 准备应用程序应对日本纪元更改。 有关 .NET 中支持具有多个纪元的日历的功能以及使用支持多个纪元的日历时的最佳做法,请参阅 使用纪元。
为了弥补历年与地球绕太阳旋转的实际时间或月球绕地球旋转的实际时间之间的差异,闰年与标准日历年具有不同的天数。 每个 Calendar 实现都以不同的方式定义闰年。
为了保持一致性,每个间隔中的第一个单位 (第一个月,例如,) 分配值 1。
命名空间 System.Globalization 包括以下 Calendar 实现:
构造函数
| Calendar() | 初始化 Calendar 类的新实例。 | 
字段
| CurrentEra | 表示当前日历的当前纪元。 字段的值为 0。 | 
属性
| AlgorithmType | 获取一个值,该值指示当前日历是阳历、阴历还是二者的组合。 | 
| DaysInYearBeforeMinSupportedYear | 获取 MinSupportedDateTime 属性指定的年份之前的年中天数。 | 
| Eras | 当在派生类中重写时,获取当前日历中的纪元列表。 | 
| IsReadOnly | 获取一个值,该值指示此 Calendar 对象是否为只读。 | 
| MaxSupportedDateTime | 获取此 Calendar 对象支持的最晚日期和时间。 | 
| MinSupportedDateTime | 获取此 Calendar 对象支持的最早日期和时间。 | 
| TwoDigitYearMax | 获取或设置可以用两位数年份表示的 100 年范围内的最后一年。 | 
方法
适用于
另请参阅
- DateTime
- DateTimeFormatInfo
- ChineseLunisolarCalendar
- EastAsianLunisolarCalendar
- GregorianCalendar
- HebrewCalendar
- HijriCalendar
- JapaneseCalendar
- JapaneseLunisolarCalendar
- JulianCalendar
- KoreanCalendar
- KoreanLunisolarCalendar
- PersianCalendar
- TaiwanCalendar
- TaiwanLunisolarCalendar
- ThaiBuddhistCalendar
- UmAlQuraCalendar
- 使用日历