DateTimeFormatInfo.GetEra(String) Método

Definición

Devuelve el valor entero que representa la era especificada.

public:
 int GetEra(System::String ^ eraName);
public int GetEra (string eraName);
member this.GetEra : string -> int
Public Function GetEra (eraName As String) As Integer

Parámetros

eraName
String

Cadena que contiene el nombre de la era.

Devoluciones

El valor entero que representa la era, si eraName es válido; en caso contrario, -1.

Excepciones

eraName es null.

Ejemplos

En el ejemplo siguiente se muestra que DateTimeFormatInfo omite la puntuación en el nombre de la era, solo si el calendario es gregoriano y la referencia cultural usa el nombre de era "A.D.".

using namespace System;
using namespace System::Globalization;
using namespace System::Collections;
int main()
{
   
   // Creates strings with punctuation and without.
   String^ strADPunc = "A.D.";
   String^ strADNoPunc = "AD";
   String^ strCEPunc = "C.E.";
   String^ strCENoPunc = "CE";
   
   // Calls DTFI::GetEra for each culture that uses GregorianCalendar as the default calendar.
   Console::WriteLine( "            ----- AD -----  ----- CE -----" );
   Console::WriteLine( "CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR" );
   IEnumerator^ en = CultureInfo::GetCultures( CultureTypes::SpecificCultures )->GetEnumerator();
   while ( en->MoveNext() )
   {
      CultureInfo^ myCI = safe_cast<CultureInfo^>(en->Current);
      Console::Write( "{0, -12}", myCI );
      Console::Write( "{0,-7}{1,-9}", myCI->DateTimeFormat->GetEra( strADPunc ), myCI->DateTimeFormat->GetEra( strADNoPunc ) );
      Console::Write( "{0, -7}{1, -9}", myCI->DateTimeFormat->GetEra( strCEPunc ), myCI->DateTimeFormat->GetEra( strCENoPunc ) );
      Console::Write( "{0}", myCI->Calendar );
      Console::WriteLine();
   }
}

/*
This code produces the following output.  This output has been cropped for brevity.

            ----- AD -----  ----- CE -----
CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR
ar-SA       -1     -1       -1     -1       System.Globalization.HijriCalendar
ar-IQ       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-EG       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-LY       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-DZ       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-MA       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-TN       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-OM       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-YE       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-SY       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-JO       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-LB       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-KW       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-AE       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-BH       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-QA       1      1        -1     -1       System.Globalization.GregorianCalendar
bg-BG       1      1        -1     -1       System.Globalization.GregorianCalendar
ca-ES       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-TW       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-CN       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-HK       1      1        -1     -1       System.Globalization.GregorianCalendar
zh-SG       1      1        -1     -1       System.Globalization.GregorianCalendar
zh-MO       1      1        -1     -1       System.Globalization.GregorianCalendar
cs-CZ       -1     -1       -1     -1       System.Globalization.GregorianCalendar
da-DK       1      1        -1     -1       System.Globalization.GregorianCalendar

*/
using System;
using System.Globalization;

public class SamplesGregorianCalendar  {

   public static void Main()  {

      // Creates strings with punctuation and without.
      String strADPunc = "A.D.";
      String strADNoPunc = "AD";
      String strCEPunc = "C.E.";
      String strCENoPunc = "CE";

      // Calls DTFI.GetEra for each culture that uses GregorianCalendar as the default calendar.
      Console.WriteLine( "            ----- AD -----  ----- CE -----" );
      Console.WriteLine( "CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR" );
      foreach ( CultureInfo myCI in CultureInfo.GetCultures( CultureTypes.SpecificCultures ) )  {
         Console.Write( "{0,-12}", myCI );
         Console.Write( "{0,-7}{1,-9}", myCI.DateTimeFormat.GetEra( strADPunc ), myCI.DateTimeFormat.GetEra( strADNoPunc ) );
         Console.Write( "{0,-7}{1,-9}", myCI.DateTimeFormat.GetEra( strCEPunc ), myCI.DateTimeFormat.GetEra( strCENoPunc ) );
         Console.Write( "{0}", myCI.Calendar );
         Console.WriteLine();
      }
   }
}

/*
This code produces the following output.  This output has been cropped for brevity.

            ----- AD -----  ----- CE -----
CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR
ar-SA       -1     -1       -1     -1       System.Globalization.HijriCalendar
ar-IQ       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-EG       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-LY       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-DZ       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-MA       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-TN       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-OM       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-YE       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-SY       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-JO       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-LB       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-KW       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-AE       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-BH       1      1        -1     -1       System.Globalization.GregorianCalendar
ar-QA       1      1        -1     -1       System.Globalization.GregorianCalendar
bg-BG       1      1        -1     -1       System.Globalization.GregorianCalendar
ca-ES       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-TW       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-CN       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-HK       -1     -1       -1     -1       System.Globalization.GregorianCalendar
zh-SG       1      1        -1     -1       System.Globalization.GregorianCalendar
zh-MO       1      1        -1     -1       System.Globalization.GregorianCalendar
cs-CZ       -1     -1       -1     -1       System.Globalization.GregorianCalendar
da-DK       1      1        -1     -1       System.Globalization.GregorianCalendar

*/
Imports System.Globalization

