DateTimeFormatInfo.GetEra 方法
返回表示指定纪元的整数。
**命名空间:**System.Globalization
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Function GetEra ( _
eraName As String _
) As Integer
用法
Dim instance As DateTimeFormatInfo
Dim eraName As String
Dim returnValue As Integer
returnValue = instance.GetEra(eraName)
public int GetEra (
string eraName
)
public:
int GetEra (
String^ eraName
)
public int GetEra (
String eraName
)
public function GetEra (
eraName : String
) : int
参数
- eraName
包含纪元名称的字符串。
返回值
如果 eraName 有效,则为表示纪元的整数;否则为 -1。
异常
异常类型 | 条件 |
---|---|
eraName 为 空引用(在 Visual Basic 中为 Nothing)。 |
备注
纪元名称是日历用来指代从某个固定点或事件计算的一段时间的名称。例如,在公历中“A.D.”或“C.E.”就是当前纪元。
与 eraName 的比较是不区分大小写的;例如“A.D.”等效于“a.d.”。
只有当在 DateTimeFormatInfo.Calendar 中选择了 GregorianCalendar 并且区域性使用“A.D.”作为纪元名称时,GetEra 才忽略缩写纪元名称中的标点;即“A.D.”等效于“AD”。
GetEra 将 eraName 与 GetEraName 返回的完整纪元名称进行比较并与 GetAbbreviatedEraName 返回的缩写纪元名称进行比较。
示例
下面的代码示例显示,只有当日历为公历并且区域性使用纪元名称“A.D.”时,DateTimeFormatInfo 才忽略纪元名称中的标点。
Imports System
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 'Main
End Class 'SamplesGregorianCalendar
'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
*/
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
*/
import System.* ;
import System.Globalization.* ;
public class SamplesGregorianCalendar
{
public static void main(String[] args)
{
// 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");
for (int iCtr = 0; iCtr < (CultureInfo.GetCultures(
CultureTypes.SpecificCultures).length); iCtr++) {
CultureInfo myCI = CultureInfo.GetCultures(
CultureTypes.SpecificCultures)[iCtr];
Console.Write("{0,-12}", myCI);
Console.Write("{0,-7}{1,-9}", System.Convert.ToString(
myCI.get_DateTimeFormat().GetEra(strADPunc)),System.Convert.
ToString(myCI.get_DateTimeFormat().GetEra(strADNoPunc)));
Console.Write("{0,-7}{1,-9}", System.Convert.ToString(
myCI.get_DateTimeFormat().GetEra(strCEPunc)),
System.Convert.ToString(myCI.get_DateTimeFormat().
GetEra(strCENoPunc)));
Console.Write("{0}", myCI.get_Calendar());
Console.WriteLine();
}
} //main
} //SamplesGregorianCalendar
/*
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
*/
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
DateTimeFormatInfo 类
DateTimeFormatInfo 成员
System.Globalization 命名空间
GetEraName
Calendar 类