Calendar 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
以週、月、年等單位表示時間。
public ref class Calendar abstract
public ref class Calendar abstract : ICloneable
public abstract class Calendar
[System.Serializable]
public abstract class Calendar
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Calendar : ICloneable
public abstract class Calendar : ICloneable
type Calendar = class
[<System.Serializable>]
type Calendar = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Calendar = class
interface ICloneable
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
備註
日曆將時間劃分為單位,例如週、月和年。 各組別的數量、長度及起始日期在各日曆中有所不同。
Note
關於在.NET中使用日曆類別的資訊,請參見與日曆合作。
任何時刻都可以用特定的曆法表示為一組數值。 例如,在格里曆的(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 20 AD」,或是不文化敏感的,例如ISO 8601格式的「1999-03-20T08:46:00」。
一個 Calendar 實作可以定義一個或多個時代。 該 Calendar 類別將時代識別為列舉整數,其中當前時代(CurrentEra)值為0。
Important
日本日曆中的紀元基於皇帝的統治,因此預計將改變。 例如,2019 年 5 月 1 日在 JapaneseCalendar 和 JapaneseLunisolarCalendar中標示了 Reiwa 時代的開頭。 這種時代變更會影響使用這些行事曆的所有應用程式。 如需詳細資訊,以及判斷您的應用程式是否受到影響,請參閱 處理 .NET 中日曆中的新紀元。 如需在 Windows 系統上測試應用程式,以確保其準備迎接時代變更的相關信息,請參閱 準備您的應用程式以取得日本年號變更。 如需 .NET 中支援具有多個紀元的行事曆功能,以及使用支援多個紀元的行事曆時的最佳作法,請參閱 使用紀元。
為了彌補曆年與地球繞太陽公轉時間或月亮繞地球轉動時間之間的差異,閏年與標準曆年有不同的天數。 每個 Calendar 實施方式對閏年的定義都不同。
為了保持一致,每個區間的第一個單位(例如第一個月)會被賦予 1。
命名 System.Globalization 空間包含以下 Calendar 實作:
建構函式
| 名稱 | Description |
|---|---|
| Calendar() |
初始化 Calendar 類別的新執行個體。 |
欄位
| 名稱 | Description |
|---|---|
| CurrentEra |
代表當前曆法的時代。 此欄位的值為 0。 |
屬性
| 名稱 | Description |
|---|---|
| AlgorithmType |
會得到一個值,表示當前曆法是太陽、月亮或兩者結合。 |
| DaysInYearBeforeMinSupportedYear |
取得該房產指定 MinSupportedDateTime 年份之前的年份天數。 |
| Eras |
當在派生類別中覆寫時,會獲得當前日曆中的時代列表。 |
| IsReadOnly |
取得一個值,表示該 Calendar 物件是否為唯讀。 |
| MaxSupportedDateTime |
取得此 Calendar 物件支持的最新日期與時間。 |
| MinSupportedDateTime |
獲得此 Calendar 物件支持的最早日期與時間。 |
| TwoDigitYearMax |
取得或設定一個百年範圍的最後一年,該範圍可用兩位數年份表示。 |
方法
適用於
另請參閱
- DateTime
- DateTimeFormatInfo
- ChineseLunisolarCalendar
- EastAsianLunisolarCalendar
- GregorianCalendar
- HebrewCalendar
- HijriCalendar
- JapaneseCalendar
- JapaneseLunisolarCalendar
- JulianCalendar
- KoreanCalendar
- KoreanLunisolarCalendar
- PersianCalendar
- TaiwanCalendar
- TaiwanLunisolarCalendar
- ThaiBuddhistCalendar
- UmAlQuraCalendar
- 與日曆合作