针对特定区域性使用日历
更新:2007 年 11 月
一个全球化应用程序应该能够根据当前区域性显示和使用日历。.NET Framework 提供 Calendar 类以及下面的类实现:
CultureInfo 类具有 Calendar 属性,该属性指定区域性的默认日历。某些区域性支持多种日历。OptionalCalendars 属性指定此类型的区域性所支持的可选日历。
下面的代码示例为指定为“th-TH”的区域性泰语(泰国)和指定为“ja-JP”的区域性日语(日本)创建 CultureInfo 对象。该示例为每个区域性显示了默认日历和可选日历。请注意,GregorianCalendar 对象进一步划分为多个子类型。CalendarType 属性指定公历的子类型。在此示例中,每当确定日历为公历时,都会检索和显示 CalendarType 值。有关可能的 CalendarType 值的列表和解释,请参见 GregorianCalendarTypes。
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Public Class TestClass
Public Shared Sub Main()
' Creates a CultureInfo for Thai in Thailand.
Dim th As New CultureInfo("th-TH")
DisplayCalendars(th)
' Creates a CultureInfo for Japanese in Japan.
Dim ja As New CultureInfo("ja-JP")
DisplayCalendars(ja)
End Sub
Protected Shared Sub DisplayCalendars(cultureinfo As CultureInfo)
Dim ci As New CultureInfo(cultureinfo.ToString())
' Displays the default calendar for the culture.
If TypeOf ci.Calendar Is GregorianCalendar Then
Console.WriteLine(ControlChars.Newline + ControlChars.Newline + _
"The default calendar for the {0} culture is:" + _
ControlChars.Newline + " {1}" + ControlChars.Newline + _
ControlChars.Newline, ci.DisplayName.ToString(), _
ci.Calendar.ToString() + " subtype" + CType(ci.Calendar, _
GregorianCalendar).CalendarType.ToString())
Else
Console.WriteLine(ControlChars.Newline + ControlChars.Newline + _
"The default calendar for the {0} culture is:" + _
ControlChars.Newline + "{1}" + ControlChars.Newline + _
ControlChars.Newline, ci.DisplayName.ToString(),_
ci.Calendar.ToString())
End If
' Displays the optional calendars for the culture.
Console.WriteLine("The optional calendars for the {0} culture are: _
", ci.DisplayName.ToString())
Dim i As Integer
For i = ci.OptionalCalendars.GetLowerBound(0) To _
ci.OptionalCalendars.GetUpperBound(0)
If TypeOf ci.OptionalCalendars(i) Is GregorianCalendar Then
' Displays the calendar subtype.
Dim CalStr As [String] = ci.OptionalCalendars(i).ToString() _
+ " subtype " + CType(ci.OptionalCalendars(i), _
GregorianCalendar).CalendarType.ToString()
Console.WriteLine(CalStr)
Else
Console.WriteLine(ci.OptionalCalendars(i).ToString())
End If
Next i
End Sub
End Class
using System;
using System.Globalization;
public class TestClass
{
public static void Main()
{
// Creates a CultureInfo for Thai in Thailand.
CultureInfo th= new CultureInfo("th-TH");
DisplayCalendars(th);
// Creates a CultureInfo for Japanese in Japan.
CultureInfo ja= new CultureInfo("ja-JP");
DisplayCalendars(ja);
}
protected static void DisplayCalendars(CultureInfo cultureinfo)
{
CultureInfo ci = new CultureInfo(cultureinfo.ToString());
// Displays the default calendar for the culture.
if (ci.Calendar is GregorianCalendar)
Console.WriteLine ("\n\nThe default calendar for the {0} culture
is:\n {1}\n\n", ci.DisplayName.ToString(),
ci.Calendar.ToString() + " subtype " +
((GregorianCalendar)ci.Calendar).CalendarType.ToString());
else
Console.WriteLine ("\n\nThe default calendar for the {0} culture
is: \n{1}\n\n", ci.DisplayName.ToString(),
ci.Calendar.ToString());
// Displays the optional calendars for the culture.
Console.WriteLine ("The optional calendars for the {0} culture are:
", ci.DisplayName.ToString());
for (int i = ci.OptionalCalendars.GetLowerBound(0); i <=
ci.OptionalCalendars.GetUpperBound(0); i++ )
{
if (ci.OptionalCalendars[i] is GregorianCalendar)
{
// Displays the calendar subtype.
String CalStr = (ci.OptionalCalendars[i].ToString() + "
subtype " + ((GregorianCalendar)ci.OptionalCalendars[i]).CalendarType.ToString());
Console.WriteLine(CalStr);
}
else
Console.WriteLine (ci.OptionalCalendars[i].ToString());
}
}
}
此代码产生下列输出:
The default calendar for the Thai (Thailand) culture is:
System.Globalization.ThaiBuddhistCalendar
The optional calendars for the Thai (Thailand) culture are:
System.Globalization.ThaiBuddhistCalendar
System.Globalization.GregorianCalendar subtype Localized
The default calendar for the Japanese (Japan) culture is:
System.Globalization.GregorianCalendar subtype Localized
The optional calendars for the Japanese (Japan) culture are:
System.Globalization.JapaneseCalendar
System.Globalization.GregorianCalendar subtype USEnglish
System.Globalization.GregorianCalendar subtype Localized
下面的代码示例阐释了 DateTime 结构和 Calendar 类中的类似方法如何为同一区域性检索不同的结果。线程的 CurrentCulture 设置为“he-IL”(以色列希伯来语),而当前日历设置为犹太历。创建和初始化了一个 DateTime 类型。接下来,使用 DateTime 和 Calendar 的成员返回日期、月份、年份以及这一年的月份数,并显示这些值。只有 Calendar 类方法根据犹太历返回日期、月份、年份以及这一年的月份数。DateTime 方法总是使用公历来执行计算,而忽视当前日历的设置。
Imports System
Imports System.Threading
Imports System.Globalization
Imports Microsoft.VisualBasic
Public Class TestClass
Public Shared Sub Main()
' Creates a CultureInfo for Hebrew in Israel.
Dim he As New CultureInfo("he-IL")
he.DateTimeFormat.Calendar = New HebrewCalendar()
Console.WriteLine(ControlChars.Newline + ControlChars.Newline _
+ "The current calendar set for the {0} culture is:" + _
ControlChars.Newline + " {1}", he.DisplayName.ToString(), _
he.DateTimeFormat.Calendar.ToString())
Dim dt As New DateTime(5760, 11, 4, he.DateTimeFormat.Calendar)
Console.WriteLine(ControlChars.Newline + " The DateTime _
returns the day as: {0}", dt.Day)
Console.WriteLine(ControlChars.Newline + " The Calendar class _
returns the day as: {0}", _
he.DateTimeFormat.Calendar.GetDayOfMonth(dt))
Console.WriteLine(ControlChars.Newline + " The DateTime _
returns the month as: {0}", dt.Month)
Console.WriteLine(ControlChars.Newline + " The Calendar class _
returns the month as: {0}", _
he.DateTimeFormat.Calendar.GetMonth(dt))
Console.WriteLine(ControlChars.Newline + " The DateTime _
returns the Year as: {0}", dt.Year)
Console.WriteLine(ControlChars.Newline + " The Calendar class _
returns the Year as: {0}", _
he.DateTimeFormat.Calendar.GetYear(dt))
Console.WriteLine(ControlChars.Newline + " The Calendar class _
returns the number of months in the year {0} as: {1}", _
he.DateTimeFormat.Calendar.GetYear(dt), _
he.DateTimeFormat.Calendar.GetMonthsInYear _
(he.DateTimeFormat.Calendar.GetYear(dt))
Console.WriteLine(ControlChars.Newline + " The DateTime does _
not return the number of months in a year " + _
ControlChars.Newline + " because it uses the Gregorian _
calendar, which always has twelve months.")
End Sub
End Class
using System;
using System.Threading;
using System.Globalization;
public class TestClass
{
public static void Main()
{
// Creates a CultureInfo for Hebrew in Israel.
CultureInfo he= new CultureInfo("he-IL");
Thread.CurrentThread.CurrentCulture = he;
he.DateTimeFormat.Calendar = new HebrewCalendar();
Console.WriteLine ("\n\nThe current calendar set for the {0} culture
is:\n {1}", he.DisplayName.ToString(),
he.DateTimeFormat.Calendar.ToString());
DateTime dt = new DateTime(5760, 11, 4, he.DateTimeFormat.Calendar);
Console.WriteLine ("\nThe DateTime returns the day as: {0}",
dt.Day);
Console.WriteLine ("\nThe Calendar class returns the day as: {0}",
he.DateTimeFormat.Calendar.GetDayOfMonth(dt));
Console.WriteLine ("\nThe DateTime returns the month as:
{0}", dt.Month);
Console.WriteLine ("\nThe Calendar class returns the month as:
{0}", he.DateTimeFormat.Calendar.GetMonth(dt));
Console.WriteLine ("\nThe DateTime returns the year as: {0}",
dt.Year);
Console.WriteLine ("\nThe Calendar class returns the year as: {0}",
he.DateTimeFormat.Calendar.GetYear(dt));
Console.WriteLine ("\nThe Calendar class returns the number of
months in the year {0} as:
{1}",he.DateTimeFormat.Calendar.GetYear(dt),
he.DateTimeFormat.Calendar.GetMonthsInYear
(he.DateTimeFormat.Calendar.GetYear(dt)));
Console.WriteLine ("\nThe DateTime does not return the number
of months in a year \nbecause it uses the Gregorian calendar,
which always has twelve months.");
}
}
此代码产生下列输出:
The current calendar set for the Hebrew (Israel) culture is:
System.Globalization.HebrewCalendar
The DateTime returns the day as: 7
The Calendar class returns the day as: 4
The DateTime returns the month as: 7
The Calendar class returns the month as: 11
The DateTime returns the year as: 2000
The Calendar class returns the year as: 5760
The Calendar class returns the number of months in the year 5760 as: 13
The DateTime does not return the number of months in a year
because it uses the Gregorian calendar, which always has twelve months.
下面的代码示例阐释了为当前月份、日期和年份检索的值如何随着为指定区域性设置的当前日历的不同而不同。线程的 CurrentCulture 设置为“ja-JP”,日历设置为 JapaneseCalendar。返回并显示日期、月份和年份。接下来,将日历设置为 GregorianCalendar,然后返回并显示日期、月份和年份。请注意年份如何随着当前日历的不同而不同。日本历返回的年份为 13,而公历返回的年份为 2001。
Imports System
Imports System.Threading
Imports System.Globalization
Imports Microsoft.VisualBasic
Public Class TestClass
Public Shared Sub Main()
Dim dt As DateTime = DateTime.Now
' Creates a CultureInfo for Japanese in Japan.
Dim jp As New CultureInfo("ja-JP")
jp.DateTimeFormat.Calendar = New JapaneseCalendar()
Console.WriteLine(ControlChars.Newline + ControlChars.Newline + _
"The current calendar set for the {0} culture is:" + _
ControlChars.Newline + " {1}", jp.DisplayName.ToString(), _
jp.DateTimeFormat.Calendar.ToString())
Console.WriteLine(ControlChars.Newline + " The day is: {0}", _
jp.DateTimeFormat.Calendar.GetDayOfMonth(dt))
Console.WriteLine(ControlChars.Newline + " The month is: {0}", _
jp.DateTimeFormat.Calendar.GetMonth(dt))
Console.WriteLine(ControlChars.Newline + " The year is: {0}", _
jp.DateTimeFormat.Calendar.GetYear(dt))
jp.DateTimeFormat.Calendar = New GregorianCalendar()
Console.WriteLine(ControlChars.Newline + ControlChars.Newline + _
"The current calendar set for the {0} culture is:" + _
ControlChars.Newline + " {1}", jp.DisplayName.ToString(), _
jp.DateTimeFormat.Calendar.ToString())
Console.WriteLine(ControlChars.Newline + " The day is: {0}", _
jp.DateTimeFormat.Calendar.GetDayOfMonth(dt))
Console.WriteLine(ControlChars.Newline + " The month is: {0}", _
jp.DateTimeFormat.Calendar.GetMonth(dt))
Console.WriteLine(ControlChars.Newline + " The year is: {0}", _
jp.DateTimeFormat.Calendar.GetYear(dt))
End Sub
End Class
using System;
using System.Threading;
using System.Globalization;
public class TestClass
{
public static void Main()
{
DateTime dt = DateTime.Now;
// Creates a CultureInfo for Japanese in Japan.
CultureInfo jp = new CultureInfo("ja-JP");
Thread.CurrentThread.CurrentCulture = jp;
jp.DateTimeFormat.Calendar = new JapaneseCalendar();
Console.WriteLine ("\n\nThe current calendar set for the {0} culture
is:\n {1}", jp.DisplayName.ToString(),
jp.DateTimeFormat.Calendar.ToString());
Console.WriteLine ("\nThe day is: {0}",
jp.DateTimeFormat.Calendar.GetDayOfMonth(dt));
Console.WriteLine ("\nThe month is: {0}",
jp.DateTimeFormat.Calendar.GetMonth(dt));
Console.WriteLine ("\nThe year is: {0}",
jp.DateTimeFormat.Calendar.GetYear(dt));
jp.DateTimeFormat.Calendar = new GregorianCalendar();
Console.WriteLine ("\n\nThe current calendar set for the {0} culture
is:\n {1}", jp.DisplayName.ToString(),
jp.DateTimeFormat.Calendar.ToString());
Console.WriteLine ("\nThe day is: {0}",
jp.DateTimeFormat.Calendar.GetDayOfMonth(dt));
Console.WriteLine ("\nThe month is: {0}",
jp.DateTimeFormat.Calendar.GetMonth(dt));
Console.WriteLine ("\nThe year is: {0}",
jp.DateTimeFormat.Calendar.GetYear(dt));
}
}
此代码产生下列输出:
The current calendar set for the Japanese (Japan) culture is:
System.Globalization.JapaneseCalendar
The day is: 3
The month is: 8
The year is: 13
The current calendar set for the Japanese (Japan) culture is:
System.Globalization.GregorianCalendar
The day is: 3
The month is: 8
The year is: 2001