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 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 中使用日历类的信息,请参阅 使用 Calendars

任何时间刻都可以表示为使用特定日历的一组数值。 例如,在公历的(1999、3、20、8、46、0、0、0.0)发生,即 1999 年 3 月 20 日 C.E. 8:46:00:0.0。 实现可以将Calendar特定日历范围中的任何日期映射到类似的数值集,并且DateTime可以使用来自和Calendar的信息DateTimeFormatInfo将此类数值集映射到文本表示形式。 文本表示形式可以是区分区域性的,例如,1999 年 3 月 20 日上午 8:46 AD;对于 en-US 区域性,或者不区分区域性,例如 ISO 8601 格式的“1999-03-20T08:46:00”。

Calendar实现可以定义一个或多个纪元。 该 Calendar 类将纪元标识为枚举整数,其中当前纪元 (CurrentEra) 的值为 0。

Important

日本日历中的纪元基于皇帝的统治,因此预计将改变。 例如,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 年范围的上一年,该范围可以由 2 位年份表示。

方法

名称 说明
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, Int32)

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

GetDaysInMonth(Int32, Int32)

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

GetDaysInYear(Int32, Int32)

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

GetDaysInYear(Int32)

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

GetEra(DateTime)

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

GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetHour(DateTime)

返回指定 DateTime中的小时值。

GetLeapMonth(Int32, Int32)

计算指定年份和纪元的跃点月。

GetLeapMonth(Int32)

计算指定年份的跃点月。

GetMilliseconds(DateTime)

返回指定 DateTime中的毫秒值。

GetMinute(DateTime)

返回指定 DateTime中的分钟值。

GetMonth(DateTime)

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

GetMonthsInYear(Int32, Int32)

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

GetMonthsInYear(Int32)

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

GetSecond(DateTime)

返回指定 DateTime中的秒值。

GetType()

获取当前实例的 Type

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

返回包含指定 DateTime 值中的日期的年份的周。

GetYear(DateTime)

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

IsLeapDay(Int32, Int32, Int32, Int32)

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

IsLeapDay(Int32, Int32, Int32)

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

IsLeapMonth(Int32, Int32, Int32)

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

IsLeapMonth(Int32, Int32)

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

IsLeapYear(Int32, Int32)

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

IsLeapYear(Int32)

确定当前时代的指定年份是否为跃年。

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ReadOnly(Calendar)

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

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

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

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

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

ToFourDigitYear(Int32)

通过使用属性来确定相应的世纪,将 TwoDigitYearMax 指定年份转换为四位数年份。

ToString()

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

(继承自 Object)

适用于

另请参阅