System.Globalization 命名空间

System.Globalization 命名空间包含定义区域性相关信息的类,这些信息包括语言、国家/地区、使用的日历、日期、货币和数字的格式模式以及字符串的排序顺序。我们可以使用这些类编写全球化(国际化)应用程序。而像 StringInfoTextInfo 这样的类更是为我们提供了诸如代理项支持和文本元素处理等高级全球化功能。

说明
Calendar将时间分成段来表示,如分成星期、月和年。
CompareInfo实现一组方法进行区分区域性的字符串比较。
CultureInfo表示有关特定区域性的信息,包括区域性的名称、书写体系和使用的日历,以及有关对常用操作(如格式化日期和排序字符串)提供信息的区域性特定对象的访问。
DateTimeFormatInfo定义如何根据区域性设置 DateTime 值的格式并显示这些值。
DaylightTime定义夏时制周期。
GregorianCalendar表示公历。
HebrewCalendar表示犹太历。
HijriCalendar表示回历。
JapaneseCalendar表示日本历。
JulianCalendar表示儒略历。
KoreanCalendar表示朝鲜历。
NumberFormatInfo根据区域性定义如何设置数值格式以及如何显示数值。
RegionInfo包含有关国家/地区的信息。
SortKey表示将字符串映射到其排序关键字的映射结果。
StringInfo提供功能将字符串拆分为文本元素并循环访问这些文本元素。
TaiwanCalendar表示台湾地区日历。
TextElementEnumerator枚举字符串的文本元素。
TextInfo定义特定于书写体系的属性和行为(如大小写)。
ThaiBuddhistCalendar表示泰国佛历。

枚举

枚举说明
CalendarWeekRule定义确定年份第一周的不同规则。
CompareOptions定义要用于 CompareInfo 的字符串比较选项。
CultureTypes定义可以使用 CultureInfo.GetCultures 检索的区域性列表的类型。
DateTimeStyles定义一些格式设置选项,这些选项自定义 DateTime.ParseDateTime.ParseExact 方法如何分析一个字符串。
GregorianCalendarTypes定义公历的不同语言版本。
NumberStyles确定数字字符串参数中允许的样式,这些参数被传递到数字基类型类的 Parse 方法。
UnicodeCategory定义字符的 Unicode 类别。

CultureInfo 类

表示有关特定区域性的信息,包括区域性的名称、书写体系和使用的日历,以及有关对常用操作(如格式化日期和排序字符串)提供信息的区域性特定对象的访问。

有关此类型所有成员的列表,请参阅 CultureInfo 成员

System.Object
   System.Globalization.CultureInfo

