JapaneseCalendar.GetEra(DateTime) Metoda

Definicja

Zwraca erę w określonej erze 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

Parametry

time
DateTime

Element do odczytania DateTime .

Zwraca

Liczba całkowita reprezentująca erę w określonym obiekcie DateTime.

Wyjątki

DateTime Wynik jest poza obsługiwanym zakresem.

Przykłady

W poniższym przykładzie przedstawiono wartości kilku składników DateTime kalendarza japońskiego.

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();
}

int main()
{
   
   // Sets a DateTime to April 3, 2002 of the Gregorian calendar.
   DateTime myDT = DateTime(2002,4,3,gcnew GregorianCalendar);
   
   // Creates an instance of the JapaneseCalendar.
   JapaneseCalendar^ myCal = gcnew 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 );
}

/*
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

*/
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

Uwagi

Japoński kalendarz rozpoznaje jedną erę dla każdego panowania cesarza. Dwie najnowsze epoki to era Heisei, począwszy od gregoriańskiego roku kalendarzowego 1989, a era Reiwa, począwszy od gregoriańskiego roku kalendarzowego 2019. Nazwa ery jest zwykle wyświetlana przed rokiem. Na przykład rok kalendarzowy gregoriański 2001 to japoński rok kalendarzowy Heisei 13. Należy pamiętać, że pierwszy rok ery nosi nazwę "Gannen". Dlatego rok kalendarzowy gregoriański 1989 był japońskim rokiem kalendarzowym Heisei Gannen.

Ważne

Era w japońskich kalendarzach opiera się na panowaniu cesarza i dlatego oczekuje się, że się zmieni. Na przykład 1 maja 2019 r. oznaczyła początek ery Reiwa w JapaneseCalendar erze i JapaneseLunisolarCalendar. Taka zmiana ery wpływa na wszystkie aplikacje korzystające z tych kalendarzy. Aby uzyskać więcej informacji i określić, czy twoje aplikacje mają wpływ, zobacz Obsługa nowej ery w kalendarzu japońskim na platformie .NET. Aby uzyskać informacje na temat testowania aplikacji w systemach Windows w celu zapewnienia gotowości do zmiany epoki, zobacz Przygotowywanie aplikacji do japońskiej zmiany epoki. Aby uzyskać informacje o funkcjach na platformie .NET, które obsługują kalendarze z wieloma erami i najlepsze rozwiązania dotyczące pracy z kalendarzami obsługującymi wiele epok, zobacz Praca z erami.

Ta klasa przypisuje liczby do epoki w następujący sposób:

Wartość GetEra Nazwa ery Skrót ery Daty gregoriańskiego
5 令和 (Reiwa) 令和 (R, r) 1 maja 2019 r.
4 平成 (Heisei) 平 (H, h) 8 stycznia 1989 r. do 30 kwietnia 2019 r.
3 昭和 (Showa) 昭 (S, s) 25 grudnia 1926 r. do 7 stycznia 1989 r.
2 正 (Taisho) } (T, t) 30 lipca 1912 r. do 24 grudnia 1926 r.
1 明治 (Meiji) 明 (M, m) 8 września 1868 do 29 lipca 1912 r.

Zwykle JapaneseCalendar klasa obsługuje daty od 8 września w roku Meiji 1 (8 września 1868 roku kalendarza gregoriańskiego), czyli wartość MinSupportedDateTime właściwości. GetEra Jednak metoda pomyślnie zwraca erę dat od 1 stycznia do 7 września w roku Meiji 1 (1 stycznia 1868 do 7 września 1868 w kalendarzu gregoriańskim). W przypadku dat wcześniejszych niż 1 stycznia 1868 w kalendarzu gregoriańskim metoda zgłasza ArgumentOutOfRangeException wyjątek.

Dotyczy

Zobacz też