JapaneseCalendar.GetEra(DateTime) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定した DateTimeの時代 (年号) を返します。
public:
override int GetEra(DateTime time);
public override int GetEra(DateTime time);
override this.GetEra : DateTime -> int
Public Overrides Function GetEra (time As DateTime) As Integer
パラメーター
返品
指定した DateTimeの時代 (年号) を表す整数。
例外
結果として得られる DateTime は、サポートされている範囲外です。
例
次の使用例は、 DateTime の複数のコンポーネントの値を日本語の暦で表示します。
using System;
using System.Globalization;
public class SamplesJapaneseCalendar {
public static void Main() {
// Sets a DateTime to April 3, 2002 of the Gregorian calendar.
DateTime myDT = new DateTime( 2002, 4, 3, new GregorianCalendar() );
// Creates an instance of the JapaneseCalendar.
JapaneseCalendar myCal = new JapaneseCalendar();
// Displays the values of the DateTime.
Console.WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Japanese calendar:" );
DisplayValues( myCal, myDT );
// Adds two years and ten months.
myDT = myCal.AddYears( myDT, 2 );
myDT = myCal.AddMonths( myDT, 10 );
// Displays the values of the DateTime.
Console.WriteLine( "After adding two years and ten months:" );
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();
}
}
/*
This code produces the following output.
April 3, 2002 of the Gregorian calendar equals the following in the Japanese calendar:
Era: 4
Year: 14
Month: 4
DayOfYear: 93
DayOfMonth: 3
DayOfWeek: Wednesday
After adding two years and ten months:
Era: 4
Year: 17
Month: 2
DayOfYear: 34
DayOfMonth: 3
DayOfWeek: Thursday
*/
Imports System.Globalization
Public Class SamplesJapaneseCalendar
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())
' Creates an instance of the JapaneseCalendar.
Dim myCal As New JapaneseCalendar()
' Displays the values of the DateTime.
Console.WriteLine("April 3, 2002 of the Gregorian calendar equals the following in the Japanese calendar:")
DisplayValues(myCal, myDT)
' Adds two years and ten months.
myDT = myCal.AddYears(myDT, 2)
myDT = myCal.AddMonths(myDT, 10)
' Displays the values of the DateTime.
Console.WriteLine("After adding two years and ten months:")
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()
End Sub
End Class
'This code produces the following output.
'
'April 3, 2002 of the Gregorian calendar equals the following in the Japanese calendar:
' Era: 4
' Year: 14
' Month: 4
' DayOfYear: 93
' DayOfMonth: 3
' DayOfWeek: Wednesday
'
'After adding two years and ten months:
' Era: 4
' Year: 17
' Month: 2
' DayOfYear: 34
' DayOfMonth: 3
' DayOfWeek: Thursday
注釈
日本暦は、天皇の治世ごとに1つの時代を認識しています。 2つの最も新しい時代は、グレゴリオ暦1989年から始まる平成時代と、グレゴリオ暦2019年から始まるレイワ時代です。 元号の名前は通常、年の前に表示されます。 たとえば、グレゴリオ暦の 2001 年は、日本の暦年平成 13 年です。 元号の最初の年は "元年" と呼ばれることに注意してください。したがって、グレゴリオ暦の1989年は、日本の暦年平成元年でした。
Important
日本暦の年号は天皇の治世に基づいているため、変化することが期待されます。 たとえば、2019 年 5 月 1 日は、 JapaneseCalendar と JapaneseLunisolarCalendarのレイワ時代の始まりを迎えています。 このような時代の変化は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細およびアプリケーションが影響を受けるかどうかを判断するには、「 .NET の日本語カレンダーでの新しい時代 (年号) の処理」を参照してください。 Windows システムでアプリケーションをテストし、時代 (年号) の変更に対する準備を行う方法については、「 日本の時代 (年号) に合わせてアプリケーションを準備する」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。
このクラスは、次のように時代 (年号) に数値を割り当てます。
| GetEra 値 | 時代 (年号) の名前 | 時代 (年号) の省略形 | グレゴリオ暦の日付 |
|---|---|---|---|
| 5 | 令和 (Reiwa) | 令和 (R, r) | 2019 年 5 月 1 日から現在 |
| 4 | 平成 | 平 (H,h) | 1989 年 1 月 8 日から 2019 年 4 月 30 日 |
| 3 | 昭和 (昭和) | 昭 (S, s) | 1926 年 12 月 25 日から 1989 年 1 月 7 日 |
| 2 | 大正 (大正) | 大 (T, t) | 1912 年 7 月 30 日から 1926 年 12 月 24 日 |
| 1 | 明治 (明治) | 明 (M, m) | 1868 年 10 月 23 日から 1912 年 7 月 29 日 |
通常、JapaneseCalendar クラスは、明治 1 年 (グレゴリオ暦の 1868 年 10 月 23 日) の 10 月 23 日からの日付をサポートします。これは、.NET 11 以降のバージョンの MinSupportedDateTime プロパティの値です。 しかし、 GetEra メソッドは、明治1年(1868年1月1日からグレゴリオ暦では1868年10月22日)の1月1日から10月22日までの日付の時代 (年号) を正常に返します。 グレゴリオ暦の 1868 年 1 月 1 日より前の日付の場合、メソッドは ArgumentOutOfRangeException 例外をスローします。