Public Class SamplesGregorianCalendar

   Public Shared Sub Main()

      ' Creates strings with punctuation and without.
      Dim strADPunc As [String] = "A.D."
      Dim strADNoPunc As [String] = "AD"
      Dim strCEPunc As [String] = "C.E."
      Dim strCENoPunc As [String] = "CE"

      ' Calls DTFI.GetEra for each culture that uses GregorianCalendar as the default calendar.
      Console.WriteLine("            ----- AD -----  ----- CE -----")
      Console.WriteLine("CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR")
      Dim myCI As CultureInfo
      For Each myCI In  CultureInfo.GetCultures(CultureTypes.SpecificCultures)
         Console.Write("{0,-12}", myCI)
         Console.Write("{0,-7}{1,-9}", myCI.DateTimeFormat.GetEra(strADPunc), myCI.DateTimeFormat.GetEra(strADNoPunc))
         Console.Write("{0,-7}{1,-9}", myCI.DateTimeFormat.GetEra(strCEPunc), myCI.DateTimeFormat.GetEra(strCENoPunc))
         Console.Write("{0}", myCI.Calendar)
         Console.WriteLine()
      Next myCI

   End Sub

End Class


'This code produces the following output.  This output has been cropped for brevity.
'
'            ----- AD -----  ----- CE -----
'CULTURE     PUNC   NO PUNC  PUNC   NO PUNC  CALENDAR
'ar-SA       -1     -1       -1     -1       System.Globalization.HijriCalendar
'ar-IQ       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-EG       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-LY       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-DZ       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-MA       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-TN       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-OM       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-YE       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-SY       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-JO       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-LB       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-KW       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-AE       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-BH       1      1        -1     -1       System.Globalization.GregorianCalendar
'ar-QA       1      1        -1     -1       System.Globalization.GregorianCalendar
'bg-BG       1      1        -1     -1       System.Globalization.GregorianCalendar
'ca-ES       -1     -1       -1     -1       System.Globalization.GregorianCalendar
'zh-TW       -1     -1       -1     -1       System.Globalization.GregorianCalendar
'zh-CN       -1     -1       -1     -1       System.Globalization.GregorianCalendar
'zh-HK       -1     -1       -1     -1       System.Globalization.GregorianCalendar
'zh-SG       1      1        -1     -1       System.Globalization.GregorianCalendar
'zh-MO       1      1        -1     -1       System.Globalization.GregorianCalendar
'cs-CZ       -1     -1       -1     -1       System.Globalization.GregorianCalendar
'da-DK       1      1        -1     -1       System.Globalization.GregorianCalendar

Comentarios

El nombre de la era es el nombre que usa un calendario para hacer referencia a un período de tiempo que se calcula desde un punto fijo o evento. Por ejemplo, "A.D." o "C.E." es la era actual en el calendario gregoriano.

La comparación con eraName no distingue mayúsculas de minúsculas, por ejemplo, "A.D." equivale a "a.d.".

GetEra omite los signos de puntuación en los nombres de la era abreviada, solo si GregorianCalendar está seleccionado en DateTimeFormatInfo.Calendar y la referencia cultural usa "A.D." como nombre de era, es decir, "A.D." es equivalente a "AD".

GetEraeraName compara con el nombre de era completo devuelto por GetEraName y con el nombre abreviado de era devuelto por GetAbbreviatedEraName.

Importante

Las eras en los calendarios japoneses se basan en el reino del emperador y, por tanto, se espera que cambien. Por ejemplo, el 1 de mayo de 2019 marcaba el comienzo de la era Reiwa en JapaneseCalendar y JapaneseLunisolarCalendar. Este cambio de era afecta a todas las aplicaciones que usan estos calendarios. Para obtener más información y determinar si las aplicaciones se ven afectadas, consulte Control de una nueva era en el calendario japonés en .NET. Para obtener información sobre cómo probar las aplicaciones en sistemas Windows para garantizar su preparación para el cambio de era, consulte Preparación de la aplicación para el cambio en la era japonesa. Para conocer las características de .NET que admiten calendarios con varias eras y procedimientos recomendados al trabajar con calendarios que admiten varias eras, consulte Trabajar con eras.

Se aplica a

Consulte también