Calendar 类

定义

表示部门中的时间(如周、月和年)。

public ref class Calendar abstract
public ref class Calendar abstract : ICloneable
public abstract class Calendar
public abstract class Calendar : ICloneable
[System.Serializable]
public abstract class Calendar
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Calendar : ICloneable
type Calendar = class
type Calendar = class
    interface ICloneable
[<System.Serializable>]
type Calendar = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Calendar = class
    interface ICloneable
Public MustInherit Class Calendar
Public 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 日在 JapaneseCalendarJapaneseLunisolarCalendar 中标志着令和年号的开始。 这种年号的变化会影响使用这些日历的所有应用程序。 有关详细信息并确定应用程序是否受到影响,请参阅 在 .NET 中处理日语日历中的新纪元。 有关在 Windows 系统上测试应用程序以确保其为时代更改做好准备的信息,请参阅 为日本时代更改准备应用程序。 有关 .NET 中支持具有多个纪元的日历的功能以及使用支持多个纪元的日历时的最佳做法,请参阅 使用纪元

为了弥补历年与地球绕太阳旋转的实际时间或月球绕地球旋转的实际时间之间的差异,闰年具有与标准日历年不同的天数。 每个实现以 Calendar 不同的方式定义闰年。

为了保持一致性,每个间隔中的第一个单位 (第一个月,例如,) 赋值 1。

命名空间 System.Globalization 包括以下 Calendar 实现:

构造函数

Calendar()

初始化 Calendar 类的新实例。

字段

CurrentEra

表示当前日历的当前纪元。 字段的值为 0。

属性

AlgorithmType

获取一个值,该值指示当前日历是阳历、阴历还是二者的组合。

DaysInYearBeforeMinSupportedYear

获取 MinSupportedDateTime 属性指定的年份之前的年中天数。

Eras

当在派生类中重写时,获取当前日历中的纪元列表。

IsReadOnly

获取一个值,该值指示此 Calendar 对象是否为只读。

MaxSupportedDateTime

获取此 Calendar 对象支持的最晚日期和时间。

MinSupportedDateTime

获取此 Calendar 对象支持的最早日期和时间。

TwoDigitYearMax

获取或设置可以用两位数年份表示的 100 年范围内的最后一年。

方法

AddDays(DateTime, Int32)

返回与指定 DateTime 相距指定天数的 DateTime

AddHours(DateTime, Int32)

返回与指定 DateTime 相距指定小时数的 DateTime

AddMilliseconds(DateTime, Double)

返回与指定 DateTime 相距指定毫秒数的 DateTime

AddMinutes(DateTime, Int32)

返回与指定 DateTime 相距指定分钟数的 DateTime

AddMonths(DateTime, Int32)

当在派生类中重写时,将返回与指定 DateTime 相距指定月数的 DateTime

AddSeconds(DateTime, Int32)

返回与指定 DateTime 相距指定秒数的 DateTime

AddWeeks(DateTime, Int32)

返回与指定 DateTime 相距指定周数的 DateTime

AddYears(DateTime, Int32)

当在派生类中重写时,将返回与指定 DateTime 相距指定年数的 DateTime

Clone()

创建表示当前 Calendar 对象副本的新对象。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetDayOfMonth(DateTime)

当在派生类中重写时,将返回指定 DateTime 中的月中日期。

GetDayOfWeek(DateTime)

当在派生类中重写时,将返回指定 DateTime 中的周中日期。

GetDayOfYear(DateTime)

当在派生类中重写时,将返回指定 DateTime 中的年中日期。

GetDaysInMonth(Int32, Int32)

返回当前纪元的指定月份和年份中的天数。

GetDaysInMonth(Int32, Int32, Int32)

当在派生类中重写时,返回指定月份、纪元年份中的天数。

GetDaysInYear(Int32)

返回当前纪元中指定年份的天数。

GetDaysInYear(Int32, Int32)

当在派生类中重写时,返回指定纪元年份中的天数。

GetEra(DateTime)

当在派生类中重写时,将返回指定 DateTime 中的纪元。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetHour(DateTime)

返回指定 DateTime 中的小时值。

GetLeapMonth(Int32)

计算指定年份的闰月。

GetLeapMonth(Int32, Int32)

计算指定纪元年份的闰月。

GetMilliseconds(DateTime)

返回指定 DateTime 中的毫秒值。

GetMinute(DateTime)

返回指定 DateTime 中的分钟值。

GetMonth(DateTime)

当在派生类中重写时,将返回指定 DateTime 中的月份。

GetMonthsInYear(Int32)

返回当前纪元中指定年份的月数。

GetMonthsInYear(Int32, Int32)

当在派生类中重写时,将返回指定纪元中指定年份的月数。

GetSecond(DateTime)

返回指定 DateTime 中的秒值。

GetType()

获取当前实例的 Type

(继承自 Object)
GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek)

返回一年中包含指定 DateTime 值中的日期的那个星期。

GetYear(DateTime)

当在派生类中重写时,将返回指定 DateTime 中的年份。

IsLeapDay(Int32, Int32, Int32)

确定当前纪元中的指定日期是否为闰日。

IsLeapDay(Int32, Int32, Int32, Int32)

当在派生类中重写时,将确定指定纪元中的指定日期是否为闰日。

IsLeapMonth(Int32, Int32)

确定当前纪元中指定年份的指定月份是否为闰月。

IsLeapMonth(Int32, Int32, Int32)

当在派生类中重写时,将确定指定纪元中指定年份的指定月份是否为闰月。

IsLeapYear(Int32)

确定当前纪元中的指定年份是否为闰年。

IsLeapYear(Int32, Int32)

当在派生类中重写时,将确定指定纪元中的指定年份是否为闰年。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ReadOnly(Calendar)

返回指定的 Calendar 对象的只读版本。

ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32)

返回设置为当前纪元中指定日期和时间的 DateTime

ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)

当在派生类中重写时,将返回设置为指定纪元中指定日期和时间的 DateTime

ToFourDigitYear(Int32)

使用 TwoDigitYearMax 属性将指定的年份转换为四位数年份,以确定相应的纪元。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