[Visual Basic]
<Serializable>
Public Class CultureInfo
Implements ICloneable, IFormatProvider
[C#]
[Serializable]
public class CultureInfo : ICloneable, IFormatProvider
[C++]
[Serializable]
public __gc class CultureInfo : public ICloneable, IFormatProvider
[JScript]
public
Serializable
class CultureInfo implements ICloneable, IFormatProvider
线程安全

此类型的所有公共静态(Visual Basic 中为 Shared)成员对多线程操作而言都是安全的。但不保证任何实例成员是线程安全的。

备注

CultureInfo 类保存区域性特定的信息,如关联的语言、子语言、国家/地区、日历和区域性约定。此类还提供对 DateTimeFormatInfoNumberFormatInfoCompareInfoTextInfo 的区域性特定实例的访问。这些对象包含区域性特定操作(如大小写、格式化日期和数字以及比较字符串)所需的信息。

String 类间接地使用此类来获取有关默认区域性的信息。

区域性名称遵循 RFC 1766 标准,格式为“<languagecode2>-<country/regioncode2>”,其中 <languagecode2> 是从 ISO 639-1 派生的由两个小写字母构成的代码,<country/regioncode2> 是从 ISO 3166 派生的由两个大写字母构成的代码。例如,美国英语为“en-US”。在双字母语言代码不可用的情况中,将使用从 ISO 639-2 派生的三字母代码;例如,三字母代码“div”用于使用 Dhivehi 语言的区域。某些区域性名称带有指定书写符号的后缀;例如“-Cyrl”指定西里尔语书写符号,“-Latn”指定拉丁语书写符号。

此类和 System.Globalization 命名空间中的其他类接受并使用下列预定义的 CultureInfo 名称和标识符。

区域性名称区域性标识符语言-国家/地区
""(空字符串)0x007F固定区域性
af0x0036南非荷兰语
af-ZA0x0436南非荷兰语 - 南非
sq0x001C阿尔巴尼亚语
sq-AL0x041C阿尔巴尼亚语 - 阿尔巴尼亚
ar0x0001阿拉伯语
ar-DZ0x1401阿拉伯语 - 阿尔及利亚
ar-BH0x3C01阿拉伯语 - 巴林
ar-EG0x0C01阿拉伯语 - 埃及
ar-IQ0x0801阿拉伯语 - 伊拉克
ar-JO0x2C01阿拉伯语 - 约旦
ar-KW0x3401阿拉伯语 - 科威特
ar-LB0x3001阿拉伯语 - 黎巴嫩
ar-LY0x1001阿拉伯语 - 利比亚
ar-MA0x1801阿拉伯语 - 摩洛哥
ar-OM0x2001阿拉伯语 - 阿曼
ar-QA0x4001阿拉伯语 - 卡塔尔
ar-SA0x0401阿拉伯语 - 沙特阿拉伯
ar-SY0x2801阿拉伯语 - 叙利亚
ar-TN0x1C01阿拉伯语 - 突尼斯
ar-AE0x3801阿拉伯语 - 阿拉伯联合酋长国
ar-YE0x2401阿拉伯语 - 也门
hy0x002B亚美尼亚语
hy-AM0x042B亚美尼亚语 - 亚美尼亚
az0x002C阿泽里语
az-AZ-Cyrl0x082C阿泽里语(西里尔语)- 阿塞拜疆
az-AZ-Latn0x042C阿泽里语(拉丁)- 阿塞拜疆
eu0x002D巴斯克语
eu-ES0x042D巴斯克语 - 巴斯克地区
be0x0023白俄罗斯语
be-BY0x0423白俄罗斯语 - 白俄罗斯
bg0x0002保加利亚语
bg-BG0x0402保加利亚语 - 保加利亚
ca0x0003加泰罗尼亚语
ca-ES0x0403加泰罗尼亚语 - 加泰罗尼亚地区
zh-HK0x0C04中文 - 香港特别行政区
zh-MO0x1404中文 - 澳门特别行政区
zh-CN0x0804中文 - 中国
zh-CHS0x0004中文(简体)
zh-SG0x1004中文 - 新加坡
zh-TW0x0404中文 - 台湾
zh-CHT0x7C04中文(繁体)
hr0x001A克罗地亚语
hr-HR0x041A克罗地亚语 - 克罗地亚
cs0x0005捷克语
cs-CZ0x0405捷克语 - 捷克共和国
da0x0006丹麦语
da-DK0x0406丹麦语 - 丹麦
div0x0065马尔代夫语
div-MV0x0465马尔代夫语 - 马尔代夫
nl0x0013荷兰语
nl-BE0x0813荷兰语 - 比利时
nl-NL0x0413荷兰语 - 荷兰
en0x0009英语
en-AU0x0C09英语 - 澳大利亚
en-BZ0x2809英语 - 伯利兹
en-CA0x1009英语 - 加拿大
en-CB0x2409英语 - 加勒比
en-IE0x1809英语 - 爱尔兰
en-JM0x2009英语 - 牙买加
en-NZ0x1409英语 - 新西兰
en-PH0x3409英语 - 菲律宾
en-ZA0x1C09英语 - 南非
en-TT0x2C09英语 - 特立尼达和多巴哥
en-GB0x0809英语 - 英国
en-US0x0409英语 - 美国
en-ZW0x3009英语 - 津巴布韦
et0x0025爱沙尼亚语
et-EE0x0425爱沙尼亚语 - 爱沙尼亚
fo0x0038法罗语
fo-FO0x0438法罗语 - 法罗群岛
fa0x0029波斯语
fa-IR0x0429波斯语 - 伊朗
fi0x000B芬兰语
fi-FI0x040B芬兰语 - 芬兰
fr0x000C法语
fr-BE0x080C法语 - 比利时
fr-CA0x0C0C法语 - 加拿大
fr-FR0x040C法语 - 法国
fr-LU0x140C法语 - 卢森堡
fr-MC0x180C法语 - 摩纳哥
fr-CH0x100C法语 - 瑞士
gl0x0056加利西亚语
gl-ES0x0456加利西亚语 - 加利西亚地区
ka0x0037格鲁吉亚语
ka-GE0x0437格鲁吉亚语 - 格鲁吉亚
de0x0007德语
de-AT0x0C07德语 - 奥地利
de-DE0x0407德语 - 德国
de-LI0x1407德语 - 列支敦士登
de-LU0x1007德语 - 卢森堡
de-CH0x0807德语 - 瑞士
el0x0008希腊语
el-GR0x0408希腊语 - 希腊
gu0x0047古吉拉特语
gu-IN0x0447古吉拉特语 - 印度
he0x000D希伯来语
he-IL0x040D希伯来语 - 以色列
hi0x0039印地语
hi-IN0x0439印地语 - 印度
hu0x000E匈牙利语
hu-HU0x040E匈牙利语 - 匈牙利
is0x000F冰岛语
is-IS0x040F冰岛语 - 冰岛
id0x0021印度尼西亚语
id-ID0x0421印度尼西亚语 - 印度尼西亚
it0x0010意大利语
it-IT0x0410意大利语 - 意大利
it-CH0x0810意大利语 - 瑞士
ja0x0011日语
ja-JP0x0411日语 - 日本
kn0x004B卡纳达语
kn-IN0x044B卡纳达语 - 印度
kk0x003F哈萨克语
kk-KZ0x043F哈萨克语 - 哈萨克斯坦
kok0x0057贡根语
kok-IN0x0457贡根语 - 印度
ko0x0012朝鲜语
ko-KR0x0412朝鲜语 - 韩国
ky0x0040吉尔吉斯语
ky-KZ0x0440吉尔吉斯语 - 吉尔吉斯坦
lv0x0026拉脱维亚语
lv-LV0x0426拉脱维亚语 - 拉脱维亚
lt0x0027立陶宛语
lt-LT0x0427立陶宛语 - 立陶宛
mk0x002F马其顿语
mk-MK0x042F马其顿语 - FYROM
ms0x003E马来语
ms-BN0x083E马来语 - 文莱
ms-MY0x043E马来语 - 马来西亚
mr0x004E马拉地语
mr-IN0x044E马拉地语 - 印度
mn0x0050蒙古语
mn-MN0x0450蒙古语 - 蒙古
no0x0014挪威语
nb-NO0x0414挪威语(博克马尔)- 挪威
nn-NO0x0814挪威语(尼诺斯克)- 挪威
pl0x0015波兰语
pl-PL0x0415波兰语 - 波兰
pt0x0016葡萄牙语
pt-BR0x0416葡萄牙语 - 巴西
pt-PT0x0816葡萄牙语 - 葡萄牙
pa0x0046旁遮普语
pa-IN0x0446旁遮普语 - 印度
ro0x0018罗马尼亚语
ro-RO0x0418罗马尼亚语 - 罗马尼亚
ru0x0019俄语
ru-RU0x0419俄语 - 俄罗斯
sa0x004F梵语
sa-IN0x044F梵语 - 印度
sr-SP-Cyrl0x0C1A塞尔维亚语(西里尔语)- 塞尔维亚
sr-SP-Latn0x081A塞尔维亚语(拉丁)- 塞尔维亚
sk0x001B斯洛伐克语
sk-SK0x041B斯洛伐克语 - 斯洛伐克
sl0x0024斯洛文尼亚语
sl-SI0x0424斯洛文尼亚语 - 斯洛文尼亚
es0x000A西班牙语
es-AR0x2C0A西班牙语 - 阿根廷
es-BO0x400A西班牙语 - 玻利维亚
es-CL0x340A西班牙语 - 智利
es-CO0x240A西班牙语 - 哥伦比亚
es-CR0x140A西班牙语 - 哥斯达黎加
es-DO0x1C0A西班牙语 - 多米尼加共和国
es-EC0x300A西班牙语 - 厄瓜多尔
es-SV0x440A西班牙语 - 萨尔瓦多
es-GT0x100A西班牙语 - 危地马拉
es-HN0x480A西班牙语 - 洪都拉斯
es-MX0x080A西班牙语 - 墨西哥
es-NI0x4C0A西班牙语 - 尼加拉瓜
es-PA0x180A西班牙语 - 巴拿马
es-PY0x3C0A西班牙语 - 巴拉圭
es-PE0x280A西班牙 - 秘鲁
es-PR0x500A西班牙语 - 波多黎各
es-ES0x0C0A西班牙语 - 西班牙
es-UY0x380A西班牙语 - 乌拉圭
es-VE0x200A西班牙语 - 委内瑞拉
sw0x0041斯瓦希里语
sw-KE0x0441斯瓦希里语 - 肯尼亚
sv0x001D瑞典语
sv-FI0x081D瑞典语 - 芬兰
sv-SE0x041D瑞典语 - 瑞典
syr0x005A叙利亚语
syr-SY0x045A叙利亚语 - 叙利亚
ta0x0049泰米尔语
ta-IN0x0449泰米尔语 - 印度
tt0x0044鞑靼语
tt-RU0x0444鞑靼语 - 俄罗斯
te0x004A泰卢固语
te-IN0x044A泰卢固语 - 印度
th0x001E泰语
th-TH0x041E泰语 - 泰国
tr0x001F土耳其语
tr-TR0x041F土耳其语 - 土耳其
uk0x0022乌克兰语
uk-UA0x0422乌克兰语 - 乌克兰
ur0x0020乌尔都语
ur-PK0x0420乌尔都语 - 巴基斯坦
uz0x0043乌兹别克语
uz-UZ-Cyrl0x0843乌兹别克语(西里尔语)- 乌兹别克斯坦
uz-UZ-Latn0x0443乌兹别克语(拉丁)- 乌兹别克斯坦
vi0x002A越南语
vi-VN0x042A越南语 - 越南

“西班牙语 - 西班牙”的区域性标识符“0x0c0a”使用默认的国际排序顺序;而区域性标识符“0x040A”(也指“西班牙语 - 西班牙”)则使用传统排序顺序。如果使用“es-ES”区域性名称构造 CultureInfo,则新的 CultureInfo 将使用默认的国际排序顺序。要构造一个使用传统排序顺序的 CultureInfo,可在构造函数中使用区域性标识符“0x040A”。有关具有备用排序方法的其他区域性的更多信息,请参见“使用备用排序顺序”。

这些区域性通常分为三个集合:固定区域性、非特定区域性和特定区域性。

固定区域性不区分区域性。可以使用空字符串 ("") 按名称或者按区域性标识符 0x007F 来指定固定区域性。InvariantCulture 检索固定区域性的实例。它与英语语言关联,但不与任何国家/地区关联。它几乎可用在要求区域性的“全局化”命名空间中的所有方法中。如果安全决策取决于字符串比较或大小写更改操作,则使用 InvariantCulture 确保无论如何设置系统的区域性,行为都将保持一致。但是,固定区域性必须仅由需要不依赖区域性的结果的进程(如系统服务)使用;否则,它得到的结果可能在语言上不正确,或者在文化上不合适。

非特定区域性是与某种语言关联但不与国家/地区关联的区域性。特定区域性是与某种语言和某个国家/地区关联的区域性。例如,“fr”是非特定区域性,而“fr-FR”是特定区域性。注意,“zh-CHS”(简体中文)和“zh-CHT”(繁体中文)均为非特定区域性。

区域性有层次结构,即特定区域性的父级是非特定区域性,而非特定区域性的父级是 InvariantCulture。Parent 属性将返回与特定区域性关联的非特定区域性。

如果特定区域性的资源在系统中不可用,则使用非特定区域性的资源;如果非特定区域性的资源也不可用,那么使用主程序集中嵌入的资源。有关资源备用进程的更多信息,请参见“资源备用进程”。

Windows API 中的区域性列表与 .NET Framework 中的区域性列表略有不同。例如,在 Windows API 中未提供带有区域性标识符 0x7C04 的非特定区域性 zh-CHT“中文(繁体)”。如果需要具备同 Windows 进行互操作的能力(例如,通过 p/invoke 机制),可使用在 .NET Framework 中定义的特定区域性。这样可以确保与等效的 Windows 区域设置保持一致,它们使用同一 LCID 进行标识。

只能为固定区域性或特定区域性创建 DateTimeFormatInfo 或 NumberFormatInfo,而不能为非特定区域性创建它们。

用户可以通过“控制面板”中的“区域和语言选项”(或“区域选项”或“区域设置”)选择重写某些与 Windows 的当前区域性关联的值。例如,用户可能选择以另一种格式显示日期,或选择使用区域性默认设置以外的货币。

如果 UseUserOverride 为 true 并且指定的区域性与当前 Windows 的区域性匹配,则 CultureInfo 使用那些重写,包括以下属性的用户设置:由 DateTimeFormat 属性返回的 DateTimeFormatInfo 实例的属性、由 NumberFormat 属性返回的 NumberFormatInfo 实例的属性,以及由 CompareInfo 属性返回的 CompareInfo 实例的属性。如果用户设置与 CultureInfo 的关联区域性不兼容(例如选定的日历不属于 OptionalCalendars 其中之一),则方法结果和属性值是未定义的。

对于使用欧元的区域性,.NET Framework 和 Windows XP 将默认货币设置为欧元;但是,旧版本的 Windows 却不是这样。因此,如果旧版本 Windows 的用户未通过“控制面板”中的“区域选项”或“区域设置”更改货币设置,则货币可能不正确。若要使用 .NET Framework 的货币的默认设置,请使用接受 useUserOverride 参数的 CultureInfo 构造函数重载,并将该参数设置为 false。

该类实现 ICloneable 接口以启用 CultureInfo 对象的复制。它还实现 IFormatProvider 以便为应用程序提供格式化信息。

示例

[Visual Basic, C#, C++] 下面的代码示例显示如何按国际排序创建一个用于“西班牙语-西班牙”的 CultureInfo,并按传统排序创建另一个 CultureInfo。

[Visual Basic]
Imports System
Imports System.Collections
Imports System.Globalization
Public Class SamplesCultureInfo
Public Shared Sub Main()
' Creates and initializes the CultureInfo which uses the international sort.
Dim myCIintl As New CultureInfo("es-ES", False)
' Creates and initializes the CultureInfo which uses the traditional sort.
Dim myCItrad As New CultureInfo(&H40A, False)
' Displays the properties of each culture.
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "PROPERTY", "INTERNATIONAL", "TRADITIONAL")
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "CompareInfo", myCIintl.CompareInfo, myCItrad.CompareInfo)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "DisplayName", myCIintl.DisplayName, myCItrad.DisplayName)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "EnglishName", myCIintl.EnglishName, myCItrad.EnglishName)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "IsNeutralCulture", myCIintl.IsNeutralCulture, myCItrad.IsNeutralCulture)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "IsReadOnly", myCIintl.IsReadOnly, myCItrad.IsReadOnly)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "LCID", myCIintl.LCID, myCItrad.LCID)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "Name", myCIintl.Name, myCItrad.Name)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "NativeName", myCIintl.NativeName, myCItrad.NativeName)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "Parent", myCIintl.Parent, myCItrad.Parent)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "TextInfo", myCIintl.TextInfo, myCItrad.TextInfo)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "ThreeLetterISOLanguageName", myCIintl.ThreeLetterISOLanguageName, myCItrad.ThreeLetterISOLanguageName)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "ThreeLetterWindowsLanguageName", myCIintl.ThreeLetterWindowsLanguageName, myCItrad.ThreeLetterWindowsLanguageName)
Console.WriteLine("{0,-33}{1,-25}{2,-25}", "TwoLetterISOLanguageName", myCIintl.TwoLetterISOLanguageName, myCItrad.TwoLetterISOLanguageName)
Console.WriteLine()
' Compare two strings using myCIintl.
Console.WriteLine("Comparing ""llegar"" and ""lugar""")
Console.WriteLine("   With myCIintl.CompareInfo.Compare: {0}", myCIintl.CompareInfo.Compare("llegar", "lugar"))
Console.WriteLine("   With myCItrad.CompareInfo.Compare: {0}", myCItrad.CompareInfo.Compare("llegar", "lugar"))
End Sub 'Main
End Class 'SamplesCultureInfo
'This code produces the following output.
'
'PROPERTY                         INTERNATIONAL            TRADITIONAL
'CompareInfo                      CompareInfo - 3082       CompareInfo - 1034
'DisplayName                      Spanish (Spain)          Spanish (Spain)
'EnglishName                      Spanish (Spain)          Spanish (Spain)
'IsNeutralCulture                 False                    False
'IsReadOnly                       False                    False
'LCID                             3082                     1034
'Name                             es-ES                    es-ES
'NativeName                       espaol (Espaa)         espaol (Espaa)
'Parent                           es                       es
'TextInfo                         TextInfo - 3082          TextInfo - 1034
'ThreeLetterISOLanguageName       spa                      spa
'ThreeLetterWindowsLanguageName   ESN                      ESN
'TwoLetterISOLanguageName         es                       es
'
'Comparing "llegar" and "lugar"
'   With myCIintl.CompareInfo.Compare: -1
'   With myCItrad.CompareInfo.Compare: 1
[C#]
using System;
using System.Collections;
using System.Globalization;
public class SamplesCultureInfo  {
public static void Main()  {
// Creates and initializes the CultureInfo which uses the international sort.
CultureInfo myCIintl = new CultureInfo( "es-ES", false );
// Creates and initializes the CultureInfo which uses the traditional sort.
CultureInfo myCItrad = new CultureInfo( 0x040A, false );
// Displays the properties of each culture.
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "PROPERTY", "INTERNATIONAL", "TRADITIONAL" );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "CompareInfo", myCIintl.CompareInfo, myCItrad.CompareInfo );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "DisplayName", myCIintl.DisplayName, myCItrad.DisplayName );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "EnglishName", myCIintl.EnglishName, myCItrad.EnglishName );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "IsNeutralCulture", myCIintl.IsNeutralCulture, myCItrad.IsNeutralCulture );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "IsReadOnly", myCIintl.IsReadOnly, myCItrad.IsReadOnly );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "LCID", myCIintl.LCID, myCItrad.LCID );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "Name", myCIintl.Name, myCItrad.Name );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "NativeName", myCIintl.NativeName, myCItrad.NativeName );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "Parent", myCIintl.Parent, myCItrad.Parent );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "TextInfo", myCIintl.TextInfo, myCItrad.TextInfo );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "ThreeLetterISOLanguageName", myCIintl.ThreeLetterISOLanguageName, myCItrad.ThreeLetterISOLanguageName );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "ThreeLetterWindowsLanguageName", myCIintl.ThreeLetterWindowsLanguageName, myCItrad.ThreeLetterWindowsLanguageName );
Console.WriteLine( "{0,-33}{1,-25}{2,-25}", "TwoLetterISOLanguageName", myCIintl.TwoLetterISOLanguageName, myCItrad.TwoLetterISOLanguageName );
Console.WriteLine();
// Compare two strings using myCIintl.
Console.WriteLine( "Comparing \"llegar\" and \"lugar\"" );
Console.WriteLine( "   With myCIintl.CompareInfo.Compare: {0}", myCIintl.CompareInfo.Compare( "llegar", "lugar" ) );
Console.WriteLine( "   With myCItrad.CompareInfo.Compare: {0}", myCItrad.CompareInfo.Compare( "llegar", "lugar" ) );
}
}
/*
This code produces the following output.
PROPERTY                         INTERNATIONAL            TRADITIONAL
CompareInfo                      CompareInfo - 3082       CompareInfo - 1034
DisplayName                      Spanish (Spain)          Spanish (Spain)
EnglishName                      Spanish (Spain)          Spanish (Spain)
IsNeutralCulture                 False                    False
IsReadOnly                       False                    False
LCID                             3082                     1034
Name                             es-ES                    es-ES
NativeName                       espaol (Espaa)         espaol (Espaa)
Parent                           es                       es
TextInfo                         TextInfo - 3082          TextInfo - 1034
ThreeLetterISOLanguageName       spa                      spa
ThreeLetterWindowsLanguageName   ESN                      ESN
TwoLetterISOLanguageName         es                       es
Comparing "llegar" and "lugar"
With myCIintl.CompareInfo.Compare: -1
With myCItrad.CompareInfo.Compare: 1
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
int main()
{
// Creates and initializes the CultureInfo which uses the international sort.
CultureInfo* myCIintl = new CultureInfo(S"es-ES", false);
// Creates and initializes the CultureInfo which uses the traditional sort.
CultureInfo* myCItrad = new CultureInfo(0x040A, false);
// Displays the properties of each culture.
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"PROPERTY", S"INTERNATIONAL", S"TRADITIONAL");
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"CompareInfo", myCIintl -> CompareInfo, myCItrad -> CompareInfo);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"DisplayName", myCIintl -> DisplayName, myCItrad -> DisplayName);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"EnglishName", myCIintl -> EnglishName, myCItrad -> EnglishName);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"IsNeutralCulture", __box(myCIintl -> IsNeutralCulture), __box(myCItrad -> IsNeutralCulture));
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"IsReadOnly", __box(myCIintl -> IsReadOnly), __box(myCItrad -> IsReadOnly));
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"LCID", __box(myCIintl -> LCID), __box(myCItrad -> LCID));
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"Name", myCIintl -> Name, myCItrad -> Name);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"NativeName", myCIintl -> NativeName, myCItrad -> NativeName);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"Parent", myCIintl -> Parent, myCItrad -> Parent);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"TextInfo", myCIintl -> TextInfo, myCItrad -> TextInfo);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"ThreeLetterISOLanguageName", myCIintl -> ThreeLetterISOLanguageName, myCItrad -> ThreeLetterISOLanguageName);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"ThreeLetterWindowsLanguageName", myCIintl -> ThreeLetterWindowsLanguageName, myCItrad -> ThreeLetterWindowsLanguageName);
Console::WriteLine(S" {0, -33} {1, -25} {2, -25}", S"TwoLetterISOLanguageName", myCIintl -> TwoLetterISOLanguageName, myCItrad -> TwoLetterISOLanguageName);
Console::WriteLine();
// Compare two strings using myCIintl ->
Console::WriteLine(S"Comparing \"llegar\" and \"lugar\"");
Console::WriteLine(S"   With myCIintl -> CompareInfo -> Compare: {0}", __box(myCIintl -> CompareInfo -> Compare(S"llegar", S"lugar")));
Console::WriteLine(S"   With myCItrad -> CompareInfo -> Compare: {0}", __box(myCItrad -> CompareInfo -> Compare(S"llegar", S"lugar")));
}
/*
This code produces the following output.
PROPERTY                         INTERNATIONAL            TRADITIONAL
CompareInfo                      CompareInfo - 3082       CompareInfo - 1034
DisplayName                      Spanish (Spain)          Spanish (Spain)
EnglishName                      Spanish (Spain)          Spanish (Spain)
IsNeutralCulture                 False                    False
IsReadOnly                       False                    False
LCID                             3082                     1034
Name                             es-ES                    es-ES
NativeName                       espaol (Espaa)         espaol (Espaa)
Parent                           es                       es
TextInfo                         TextInfo - 3082          TextInfo - 1034
ThreeLetterISOLanguageName       spa                      spa
ThreeLetterWindowsLanguageName   ESN                      ESN
TwoLetterISOLanguageName         es                       es
Comparing S"llegar" and S"lugar"
With myCIintl -> CompareInfo -> Compare: -1
With myCItrad -> CompareInfo -> Compare: 1
*/

