DateTimeFormatInfo.GetEra(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回表示指定纪元的整数。
public:
int GetEra(System::String ^ eraName);
public int GetEra (string eraName);
member this.GetEra : string -> int
Public Function GetEra (eraName As String) As Integer
参数
- eraName
- String
包含纪元名称的字符串。
返回
如果 eraName
有效,则为表示纪元的整数;否则为 -1。
例外
eraName
为 null
。
示例
以下示例显示, DateTimeFormatInfo 仅当日历为公历且区域性使用纪元名称“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
注解
纪元名称是日历用来引用从固定点或事件估计的时间段的名称。 例如,“A.D.”或“C.E.”是公历中的当前纪元。
与 eraName
的比较不区分大小写,例如,“A.D.”等效于“a.d.”。
GetEra 仅当 GregorianCalendar 在 中选择 DateTimeFormatInfo.Calendar 了 并且区域性使用“A.D.”作为纪元名称(即“A.D.”)等效于“AD”时,才忽略缩写纪元名称中的标点符号。
GetEraeraName
将 与 返回GetEraName的完整纪元名称进行比较,并将 与 返回GetAbbreviatedEraName的缩写纪元名称进行比较。
重要
日本历法中的年号是根据天皇统治来命名的,因此预计会发生变化。 例如,2019 年 5 月 1 日在 JapaneseCalendar 和 JapaneseLunisolarCalendar 中标志着令和年号的开始。 这种年号的变化会影响使用这些日历的所有应用程序。 有关详细信息并确定应用程序是否受到影响,请参阅 在 .NET 中处理日语日历中的新纪元。 有关在 Windows 系统上测试应用程序以确保其准备好进行纪元更改的信息,请参阅 准备应用程序应对日本纪元更改。 有关 .NET 中支持具有多个纪元的日历的功能以及使用支持多个纪元的日历时的最佳做法,请参阅 使用纪元。