Calendar 类
将时间分成段来表示,如分成星期、月和年。
**命名空间:**System.Globalization
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public MustInherit Class Calendar
Implements ICloneable
用法
Dim instance As Calendar
[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class Calendar : ICloneable
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class Calendar abstract : ICloneable
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public abstract class Calendar implements ICloneable
SerializableAttribute
ComVisibleAttribute(true)
public abstract class Calendar implements ICloneable
备注
日历将时间按单位(如星期、月和年)划分。每种日历中分成的段数、段的长度和起始点均不相同。
使用特定日历可以将任何时刻都表示为一组数值。例如,某次春分发生在公历的 (1999, 3, 20, 8, 46, 0, 0.0),即公元 1999 年 3 月 20 日 8:46:00:0.0。Calendar 的实现可以将特定日历范围内的任何日期映射到一个类似的数值集,并且 DateTime 可以使用 Calendar 和 DateTimeFormatInfo 中的信息将这些数值集映射为一种文本表示形式。文本表示形式既可以是区分区域性的(例如,按照 en-US 区域性表示的“8:46 AM March 20th 1999 AD”),也可以是不区分区域性的(例如,以 ISO 8601 格式表示的“1999-03-20T08:46:00”)。
一个 Calendar 实现可定义一个或多个纪元。Calendar 类将纪元标识为枚举的整数,其中当前纪元 (CurrentEra) 的值为 0。
闰年与标准历年的天数不同,这是为了弥补日历年与地球绕太阳旋转的实际时间或日历年与月亮绕地球旋转的实际时间之间的差距。各个 Calendar 实现分别对闰年作出了不同的定义。
为一致起见,每个时间间隔中的第一个单元(例如第一个月)被赋予值 1。
System.Globalization 命名空间包括下面的 Calendar 实现:GregorianCalendar、HebrewCalendar、HijriCalendar、JapaneseCalendar、JulianCalendar、KoreanCalendar、TaiwanCalendar 和 ThaiBuddhistCalendar。
示例
下面的代码示例介绍 Calendar 类的成员。
Imports System
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 'Main
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 'DisplayValues
End Class 'SamplesCalendar
'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
*/
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
*/
import System.* ;
import System.Globalization.* ;
public class SamplesCalendar
{
public static void main(String[] args)
{
// 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.get_InvariantCulture().get_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);
} //main
public static void DisplayValues(Calendar myCal, DateTime myDT)
{
Console.WriteLine(" Era: {0}",
System.Convert.ToString(myCal.GetEra(myDT)));
Console.WriteLine(" Year: {0}",
System.Convert.ToString(myCal.GetYear(myDT)));
Console.WriteLine(" Month: {0}",
System.Convert.ToString(myCal.GetMonth(myDT)));
Console.WriteLine(" DayOfYear: {0}",
System.Convert.ToString(myCal.GetDayOfYear(myDT)));
Console.WriteLine(" DayOfMonth: {0}",
System.Convert.ToString(myCal.GetDayOfMonth(myDT)));
Console.WriteLine(" DayOfWeek: {0}",
System.Convert.ToString(myCal.GetDayOfWeek(myDT)));
Console.WriteLine(" Hour: {0}",
System.Convert.ToString(myCal.GetHour(myDT)));
Console.WriteLine(" Minute: {0}",
System.Convert.ToString(myCal.GetMinute(myDT)));
Console.WriteLine(" Second: {0}",
System.Convert.ToString(myCal.GetSecond(myDT)));
Console.WriteLine(" Milliseconds: {0}",
System.Convert.ToString(myCal.GetMilliseconds(myDT)));
Console.WriteLine();
} //DisplayValues
} //SamplesCalendar
/*
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
*/
继承层次结构
System.Object
System.Globalization.Calendar
派生类
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
Calendar 成员
System.Globalization 命名空间
DateTime
DateTimeFormatInfo
GregorianCalendar
HebrewCalendar
HijriCalendar
JapaneseCalendar
JulianCalendar
KoreanCalendar
TaiwanCalendar
ThaiBuddhistCalendar