[Visual Basic, C#, C++] 下面的代码示例使用中文确定每个特定区域性的父区域性。

[Visual Basic]
Imports System
Imports System.Globalization
Public Class SamplesCultureInfo
Public Shared Sub Main()
' Prints the header.
Console.WriteLine("SPECIFIC CULTURE                                  PARENT CULTURE")
' Determines the specific cultures that use the Chinese language, and displays the parent culture.
Dim ci As CultureInfo
For Each ci In  CultureInfo.GetCultures(CultureTypes.SpecificCultures)
If ci.TwoLetterISOLanguageName = "zh" Then
Console.Write("0x{0} {1} {2,-37}", ci.LCID.ToString("X4"), ci.Name, ci.EnglishName)
Console.WriteLine("0x{0} {1} {2}", ci.Parent.LCID.ToString("X4"), ci.Parent.Name, ci.Parent.EnglishName)
End If
Next ci
End Sub 'Main
End Class 'SamplesCultureInfo
'This code produces the following output.
'
'SPECIFIC CULTURE                                  PARENT CULTURE
'0x0404 zh-TW Chinese (Taiwan)                     0x7C04 zh-CHT Chinese (Traditional)
'0x0804 zh-CN Chinese (People's Republic of China) 0x0004 zh-CHS Chinese (Simplified)
'0x0C04 zh-HK Chinese (Hong Kong S.A.R.)           0x7C04 zh-CHT Chinese (Traditional)
'0x1004 zh-SG Chinese (Singapore)                  0x0004 zh-CHS Chinese (Simplified)
'0x1404 zh-MO Chinese (Macau S.A.R.)               0x0004 zh-CHS Chinese (Simplified)
[C#]
using System;
using System.Globalization;
public class SamplesCultureInfo  {
public static void Main()  {
// Prints the header.
Console.WriteLine( "SPECIFIC CULTURE                                  PARENT CULTURE" );
// Determines the specific cultures that use the Chinese language, and displays the parent culture.
foreach ( CultureInfo ci in CultureInfo.GetCultures( CultureTypes.SpecificCultures ) )  {
if ( ci.TwoLetterISOLanguageName == "zh" )  {
Console.Write( "0x{0} {1} {2,-37}", ci.LCID.ToString("X4"), ci.Name, ci.EnglishName );
Console.WriteLine( "0x{0} {1} {2}", ci.Parent.LCID.ToString("X4"), ci.Parent.Name, ci.Parent.EnglishName );
}
}
}
}
/*
This code produces the following output.
SPECIFIC CULTURE                                  PARENT CULTURE
0x0404 zh-TW Chinese (Taiwan)                     0x7C04 zh-CHT Chinese (Traditional)
0x0804 zh-CN Chinese (People's Republic of China) 0x0004 zh-CHS Chinese (Simplified)
0x0C04 zh-HK Chinese (Hong Kong S.A.R.)           0x7C04 zh-CHT Chinese (Traditional)
0x1004 zh-SG Chinese (Singapore)                  0x0004 zh-CHS Chinese (Simplified)
0x1404 zh-MO Chinese (Macau S.A.R.)               0x0004 zh-CHS Chinese (Simplified)
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
int main() {
// Prints the header.
Console::WriteLine(S"SPECIFIC CULTURE                                  PARENT CULTURE" );
// Determines the specific cultures that use the Chinese language,
// and displays the parent culture.
System::Collections::IEnumerator* en =
CultureInfo::GetCultures(CultureTypes::SpecificCultures)->GetEnumerator();
while (en->MoveNext())
{
CultureInfo* ci = __try_cast<CultureInfo*>(en->Current);
if (ci->TwoLetterISOLanguageName->Equals(S"zh"))
{
Console::Write(S"0x{0} {1} {2,-37}", ci->LCID.ToString("X4"),
ci->Name, ci->EnglishName);
Console::WriteLine(S"0x{0} {1} {2}", ci->Parent->LCID.ToString(S"X4"),
ci->Parent->Name, ci->Parent->EnglishName);
}
}
}
/*
This code produces the following output.
SPECIFIC CULTURE                                  PARENT CULTURE
0x0404 zh-TW Chinese (Taiwan)                     0x7C04 zh-CHT Chinese (Traditional)
0x0804 zh-CN Chinese (People's Republic of China) 0x0004 zh-CHS Chinese (Simplified)
0x0C04 zh-HK Chinese (Hong Kong S.A.R.)           0x7C04 zh-CHT Chinese (Traditional)
0x1004 zh-SG Chinese (Singapore)                  0x0004 zh-CHS Chinese (Simplified)
0x1404 zh-MO Chinese (Macau S.A.R.)               0x0004 zh-CHS Chinese (Simplified)
*/
 
 
  
.NET Framework 类库 

DateTime.ParseExact 方法 (String, String, IFormatProvider, DateTimeStyles)

使用指定的格式、区域性特定格式信息和样式,将日期和时间的指定字符串表示形式转换为其等效的 DateTime。字符串表示的格式必须与指定的格式完全匹配。

[Visual Basic]
Overloads Public Shared Function ParseExact( _
ByVal s As String, _
ByVal format As String, _
ByVal provider As IFormatProvider, _
ByVal style As DateTimeStyles _
) As DateTime
[C#]
public static DateTime ParseExact(
string s,
string format,
IFormatProvider provider,
DateTimeStyles style
);
[C++]
public: static DateTime ParseExact(
String* s,
String* format,
IFormatProvider* provider,
DateTimeStyles style
);
[JScript]
public static function ParseExact(
s : String,
format : String,
provider : IFormatProvider,
style : DateTimeStyles
) : DateTime;
参数
s
包含要转换的日期和时间的字符串。
format
s 的预期格式。
provider
一个 IFormatProvider,它提供有关 s 的区域性特定格式设置信息。
style
一个或多个 DateTimeStyles 常数的组合,用以指示 s 的允许格式。
返回值

DateTime,等效于由 format、provider 和 style 所指定的 s 中所包含的日期和时间。

异常
异常类型条件
ArgumentNullExceptions 或 format 为空引用(Visual Basic 中为 Nothing)。
FormatExceptions 或 format 是空字符串

- 或 -

s 不包含与 format 中指定的模式相对应的日期和时间。

备注

如果 s 的格式与 format 中的格式模式所指定的格式不完全匹配,则此方法引发 FormatException。如果 format 由单个标准格式的字符组成,则使用该字符所表示的格式模式。有关更多信息,请参见 DateTimeFormatInfo 主题。

如果 s 中包含时间不包含日期,则 style 参数确定是使用当前日期还是使用默认日期。如果 s 中包含日期不包含时间,则使用午夜 12 点。style 参数还确定是否忽略前置的、内部的或后随的空白字符。

provider 参数提供区域性特定的日期和时间格式信息。例如,用特定语言表示的一周内各天的名称,或年、月和日的惯用表示顺序。如果 provider 为空引用(Visual Basic 中为 Nothing),则使用当前区域性。

示例

[Visual Basic, C#, C++] 下面的示例说明 ParseExact 方法。

[Visual Basic]
Imports System
Imports System.Globalization
Class Class1
Public Shared Sub Main()
' Assume the current culture is en-US.
' The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
Dim myDateTimeValue As String = "2/16/1992 12:15:12"
Dim myDateTime As DateTime = DateTime.Parse(myDateTimeValue)
Console.WriteLine("1) myDateTime       = {0}", myDateTime)
' Reverse month and day to conform to a different culture.
' The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
Dim culture = New CultureInfo("fr-FR", True)
Dim myDateTimeFrenchValue As String = "    16/02/1992 12:15:12"
Dim myDateTimeFrench As DateTime = _
DateTime.Parse(myDateTimeFrenchValue, _
culture, _
DateTimeStyles.NoCurrentDateDefault)
Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench)
' The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
Dim expectedFormats As String() =  {"G", "g", "f", "F"}
myDateTimeFrench = DateTime.ParseExact(myDateTimeFrenchValue, _
expectedFormats, _
culture, _
DateTimeStyles.AllowWhiteSpaces)
Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench)
End Sub 'Main
End Class 'Class1
'
'This example yields the following results:
'
'1) myDateTime       = 2/16/1992 12:15:12 PM
'2) myDateTimeFrench = 2/16/1992 12:15:12 PM
'3) myDateTimeFrench = 2/16/1992 12:15:12 PM
'
[C#]
using System;
using System.Globalization;
namespace Parse
{
class Class1
{
public static void Main(string[] args)
{
// Assume the current culture is en-US.
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
string myDateTimeValue = "2/16/1992 12:15:12";
DateTime myDateTime = DateTime.Parse(myDateTimeValue);
Console.WriteLine("1) myDateTime       = {0}", myDateTime);
// Reverse month and day to conform to a different culture.
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
IFormatProvider culture = new CultureInfo("fr-FR", true);
string myDateTimeFrenchValue = "    16/02/1992 12:15:12";
DateTime myDateTimeFrench =
DateTime.Parse(myDateTimeFrenchValue,
culture,
DateTimeStyles.NoCurrentDateDefault);
Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench);
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
string[] expectedFormats = {"G", "g", "f" ,"F"};
myDateTimeFrench =
DateTime.ParseExact(myDateTimeFrenchValue,
expectedFormats,
culture,
DateTimeStyles.AllowWhiteSpaces);
Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench);
}
}
}
/*
This example yields the following results:
1) myDateTime       = 2/16/1992 12:15:12 PM
2) myDateTimeFrench = 2/16/1992 12:15:12 PM
3) myDateTimeFrench = 2/16/1992 12:15:12 PM
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
int main() {
// Assume the current culture is en-US.
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
String* myDateTimeValue = S"2/16/1992 12:15:12";
DateTime myDateTime = DateTime::Parse(myDateTimeValue);
Console::WriteLine(S"1) myDateTime       = {0}", __box(myDateTime));
// Reverse month and day to conform to a different culture.
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
IFormatProvider* culture = new CultureInfo(S"fr-FR", true);
String* myDateTimeFrenchValue = S"    16/02/1992 12:15:12";
DateTime myDateTimeFrench =
DateTime::Parse(myDateTimeFrenchValue,
culture,
DateTimeStyles::NoCurrentDateDefault);
Console::WriteLine(S"2) myDateTimeFrench = {0}", __box(myDateTimeFrench));
// The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds.
String* expectedFormats[] = {S"G", S"g", S"f" , S"F"};
myDateTimeFrench =
DateTime::ParseExact(myDateTimeFrenchValue,
expectedFormats,
culture,
DateTimeStyles::AllowWhiteSpaces);
Console::WriteLine(S"3) myDateTimeFrench = {0}", __box(myDateTimeFrench));
}
/*
This example yields the following results:
1) myDateTime       = 2/16/1992 12:15:12 PM
2) myDateTimeFrench = 2/16/1992 12:15:12 PM
3) myDateTimeFrench = 2/16/1992 12:15:12 PM
*/

[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 语言筛选器

要求

平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版, 公共语言基础结构 (CLI) 标准

请参见


注释:
string[] expectedFormats = {"G", "g", "f" ,"F"};的意思呢?如下

.NET Framework 类库 

DateTimeFormatInfo 类

定义如何根据区域性设置 DateTime 值的格式并显示这些值。

有关此类型所有成员的列表,请参阅 DateTimeFormatInfo 成员

System.Object
   System.Globalization.DateTimeFormatInfo

[Visual Basic]
<Serializable>
NotInheritable Public Class DateTimeFormatInfo
Implements ICloneable, IFormatProvider
[C#]
[Serializable]
public sealed class DateTimeFormatInfo : ICloneable,
IFormatProvider
[C++]
[Serializable]
public __gc __sealed class DateTimeFormatInfo : public ICloneable,
IFormatProvider
[JScript]
public
Serializable
class DateTimeFormatInfo implements ICloneable,
IFormatProvider
线程安全

此类型的所有公共静态(Visual Basic 中为 Shared)成员是线程安全的。但不保证任何实例成员是线程安全的。

备注

此类包含各种信息,例如日期模式、时间模式和 AM/PM 指示项。

若要为特定区域性创建 DateTimeFormatInfo,请为该区域性创建 CultureInfo 并检索 CultureInfo.DateTimeFormat 属性。若要为当前线程的区域性创建 DateTimeFormatInfo,请使用 CurrentInfo 属性。若要为固定区域性创建 DateTimeFormatInfo,请将 InvariantInfo 属性用于只读版本,或将 DateTimeFormatInfo 构造函数用于可写版本。不可能为非特定区域性创建 DateTimeFormatInfo。

用户可以通过“控制面板”中的“区域和语言选项”(或“区域选项”或“区域设置”)选择重写某些与 Windows 的当前区域性关联的值。例如,用户可能选择以另一种格式显示日期,或选择使用区域性默认设置以外的货币。如果 CultureInfo.UseUserOverride 属性设置为 true,则还将从用户设置中检索 CultureInfo.DateTimeFormat 实例、CultureInfo.NumberFormat 实例和 CultureInfo.TextInfo 实例的属性。如果用户设置与 CultureInfo 的关联区域性不兼容(例如选定的日历不属于 OptionalCalendars 其中之一),则方法结果和属性值是未定义的。

使用在 DateTimeFormatInfo 的属性中存储的标准或自定义模式设置 DateTime 值的格式。

可以通过设置可写 DateTimeFormatInfo 的关联属性用自定义模式替代标准模式。若要确定 DateTimeFormatInfo 是否是可写的,请使用 IsReadOnly 属性。

下表列出了每一种标准模式的标准格式字符以及可以进行设置以修改标准模式的关联 DateTimeFormatInfo 属性。格式字符区分大小写;例如,“g”和“G”所代表的模式稍有不同。

格式字符关联属性/说明
dShortDatePattern
DLongDatePattern
f完整日期和时间(长日期和短时间)
FFullDateTimePattern(长日期和长时间)
g常规(短日期和短时间)
G常规(短日期和长时间)
m、MMonthDayPattern
r、RRFC1123Pattern
s使用当地时间的 SortableDateTimePattern(基于 ISO 8601)
tShortTimePattern
TLongTimePattern
uUniversalSortableDateTimePattern 用于显示通用时间的格式
U使用通用时间的完整日期和时间(长日期和长时间)
y、YYearMonthPattern

下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的;例如,识别“MM”,但不识别“mm”。如果自定义模式包含空白字符或用单引号括起来的字符,则输出字符串页也将包含这些字符。未定义为格式模式的一部分或未定义为格式字符的字符按其原义复制。

格式模式说明
d月中的某一天。一位数的日期没有前导零。
dd月中的某一天。一位数的日期有一个前导零。
ddd周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
dddd周中某天的完整名称,在 DayNames 中定义。
M月份数字。一位数的月份没有前导零。
MM月份数字。一位数的月份有一个前导零。
MMM月份的缩写名称,在 AbbreviatedMonthNames 中定义。
MMMM月份的完整名称,在 MonthNames 中定义。
y不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
yy不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
yyyy包括纪元的四位数的年份。
gg时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
h12 小时制的小时。一位数的小时数没有前导零。
hh12 小时制的小时。一位数的小时数有前导零。
H24 小时制的小时。一位数的小时数没有前导零。
HH24 小时制的小时。一位数的小时数有前导零。
m分钟。一位数的分钟数没有前导零。
mm分钟。一位数的分钟数有一个前导零。
s秒。一位数的秒数没有前导零。
ss秒。一位数的秒数有一个前导零。
f秒的小数精度为一位。其余数字被截断。
ff秒的小数精度为两位。其余数字被截断。
fff秒的小数精度为三位。其余数字被截断。
ffff秒的小数精度为四位。其余数字被截断。
fffff秒的小数精度为五位。其余数字被截断。
ffffff秒的小数精度为六位。其余数字被截断。
fffffff秒的小数精度为七位。其余数字被截断。
tAMDesignatorPMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。
ttAMDesignatorPMDesignator 中定义的 AM/PM 指示项(如果存在)。
z时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
zz时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
zzz完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
:TimeSeparator 中定义的默认时间分隔符。
/DateSeparator 中定义的默认日期分隔符。
% c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。
\ c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\\”。

只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。自定义模式的长度至少为两个字符;例如,

  • DateTime.ToString( "d") 返回 DateTime 值;“d”是标准短日期模式。
  • DateTime.ToString( "%d") 返回月中的某天;“%d”是自定义模式。
  • DateTime.ToString( "d ") 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。

只能为固定区域性或特定区域性创建 DateTimeFormatInfo 或 NumberFormatInfo,而不能为非特定区域性创建它们。有关固定区域性、特定区域性和非特定区域性的更多信息,请参见 CultureInfo 类。

该类实现 ICloneable 接口以启用 DateTimeFormatInfo 对象的复制。它还实现 IFormatProvider 以便为应用程序提供格式化信息。

示例

[Visual Basic, C#, C++] 下面的代码示例使用格式字符输出 en-US 区域性的不同格式模式。该示例还将显示与格式字符关联的属性值。

[Visual Basic]
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Public Class SamplesDTFI
Public Shared Sub Main()
' Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
Dim myDTFI As DateTimeFormatInfo = New CultureInfo("en-US", False).DateTimeFormat
' Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
' The Gregorian calendar is the default calendar for the en-US culture.
Dim myDT As New DateTime(2002, 1, 3)
' Displays the format pattern associated with each format character.
Console.WriteLine("FORMAT  en-US EXAMPLE")
Console.WriteLine("CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY")
Console.WriteLine()
Console.WriteLine("  d     {0}", myDT.ToString("d"))
Console.WriteLine("        {0} {1}", myDTFI.ShortDatePattern, "(ShortDatePattern)")
Console.WriteLine()
Console.WriteLine("  D     {0}", myDT.ToString("D"))
Console.WriteLine("        {0} {1}", myDTFI.LongDatePattern, "(LongDatePattern)")
Console.WriteLine()
Console.WriteLine("  f     {0}", myDT.ToString("f"))
Console.WriteLine()
Console.WriteLine("  F     {0}", myDT.ToString("F"))
Console.WriteLine("        {0} {1}", myDTFI.FullDateTimePattern, "(FullDateTimePattern)")
Console.WriteLine()
Console.WriteLine("  g     {0}", myDT.ToString("g"))
Console.WriteLine()
Console.WriteLine("  G     {0}", myDT.ToString("G"))
Console.WriteLine()
Console.WriteLine("  m     {0}", myDT.ToString("m"))
Console.WriteLine("        {0} {1}", myDTFI.MonthDayPattern, "(MonthDayPattern)")
Console.WriteLine()
Console.WriteLine("  M     {0}", myDT.ToString("M"))
Console.WriteLine("        {0} {1}", myDTFI.MonthDayPattern, "(MonthDayPattern)")
Console.WriteLine()
Console.WriteLine("  r     {0}", myDT.ToString("r"))
Console.WriteLine("        {0} {1}", myDTFI.RFC1123Pattern, "(RFC1123Pattern)")
Console.WriteLine()
Console.WriteLine("  R     {0}", myDT.ToString("R"))
Console.WriteLine("        {0} {1}", myDTFI.RFC1123Pattern, "(RFC1123Pattern)")
Console.WriteLine()
Console.WriteLine("  s     {0}", myDT.ToString("s"))
Console.WriteLine("        {0} {1}", myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)")
Console.WriteLine()
Console.WriteLine("  t     {0}", myDT.ToString("t"))
Console.WriteLine("        {0} {1}", myDTFI.ShortTimePattern, "(ShortTimePattern)")
Console.WriteLine()
Console.WriteLine("  T     {0}", myDT.ToString("T"))
Console.WriteLine("        {0} {1}", myDTFI.LongTimePattern, "(LongTimePattern)")
Console.WriteLine()
Console.WriteLine("  u     {0}", myDT.ToString("u"))
Console.WriteLine("        {0} {1}", myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)")
Console.WriteLine()
Console.WriteLine("  U     {0}", myDT.ToString("U"))
Console.WriteLine()
Console.WriteLine("  y     {0}", myDT.ToString("y"))
Console.WriteLine("        {0} {1}", myDTFI.YearMonthPattern, "(YearMonthPattern)")
Console.WriteLine()
Console.WriteLine("  Y     {0}", myDT.ToString("Y"))
Console.WriteLine("        {0} {1}", myDTFI.YearMonthPattern, "(YearMonthPattern)")
End Sub 'Main
End Class 'SamplesDTFI
'This code produces the following output.
'
'FORMAT  en-US EXAMPLE
'CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY
'
'  d     1/3/2002
'        M/d/yyyy (ShortDatePattern)
'
'  D     Thursday, January 03, 2002
'        dddd, MMMM dd, yyyy (LongDatePattern)
'
'  f     Thursday, January 03, 2002 12:00 AM
'
'  F     Thursday, January 03, 2002 12:00:00 AM
'        dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
'
'  g     1/3/2002 12:00 AM
'
'  G     1/3/2002 12:00:00 AM
'
'  m     January 03
'        MMMM dd (MonthDayPattern)
'
'  M     January 03
'        MMMM dd (MonthDayPattern)
'
'  r     Thu, 03 Jan 2002 00:00:00 GMT
'        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
'
'  R     Thu, 03 Jan 2002 00:00:00 GMT
'        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
'
'  s     2002-01-03T00:00:00
'        yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
'
'  t     12:00 AM
'        h:mm tt (ShortTimePattern)
'
'  T     12:00:00 AM
'        h:mm:ss tt (LongTimePattern)
'
'  u     2002-01-03 00:00:00Z
'        yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
'
'  U     Thursday, January 03, 2002 8:00:00 AM
'
'  y     January, 2002
'        MMMM, yyyy (YearMonthPattern)
'
'  Y     January, 2002
'        MMMM, yyyy (YearMonthPattern)
'
[C#]
using System;
using System.Globalization;
public class SamplesDTFI  {
public static void Main()  {
// Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false ).DateTimeFormat;
// Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
// The Gregorian calendar is the default calendar for the en-US culture.
DateTime myDT = new DateTime( 2002, 1, 3 );
// Displays the format pattern associated with each format character.
Console.WriteLine( "FORMAT  en-US EXAMPLE" );
Console.WriteLine( "CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY\n" );
Console.WriteLine( "  d     {0}", myDT.ToString("d") );
Console.WriteLine( "        {0} {1}\n", myDTFI.ShortDatePattern, "(ShortDatePattern)" );
Console.WriteLine( "  D     {0}", myDT.ToString("D") );
Console.WriteLine( "        {0} {1}\n", myDTFI.LongDatePattern, "(LongDatePattern)" );
Console.WriteLine( "  f     {0}\n", myDT.ToString("f") );
Console.WriteLine( "  F     {0}", myDT.ToString("F") );
Console.WriteLine( "        {0} {1}\n", myDTFI.FullDateTimePattern, "(FullDateTimePattern)" );
Console.WriteLine( "  g     {0}\n", myDT.ToString("g") );
Console.WriteLine( "  G     {0}\n", myDT.ToString("G") );
Console.WriteLine( "  m     {0}", myDT.ToString("m") );
Console.WriteLine( "        {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
Console.WriteLine( "  M     {0}", myDT.ToString("M") );
Console.WriteLine( "        {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
Console.WriteLine( "  r     {0}", myDT.ToString("r") );
Console.WriteLine( "        {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
Console.WriteLine( "  R     {0}", myDT.ToString("R") );
Console.WriteLine( "        {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
Console.WriteLine( "  s     {0}", myDT.ToString("s") );
Console.WriteLine( "        {0} {1}\n", myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)" );
Console.WriteLine( "  t     {0}", myDT.ToString("t") );
Console.WriteLine( "        {0} {1}\n", myDTFI.ShortTimePattern, "(ShortTimePattern)" );
Console.WriteLine( "  T     {0}", myDT.ToString("T") );
Console.WriteLine( "        {0} {1}\n", myDTFI.LongTimePattern, "(LongTimePattern)" );
Console.WriteLine( "  u     {0}", myDT.ToString("u") );
Console.WriteLine( "        {0} {1}\n", myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)" );
Console.WriteLine( "  U     {0}\n", myDT.ToString("U") );
Console.WriteLine( "  y     {0}", myDT.ToString("y") );
Console.WriteLine( "        {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );
Console.WriteLine( "  Y     {0}", myDT.ToString("Y") );
Console.WriteLine( "        {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );
}
}
/*
This code produces the following output.
FORMAT  en-US EXAMPLE
CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY
d     1/3/2002
M/d/yyyy (ShortDatePattern)
D     Thursday, January 03, 2002
dddd, MMMM dd, yyyy (LongDatePattern)
f     Thursday, January 03, 2002 12:00 AM
F     Thursday, January 03, 2002 12:00:00 AM
dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
g     1/3/2002 12:00 AM
G     1/3/2002 12:00:00 AM
m     January 03
MMMM dd (MonthDayPattern)
M     January 03
MMMM dd (MonthDayPattern)
r     Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
R     Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
s     2002-01-03T00:00:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
t     12:00 AM
h:mm tt (ShortTimePattern)
T     12:00:00 AM
h:mm:ss tt (LongTimePattern)
u     2002-01-03 00:00:00Z
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
U     Thursday, January 03, 2002 8:00:00 AM
y     January, 2002
MMMM, yyyy (YearMonthPattern)
Y     January, 2002
MMMM, yyyy (YearMonthPattern)
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
int main()
{
// Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
CultureInfo * MyCI = new CultureInfo(S"en-US", false);
DateTimeFormatInfo* myDTFI = MyCI -> DateTimeFormat;
// Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
// The Gregorian calendar is the default calendar for the en-US culture.
DateTime myDT = DateTime(2002, 1, 3);
// Displays the format pattern associated with each format character.
Console::WriteLine(S"FORMAT  en-US EXAMPLE");
Console::WriteLine(S"CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY\n");
Console::WriteLine(S"  d {0}", myDT.ToString(S"d"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> ShortDatePattern, S"(ShortDatePattern)");
Console::WriteLine(S"  D {0}", myDT.ToString(S"D"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> LongDatePattern, S"(LongDatePattern)");
Console::WriteLine(S"  f {0}\n", myDT.ToString(S"f"));
Console::WriteLine(S"  F {0}", myDT.ToString(S"F"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> FullDateTimePattern, S"(FullDateTimePattern)");
Console::WriteLine(S"  g {0}\n", myDT.ToString(S"g"));
Console::WriteLine(S"  G {0}\n", myDT.ToString(S"G"));
Console::WriteLine(S"  m {0}", myDT.ToString(S"m"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> MonthDayPattern, S"(MonthDayPattern)");
Console::WriteLine(S"  M {0}", myDT.ToString(S"M"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> MonthDayPattern, S"(MonthDayPattern)");
Console::WriteLine(S"  r {0}", myDT.ToString(S"r"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> RFC1123Pattern, S"(RFC1123Pattern)");
Console::WriteLine(S"  R {0}", myDT.ToString(S"R"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> RFC1123Pattern, S"(RFC1123Pattern)");
Console::WriteLine(S"  s {0}", myDT.ToString(S"s"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> SortableDateTimePattern, S"(SortableDateTimePattern)");
Console::WriteLine(S"  t {0}", myDT.ToString(S"t"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> ShortTimePattern, S"(ShortTimePattern)");
Console::WriteLine(S"  T {0}", myDT.ToString(S"T"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> LongTimePattern, S"(LongTimePattern)");
Console::WriteLine(S"  u {0}", myDT.ToString(S"u"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> UniversalSortableDateTimePattern, S"(UniversalSortableDateTimePattern)");
Console::WriteLine(S"  U {0}\n", myDT.ToString(S"U"));
Console::WriteLine(S"  y {0}", myDT.ToString(S"y"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> YearMonthPattern, S"(YearMonthPattern)");
Console::WriteLine(S"  Y {0}", myDT.ToString(S"Y"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> YearMonthPattern, S"(YearMonthPattern)");
}
/*
This code produces the following output.
FORMAT  en-US EXAMPLE
CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY
d     1/3/2002
M/d/yyyy (ShortDatePattern)
D     Thursday, January 03, 2002
dddd, MMMM dd, yyyy (LongDatePattern)
f     Thursday, January 03, 2002 12:00 AM
F     Thursday, January 03, 2002 12:00:00 AM
dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
g     1/3/2002 12:00 AM
G     1/3/2002 12:00:00 AM
m     January 03
MMMM dd (MonthDayPattern)
M     January 03
MMMM dd (MonthDayPattern)
r     Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
R     Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
s     2002-01-03T00:00:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
t     12:00 AM
h:mm tt (ShortTimePattern)
T     12:00:00 AM
h:mm:ss tt (LongTimePattern)
u     2002-01-03 00:00:00Z
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
U     Thursday, January 03, 2002 8:00:00 AM
y     January, 2002
MMMM, yyyy (YearMonthPattern)
Y     January, 2002
MMMM, yyyy (YearMonthPattern)
*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值