날짜 및 시간 값은 특정 시간을 나타내지만 문자열 표현은 문화권을 구분하며 특정 문화권별 날짜 및 시간 값을 표시하는 데 사용되는 규칙과 해당 문화권에서 사용하는 달력에 따라 달라집니다. 이 항목에서는 .NET의 일정에 대한 지원을 살펴보고 날짜 값으로 작업할 때 일정 클래스를 사용하는 것에 대해 설명합니다.
.NET의 일정
.NET의 모든 달력은 기본 달력 구현을 제공하는 System.Globalization.Calendar 클래스에서 파생됩니다. Calendar 클래스에서 상속되는 클래스 중 하나는 모든 lunisolar 달력의 기본 클래스인 EastAsianLunisolarCalendar 클래스입니다. .NET에는 다음과 같은 일정 구현이 포함됩니다.
ChineseLunisolarCalendar- 중국어 음력 달력을 나타냅니다.
GregorianCalendar- 그레고리오력을 나타냅니다. 이 달력은 System.Globalization.GregorianCalendarTypes 열거형으로 정의된 하위 형식(예: 아랍어 및 중동 프랑스어)으로 나뉩니다. GregorianCalendar.CalendarType 속성은 그레고리오력의 하위 형식을 지정합니다.
히브리어 달력을 나타내는 HebrewCalendar.
HijriCalendar- Hijri 달력을 나타냅니다.
일본어 달력을 나타내는 JapaneseCalendar.
JapaneseLunisolarCalendar- 일본어 음력 달력을 나타냅니다.
JulianCalendar- 줄리안 달력을 나타냅니다.
KoreanCalendar- 한국어 달력을 나타냅니다.
KoreanLunisolarCalendar- 한국어 루니솔라 달력을 나타냅니다.
PersianCalendar- 페르시아 달력을 나타냅니다.
TaiwanCalendar- 대만 달력을 나타냅니다.
TaiwanLunisolarCalendar- 대만 루니솔라 달력을 나타냅니다.
ThaiBuddhistCalendar태국 불교 달력을 나타냅니다.
UmAlQuraCalendar- Um Al Qura 달력을 나타냅니다.
달력은 다음 두 가지 방법 중 하나로 사용할 수 있습니다.
특정 문화권에서 사용하는 달력입니다. 각 CultureInfo 개체에는 개체가 현재 사용하고 있는 달력인 현재 달력이 있습니다. 모든 날짜 및 시간 값의 문자열 표현은 현재 문화권과 현재 달력을 자동으로 반영합니다. 일반적으로 현재 달력은 문화권의 기본 달력입니다. CultureInfo 개체에는 선택적으로 문화권에서 사용할 수 있는 추가 일정이 포함된 달력이 있습니다.
특정 문화권과 독립적인 독립 실행형 달력입니다. 이 경우 Calendar 메서드를 사용하여 날짜를 달력을 반영하는 값으로 표현합니다.
ChineseLunisolarCalendar, JapaneseLunisolarCalendar, JulianCalendar, KoreanLunisolarCalendar, PersianCalendar및 TaiwanLunisolarCalendar 6개의 일정 클래스는 독립 실행형 달력으로만 사용할 수 있습니다. 문화권에서는 기본 달력이나 선택적 달력으로 사용되지 않습니다.
캘린더와 문화
각 문화권에는 CultureInfo.Calendar 속성으로 정의된 기본 달력이 있습니다. CultureInfo.OptionalCalendars 속성은 해당 문화권의 기본 달력을 포함하여 특정 문화권에서 지원하는 모든 달력을 지정하는 Calendar 개체의 배열을 반환합니다.
다음 예제에서는 CultureInfo.Calendar 및 CultureInfo.OptionalCalendars 속성을 보여 줍니다. 태국어(태국) 및 일본어(일본) 문화권에 대한 CultureInfo 개체를 만들고 기본 및 선택적 달력을 표시합니다. 두 경우 모두 문화권의 기본 달력도 CultureInfo.OptionalCalendars 컬렉션에 포함됩니다.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
// Create a CultureInfo for Thai in Thailand.
CultureInfo th = CultureInfo.CreateSpecificCulture("th-TH");
DisplayCalendars(th);
// Create a CultureInfo for Japanese in Japan.
CultureInfo ja = CultureInfo.CreateSpecificCulture("ja-JP");
DisplayCalendars(ja);
}
static void DisplayCalendars(CultureInfo ci)
{
Console.WriteLine($"Calendars for the {ci.Name} culture:");
// Get the culture's default calendar.
Calendar defaultCalendar = ci.Calendar;
Console.Write(" Default Calendar: {0}", GetCalendarName(defaultCalendar));
if (defaultCalendar is GregorianCalendar)
Console.WriteLine($" ({((GregorianCalendar) defaultCalendar).CalendarType})");
else
Console.WriteLine();
// Get the culture's optional calendars.
Console.WriteLine(" Optional Calendars:");
foreach (var optionalCalendar in ci.OptionalCalendars) {
Console.Write("{0,6}{1}", "", GetCalendarName(optionalCalendar));
if (optionalCalendar is GregorianCalendar)
Console.Write(" ({0})",
((GregorianCalendar) optionalCalendar).CalendarType);
Console.WriteLine();
}
Console.WriteLine();
}
static string GetCalendarName(Calendar cal)
{
return cal.ToString().Replace("System.Globalization.", "");
}
}
// The example displays the following output:
// Calendars for the th-TH culture:
// Default Calendar: ThaiBuddhistCalendar
// Optional Calendars:
// ThaiBuddhistCalendar
// GregorianCalendar (Localized)
//
// Calendars for the ja-JP culture:
// Default Calendar: GregorianCalendar (Localized)
// Optional Calendars:
// GregorianCalendar (Localized)
// JapaneseCalendar
// GregorianCalendar (USEnglish)
Imports System.Globalization
Public Module Example
Public Sub Main()
' Create a CultureInfo for Thai in Thailand.
Dim th As CultureInfo = CultureInfo.CreateSpecificCulture("th-TH")
DisplayCalendars(th)
' Create a CultureInfo for Japanese in Japan.
Dim ja As CultureInfo = CultureInfo.CreateSpecificCulture("ja-JP")
DisplayCalendars(ja)
End Sub
Sub DisplayCalendars(ci As CultureInfo)
Console.WriteLine("Calendars for the {0} culture:", ci.Name)
' Get the culture's default calendar.
Dim defaultCalendar As Calendar = ci.Calendar
Console.Write(" Default Calendar: {0}", GetCalendarName(defaultCalendar))
If TypeOf defaultCalendar Is GregorianCalendar Then
Console.WriteLine(" ({0})",
CType(defaultCalendar, GregorianCalendar).CalendarType)
Else
Console.WriteLine()
End If
' Get the culture's optional calendars.
Console.WriteLine(" Optional Calendars:")
For Each optionalCalendar In ci.OptionalCalendars
Console.Write("{0,6}{1}", "", GetCalendarName(optionalCalendar))
If TypeOf optionalCalendar Is GregorianCalendar Then
Console.Write(" ({0})",
CType(optionalCalendar, GregorianCalendar).CalendarType)
End If
Console.WriteLine()
Next
Console.WriteLine()
End Sub
Function GetCalendarName(cal As Calendar) As String
Return cal.ToString().Replace("System.Globalization.", "")
End Function
End Module
' The example displays the following output:
' Calendars for the th-TH culture:
' Default Calendar: ThaiBuddhistCalendar
' Optional Calendars:
' ThaiBuddhistCalendar
' GregorianCalendar (Localized)
'
' Calendars for the ja-JP culture:
' Default Calendar: GregorianCalendar (Localized)
' Optional Calendars:
' GregorianCalendar (Localized)
' JapaneseCalendar
' GregorianCalendar (USEnglish)
현재 특정 CultureInfo 개체에서 사용 중인 달력은 문화권의 DateTimeFormatInfo.Calendar 속성에 의해 정의됩니다. 문화권의 DateTimeFormatInfo 개체는 CultureInfo.DateTimeFormat 속성에 의해 반환됩니다. 문화권이 만들어지면 기본값은 CultureInfo.Calendar 속성의 값과 동일합니다. 그러나 문화권의 현재 달력을 CultureInfo.OptionalCalendars 속성에서 반환된 배열에 포함된 일정으로 변경할 수 있습니다. 현재 달력을 CultureInfo.OptionalCalendars 속성 값에 포함되지 않은 달력으로 설정하려고 하면 ArgumentException이 발생됩니다.
다음 예제에서는 아랍어(사우디아라비아) 문화권에서 사용하는 달력을 변경합니다. 먼저 DateTime 값을 인스턴스화하고 현재 문화권(이 경우 영어(미국))과 현재 문화권의 달력(이 경우 그레고리력)을 사용하여 표시합니다. 다음으로 현재 문화권을 아랍어(사우디아라비아)로 변경하고 기본 Um Al-Qura 달력을 사용하여 날짜를 표시합니다. 그런 다음 CalendarExists 메서드를 호출하여 히즈리 달력이 아랍어(사우디아라비아) 문화권에서 지원되는지 여부를 확인합니다. 일정이 지원되므로 현재 달력을 Hijri로 변경하고 날짜를 다시 표시합니다. 각 경우에 날짜는 현재 문화권의 현재 달력을 사용하여 표시됩니다.
using System;
using System.Globalization;
using System.Threading;
public class Example
{
public static void Main()
{
DateTime date1 = new DateTime(2011, 6, 20);
DisplayCurrentInfo();
// Display the date using the current culture and calendar.
Console.WriteLine(date1.ToString("d"));
Console.WriteLine();
CultureInfo arSA = CultureInfo.CreateSpecificCulture("ar-SA");
// Change the current culture to Arabic (Saudi Arabia).
Thread.CurrentThread.CurrentCulture = arSA;
// Display date and information about the current culture.
DisplayCurrentInfo();
Console.WriteLine(date1.ToString("d"));
Console.WriteLine();
// Change the calendar to Hijri.
Calendar hijri = new HijriCalendar();
if (CalendarExists(arSA, hijri)) {
arSA.DateTimeFormat.Calendar = hijri;
// Display date and information about the current culture.
DisplayCurrentInfo();
Console.WriteLine(date1.ToString("d"));
}
}
private static void DisplayCurrentInfo()
{
Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.Name}");
Console.WriteLine($"Current Calendar: {DateTimeFormatInfo.CurrentInfo.Calendar}");
}
private static bool CalendarExists(CultureInfo culture, Calendar cal)
{
foreach (Calendar optionalCalendar in culture.OptionalCalendars)
if (cal.ToString().Equals(optionalCalendar.ToString()))
return true;
return false;
}
}
// The example displays the following output:
// Current Culture: en-US
// Current Calendar: System.Globalization.GregorianCalendar
// 6/20/2011
//
// Current Culture: ar-SA
// Current Calendar: System.Globalization.UmAlQuraCalendar
// 18/07/32
//
// Current Culture: ar-SA
// Current Calendar: System.Globalization.HijriCalendar
// 19/07/32
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
Dim date1 As Date = #6/20/2011#
DisplayCurrentInfo()
' Display the date using the current culture and calendar.
Console.WriteLine(date1.ToString("d"))
Console.WriteLine()
Dim arSA As CultureInfo = CultureInfo.CreateSpecificCulture("ar-SA")
' Change the current culture to Arabic (Saudi Arabia).
Thread.CurrentThread.CurrentCulture = arSA
' Display date and information about the current culture.
DisplayCurrentInfo()
Console.WriteLine(date1.ToString("d"))
Console.WriteLine()
' Change the calendar to Hijri.
Dim hijri As Calendar = New HijriCalendar()
If CalendarExists(arSA, hijri) Then
arSA.DateTimeFormat.Calendar = hijri
' Display date and information about the current culture.
DisplayCurrentInfo()
Console.WriteLine(date1.ToString("d"))
End If
End Sub
Private Sub DisplayCurrentInfo()
Console.WriteLine("Current Culture: {0}",
CultureInfo.CurrentCulture.Name)
Console.WriteLine("Current Calendar: {0}",
DateTimeFormatInfo.CurrentInfo.Calendar)
End Sub
Private Function CalendarExists(ByVal culture As CultureInfo,
cal As Calendar) As Boolean
For Each optionalCalendar As Calendar In culture.OptionalCalendars
If cal.ToString().Equals(optionalCalendar.ToString()) Then Return True
Next
Return False
End Function
End Module
' The example displays the following output:
' Current Culture: en-US
' Current Calendar: System.Globalization.GregorianCalendar
' 6/20/2011
'
' Current Culture: ar-SA
' Current Calendar: System.Globalization.UmAlQuraCalendar
' 18/07/32
'
' Current Culture: ar-SA
' Current Calendar: System.Globalization.HijriCalendar
' 19/07/32
날짜 및 일정
Calendar 형식의 매개 변수를 포함하고 날짜의 요소(즉, 월, 일 및 연도)가 지정된 달력의 값을 반영하도록 허용하는 생성자를 제외하고 DateTime 값과 DateTimeOffset 값은 항상 양력에 기반합니다. 예를 들어 DateTime.Year 속성은 그레고리오력에서 연도를 반환하고 DateTime.Day 속성은 그레고리오력에서 해당 월의 일을 반환합니다.
중요합니다
날짜 값과 해당 문자열 표현 사이에는 차이가 있음을 기억해야 합니다. 전자는 그레고리오력을 기반으로 합니다. 후자는 특정 문화권의 현재 달력을 기반으로 합니다.
다음 예제에서는 DateTime 속성과 해당 Calendar 메서드 간의 이러한 차이점을 보여 줍니다. 이 예제에서 현재 문화권은 아랍어(이집트)이고 현재 달력은 Um Al Qura입니다.
DateTime 값은 2011년 7월 15일로 설정됩니다. 이러한 동일한 값은 고정 문화권의 규칙을 사용할 때 DateTime.ToString(String, IFormatProvider) 메서드에서 반환되기 때문에 이는 그레고리오 날짜로 해석됩니다. 현재 문화권의 규칙을 사용하여 형식이 지정된 날짜의 문자열 표현은 Um Al Qura 달력의 동일한 날짜인 14/08/32입니다. 다음으로, DateTime 및 Calendar 멤버는 DateTime 값의 일, 월 및 연도를 반환하는 데 사용됩니다. 각 경우에서 DateTime 멤버가 반환하는 값은 양력의 값을 반영하는 반면, UmAlQuraCalendar 멤버가 반환하는 값은 Uum al-Qura 달력의 값을 반영합니다.
using System;
using System.Globalization;
using System.Threading;
public class Example
{
public static void Main()
{
// Make Arabic (Egypt) the current culture
// and Umm al-Qura calendar the current calendar.
CultureInfo arEG = CultureInfo.CreateSpecificCulture("ar-EG");
Calendar cal = new UmAlQuraCalendar();
arEG.DateTimeFormat.Calendar = cal;
Thread.CurrentThread.CurrentCulture = arEG;
// Display information on current culture and calendar.
DisplayCurrentInfo();
// Instantiate a date object.
DateTime date1 = new DateTime(2011, 7, 15);
// Display the string representation of the date.
Console.WriteLine($"Date: {date1:d}");
Console.WriteLine($"Date in the Invariant Culture: {date1.ToString("d", CultureInfo.InvariantCulture)}");
Console.WriteLine();
// Compare DateTime properties and Calendar methods.
Console.WriteLine($"DateTime.Month property: {date1.Month}");
Console.WriteLine($"UmAlQura.GetMonth: {cal.GetMonth(date1)}");
Console.WriteLine();
Console.WriteLine($"DateTime.Day property: {date1.Day}");
Console.WriteLine($"UmAlQura.GetDayOfMonth: {cal.GetDayOfMonth(date1)}");
Console.WriteLine();
Console.WriteLine($"DateTime.Year property: {date1.Year:D4}");
Console.WriteLine($"UmAlQura.GetYear: {cal.GetYear(date1)}");
Console.WriteLine();
}
private static void DisplayCurrentInfo()
{
Console.WriteLine($"Current Culture: {CultureInfo.CurrentCulture.Name}");
Console.WriteLine($"Current Calendar: {DateTimeFormatInfo.CurrentInfo.Calendar}");
}
}
// The example displays the following output:
// Current Culture: ar-EG
// Current Calendar: System.Globalization.UmAlQuraCalendar
// Date: 14/08/32
// Date in the Invariant Culture: 07/15/2011
//
// DateTime.Month property: 7
// UmAlQura.GetMonth: 8
//
// DateTime.Day property: 15
// UmAlQura.GetDayOfMonth: 14
//
// DateTime.Year property: 2011
// UmAlQura.GetYear: 1432
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
' Make Arabic (Egypt) the current culture
' and Umm al-Qura calendar the current calendar.
Dim arEG As CultureInfo = CultureInfo.CreateSpecificCulture("ar-EG")
Dim cal As Calendar = New UmAlQuraCalendar()
arEG.DateTimeFormat.Calendar = cal
Thread.CurrentThread.CurrentCulture = arEG
' Display information on current culture and calendar.
DisplayCurrentInfo()
' Instantiate a date object.
Dim date1 As Date = #07/15/2011#
' Display the string representation of the date.
Console.WriteLine("Date: {0:d}", date1)
Console.WriteLine("Date in the Invariant Culture: {0}",
date1.ToString("d", CultureInfo.InvariantCulture))
Console.WriteLine()
' Compare DateTime properties and Calendar methods.
Console.WriteLine("DateTime.Month property: {0}", date1.Month)
Console.WriteLine("UmAlQura.GetMonth: {0}",
cal.GetMonth(date1))
Console.WriteLine()
Console.WriteLine("DateTime.Day property: {0}", date1.Day)
Console.WriteLine("UmAlQura.GetDayOfMonth: {0}",
cal.GetDayOfMonth(date1))
Console.WriteLine()
Console.WriteLine("DateTime.Year property: {0:D4}", date1.Year)
Console.WriteLine("UmAlQura.GetYear: {0}",
cal.GetYear(date1))
Console.WriteLine()
End Sub
Private Sub DisplayCurrentInfo()
Console.WriteLine("Current Culture: {0}",
CultureInfo.CurrentCulture.Name)
Console.WriteLine("Current Calendar: {0}",
DateTimeFormatInfo.CurrentInfo.Calendar)
End Sub
End Module
' The example displays the following output:
' Current Culture: ar-EG
' Current Calendar: System.Globalization.UmAlQuraCalendar
' Date: 14/08/32
' Date in the Invariant Culture: 07/15/2011
'
' DateTime.Month property: 7
' UmAlQura.GetMonth: 8
'
' DateTime.Day property: 15
' UmAlQura.GetDayOfMonth: 14
'
' DateTime.Year property: 2011
' UmAlQura.GetYear: 1432
달력을 기반으로 날짜 인스턴스화
DateTime 및 DateTimeOffset 값은 그레고리오력을 기반으로 하기 때문에 다른 달력의 날짜, 월 또는 연도 값을 사용하려면 Calendar 형식의 매개 변수를 포함하는 오버로드된 생성자를 호출하여 날짜 값을 인스턴스화해야 합니다. 특정 일정의 Calendar.ToDateTime 메서드 오버로드 중 하나를 호출하여 특정 달력의 값에 따라 DateTime 개체를 인스턴스화할 수도 있습니다.
다음 예제에서는 DateTime 개체를 HebrewCalendar 생성자에 전달하여 하나의 DateTime 값을 인스턴스화하고 DateTime 메서드를 호출하여 두 번째 HebrewCalendar.ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32) 값을 인스턴스화합니다. 두 값은 히브리어 달력에서 동일한 값으로 만들어지므로 DateTime.Equals 메서드를 호출하면 두 DateTime 값이 같음이 표시됩니다.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
HebrewCalendar hc = new HebrewCalendar();
DateTime date1 = new DateTime(5771, 6, 1, hc);
DateTime date2 = hc.ToDateTime(5771, 6, 1, 0, 0, 0, 0);
Console.WriteLine("{0:d} (Gregorian) = {1:d2}/{2:d2}/{3:d4} ({4}): {5}",
date1,
hc.GetMonth(date2),
hc.GetDayOfMonth(date2),
hc.GetYear(date2),
GetCalendarName(hc),
date1.Equals(date2));
}
private static string GetCalendarName(Calendar cal)
{
return cal.ToString().Replace("System.Globalization.", "").
Replace("Calendar", "");
}
}
// The example displays the following output:
// 2/5/2011 (Gregorian) = 06/01/5771 (Hebrew): True
Imports System.Globalization
Module Example
Public Sub Main()
Dim hc As New HebrewCalendar()
Dim date1 As New Date(5771, 6, 1, hc)
Dim date2 As Date = hc.ToDateTime(5771, 6, 1, 0, 0, 0, 0)
Console.WriteLine("{0:d} (Gregorian) = {1:d2}/{2:d2}/{3:d4} ({4}): {5}",
date1,
hc.GetMonth(date2),
hc.GetDayOfMonth(date2),
hc.GetYear(date2),
GetCalendarName(hc),
date1.Equals(date2))
End Sub
Private Function GetCalendarName(cal As Calendar) As String
Return cal.ToString().Replace("System.Globalization.", "").
Replace("Calendar", "")
End Function
End Module
' The example displays the following output:
' 2/5/2011 (Gregorian) = 06/01/5771 (Hebrew): True
현재 달력의 날짜 표시
날짜 및 시간 서식 지정 메서드는 날짜를 문자열로 변환할 때 항상 현재 달력을 사용합니다. 즉, 연도, 월 및 월의 날짜의 문자열 표현은 현재 달력을 반영하며 반드시 그레고리오력을 반영하지는 않습니다.
다음 예제에서는 현재 달력이 날짜의 문자열 표현에 미치는 영향을 보여 줍니다. 현재 문화권을 중국어(번체, 대만)로 변경하고 날짜 값을 인스턴스화합니다. 그런 다음 현재 달력과 날짜를 표시하고, 현재 달력을 TaiwanCalendar변경하고, 현재 달력과 날짜를 다시 한 번 표시합니다. 날짜가 처음 표시되면 그레고리오력에서 날짜로 표시됩니다. 두 번째로 표시되면 대만 달력에 날짜로 표시됩니다.
using System;
using System.Globalization;
using System.Threading;
public class Example
{
public static void Main()
{
// Change the current culture to zh-TW.
CultureInfo zhTW = CultureInfo.CreateSpecificCulture("zh-TW");
Thread.CurrentThread.CurrentCulture = zhTW;
// Define a date.
DateTime date1 = new DateTime(2011, 1, 16);
// Display the date using the default (Gregorian) calendar.
Console.WriteLine($"Current calendar: {zhTW.DateTimeFormat.Calendar}");
Console.WriteLine(date1.ToString("d"));
// Change the current calendar and display the date.
zhTW.DateTimeFormat.Calendar = new TaiwanCalendar();
Console.WriteLine($"Current calendar: {zhTW.DateTimeFormat.Calendar}");
Console.WriteLine(date1.ToString("d"));
}
}
// The example displays the following output:
// Current calendar: System.Globalization.GregorianCalendar
// 2011/1/16
// Current calendar: System.Globalization.TaiwanCalendar
// 100/1/16
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
' Change the current culture to zh-TW.
Dim zhTW As CultureInfo = CultureInfo.CreateSpecificCulture("zh-TW")
Thread.CurrentThread.CurrentCulture = zhTW
' Define a date.
Dim date1 As Date = #1/16/2011#
' Display the date using the default (Gregorian) calendar.
Console.WriteLine("Current calendar: {0}",
zhTW.DateTimeFormat.Calendar)
Console.WriteLine(date1.ToString("d"))
' Change the current calendar and display the date.
zhTW.DateTimeFormat.Calendar = New TaiwanCalendar()
Console.WriteLine("Current calendar: {0}",
zhTW.DateTimeFormat.Calendar)
Console.WriteLine(date1.ToString("d"))
End Sub
End Module
' The example displays the following output:
' Current calendar: System.Globalization.GregorianCalendar
' 2011/1/16
' Current calendar: System.Globalization.TaiwanCalendar
' 100/1/16
현재가 아닌 달력의 날짜 표시
특정 문화권의 현재 달력이 아닌 달력을 사용하여 날짜를 나타내려면 해당 Calendar 개체의 메서드를 호출해야 합니다. 예를 들어 Calendar.GetYear, Calendar.GetMonth및 Calendar.GetDayOfMonth 메서드는 연도, 월 및 일을 특정 달력을 반영하는 값으로 변환합니다.
경고
일부 일정은 문화권의 선택적 달력이 아니므로 이러한 일정의 날짜를 나타내려면 항상 일정 메서드를 호출해야 합니다. 이는 EastAsianLunisolarCalendar, JulianCalendar및 PersianCalendar 클래스에서 파생되는 모든 일정에 해당합니다.
다음 예제에서는 JulianCalendar 개체를 사용하여 Julian 달력에서 1905년 1월 9일 날짜를 인스턴스화합니다. 이 날짜는 기본(그레고리오력) 달력을 사용하여 표시되면 1905년 1월 22일로 표시됩니다. 개별 JulianCalendar 메서드를 호출하면 날짜가 Julian 달력에 표시될 수 있습니다.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
JulianCalendar julian = new JulianCalendar();
DateTime date1 = new DateTime(1905, 1, 9, julian);
Console.WriteLine("Date ({0}): {1:d}",
CultureInfo.CurrentCulture.Calendar,
date1);
Console.WriteLine("Date in Julian calendar: {0:d2}/{1:d2}/{2:d4}",
julian.GetMonth(date1),
julian.GetDayOfMonth(date1),
julian.GetYear(date1));
}
}
// The example displays the following output:
// Date (System.Globalization.GregorianCalendar): 1/22/1905
// Date in Julian calendar: 01/09/1905
Imports System.Globalization
Module Example
Public Sub Main()
Dim julian As New JulianCalendar()
Dim date1 As New Date(1905, 1, 9, julian)
Console.WriteLine("Date ({0}): {1:d}",
CultureInfo.CurrentCulture.Calendar,
date1)
Console.WriteLine("Date in Julian calendar: {0:d2}/{1:d2}/{2:d4}",
julian.GetMonth(date1),
julian.GetDayOfMonth(date1),
julian.GetYear(date1))
End Sub
End Module
' The example displays the following output:
' Date (System.Globalization.GregorianCalendar): 1/22/1905
' Date in Julian calendar: 01/09/1905
일정 및 날짜 범위
일정에서 지원하는 가장 빠른 날짜는 해당 달력의 Calendar.MinSupportedDateTime 속성으로 표시됩니다. GregorianCalendar 클래스의 경우 해당 날짜는 0001년 1월 1일 C.E. .NET의 다른 일정 대부분은 이후 날짜를 지원합니다. 일정의 지원되는 가장 빠른 날짜 앞에 있는 날짜 및 시간 값을 사용하려고 하면 ArgumentOutOfRangeException 예외가 발생합니다.
그러나 한 가지 중요한 예외가 있습니다. DateTime 개체와 DateTimeOffset 개체의 기본값(초기화되지 않은) 값은 GregorianCalendar.MinSupportedDateTime 값과 같습니다. 0001년 1월 1일 C.E.를 지원하지 않는 달력에서 이 날짜의 서식을 지정하려고 하고 서식 지정자를 제공하지 않는 경우 서식 지정 메서드는 "G"(일반 날짜/시간 패턴) 형식 지정자 대신 "s"(정렬 가능한 날짜/시간 패턴) 형식 지정자를 사용합니다. 따라서 서식 지정 작업은 ArgumentOutOfRangeException 예외를 throw하지 않습니다. 대신 지원되지 않는 날짜를 반환합니다. 다음은 현재 문화권이 일본 달력을 사용하여 일본어(일본)로 설정된 경우 DateTime.MinValue 값을 표시하고, Um Al Qura 달력을 사용하여 아랍어(이집트)로 표시하는 다음 예제에 설명되어 있습니다. 또한 현재 문화권을 영어(미국)로 설정하고 이러한 각 DateTime.ToString(IFormatProvider) 개체를 사용하여 CultureInfo 메서드를 호출합니다. 각 경우에 정렬 가능한 날짜/시간 패턴을 사용하여 날짜가 표시됩니다.
using System;
using System.Globalization;
using System.Threading;
public class Example
{
public static void Main()
{
DateTime dat = DateTime.MinValue;
// Change the current culture to ja-JP with the Japanese Calendar.
CultureInfo jaJP = CultureInfo.CreateSpecificCulture("ja-JP");
jaJP.DateTimeFormat.Calendar = new JapaneseCalendar();
Thread.CurrentThread.CurrentCulture = jaJP;
Console.WriteLine($"Earliest supported date by {GetCalendarName(jaJP)} calendar: {jaJP.DateTimeFormat.Calendar.MinSupportedDateTime:d}");
// Attempt to display the date.
Console.WriteLine(dat.ToString());
Console.WriteLine();
// Change the current culture to ar-EG with the Um Al Qura calendar.
CultureInfo arEG = CultureInfo.CreateSpecificCulture("ar-EG");
arEG.DateTimeFormat.Calendar = new UmAlQuraCalendar();
Thread.CurrentThread.CurrentCulture = arEG;
Console.WriteLine($"Earliest supported date by {GetCalendarName(arEG)} calendar: {arEG.DateTimeFormat.Calendar.MinSupportedDateTime:d}");
// Attempt to display the date.
Console.WriteLine(dat.ToString());
Console.WriteLine();
// Change the current culture to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(dat.ToString(jaJP));
Console.WriteLine(dat.ToString(arEG));
Console.WriteLine(dat.ToString("d"));
}
private static string GetCalendarName(CultureInfo culture)
{
Calendar cal = culture.DateTimeFormat.Calendar;
return cal.GetType().Name.Replace("System.Globalization.", "").Replace("Calendar", "");
}
}
// The example displays the following output:
// Earliest supported date by Japanese calendar: 明治 1/9/8
// 0001-01-01T00:00:00
//
// Earliest supported date by UmAlQura calendar: 01/01/18
// 0001-01-01T00:00:00
//
// 0001-01-01T00:00:00
// 0001-01-01T00:00:00
// 1/1/0001
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
Dim dat As Date = DateTime.MinValue
' Change the current culture to ja-JP with the Japanese Calendar.
Dim jaJP As CultureInfo = CultureInfo.CreateSpecificCulture("ja-JP")
jaJP.DateTimeFormat.Calendar = New JapaneseCalendar()
Thread.CurrentThread.CurrentCulture = jaJP
Console.WriteLine("Earliest supported date by {1} calendar: {0:d}",
jaJP.DateTimeFormat.Calendar.MinSupportedDateTime,
GetCalendarName(jaJP))
' Attempt to display the date.
Console.WriteLine(dat.ToString())
Console.WriteLine()
' Change the current culture to ar-EG with the Um Al Qura calendar.
Dim arEG As CultureInfo = CultureInfo.CreateSpecificCulture("ar-EG")
arEG.DateTimeFormat.Calendar = New UmAlQuraCalendar()
Thread.CurrentThread.CurrentCulture = arEG
Console.WriteLine("Earliest supported date by {1} calendar: {0:d}",
arEG.DateTimeFormat.Calendar.MinSupportedDateTime,
GetCalendarName(arEG))
' Attempt to display the date.
Console.WRiteLine(dat.ToString())
Console.WRiteLine()
' Change the current culture to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Console.WriteLine(dat.ToString(jaJP))
Console.WriteLine(dat.ToString(arEG))
Console.WriteLine(dat.ToString("d"))
End Sub
Private Function GetCalendarName(culture As CultureInfo) As String
Dim cal As Calendar = culture.DateTimeFormat.Calendar
Return cal.GetType().Name.Replace("System.Globalization.", "").Replace("Calendar", "")
End Function
End Module
' The example displays the following output:
' Earliest supported date by Japanese calendar: 明治 1/9/8
' 0001-01-01T00:00:00
'
' Earliest supported date by UmAlQura calendar: 01/01/18
' 0001-01-01T00:00:00
'
' 0001-01-01T00:00:00
' 0001-01-01T00:00:00
' 1/1/0001
연대 작업
달력은 일반적으로 날짜를 연대로 나눕니다. 그러나 .NET의 Calendar 클래스는 달력에 정의된 모든 연대를 지원하지 않으며 대부분의 Calendar 클래스는 단일 연대만 지원합니다. JapaneseCalendar 및 JapaneseLunisolarCalendar 클래스만 여러 연대를 지원합니다.
중요합니다
JapaneseCalendar JapaneseLunisolarCalendar새로운 시대인 레이와 시대는 2019년 5월 1일에 시작됩니다. 이 변경 내용은 이러한 달력을 사용하는 모든 애플리케이션에 영향을 줍니다. 자세한 내용은 다음 아티클을 참조하세요.
- .NET일본 달력에서 새 시대를 처리합니다. 이 기능은 여러 연대가 있는 달력을 지원하기 위해 .NET에 추가된 기능을 문서화하고 다중 시대 달력을 처리할 때 사용할 모범 사례를 설명합니다.
- 일본의 시대 변화에 대비하여 애플리케이션을 준비합니다.Windows에서 애플리케이션을 테스트하여 시대 변화에 대비할 수 있도록 정보를 제공합니다.
- 새로운 일본어 달력 시대와 관련된 개별 Windows 버전에 대한 .NET Framework 업데이트를 나열하는 .NET Framework대한 새로운 일본어 시대 업데이트 요약, 다중 시대 지원을 위한 새로운 .NET Framework 기능에 대해 설명하며 애플리케이션 테스트 시 찾을 항목이 포함되어 있습니다.
대부분의 달력의 시대는 매우 긴 기간을 나타냅니다. 예를 들어 양력에서는 현재 시대가 2천년 이상 지속됩니다. JapaneseCalendar 및 JapaneseLunisolarCalendar여러 연대를 지원하는 두 달력의 경우는 그렇지 않습니다. 시대는 황제의 통치 기간에 해당합니다. 여러 연대에 대한 지원, 특히 현재 시대의 상한을 알 수없는 경우, 특별한 도전을 제기한다.
연대 및 연대 이름
.NET에서 특정 달력 구현에서 지원하는 연대를 나타내는 정수는 Calendar.Eras 배열에 역순으로 저장됩니다. 현재 시대(최신 시간 범위가 있는 시대)는 인덱스 0이며, 여러 연대를 지원하는 Calendar 클래스의 경우 각 연속 인덱스는 이전 시대를 반영합니다. 정적 Calendar.CurrentEra 속성은 Calendar.Eras 배열에서 현재 연대의 인덱스를 정의합니다. 값이 항상 0인 상수입니다. 개별 Calendar 클래스에는 현재 시대의 값을 반환하는 정적 필드도 포함됩니다. 다음 표에 나열되어 있습니다.
특정 연대 번호에 해당하는 이름은 DateTimeFormatInfo.GetEraName 또는 DateTimeFormatInfo.GetAbbreviatedEraName 메서드에 연대 번호를 전달하여 검색할 수 있습니다. 다음 예제에서는 이러한 메서드를 호출하여 GregorianCalendar 클래스의 연대 지원에 대한 정보를 검색합니다. 현재 시대 2년차의 1월 1일에 해당하는 양력 달력 날짜와 지원되는 각 일본 달력 시대의 2년차 1월 1일에 해당하는 양력 날짜가 표시됩니다.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
int year = 2;
int month = 1;
int day = 1;
Calendar cal = new JapaneseCalendar();
Console.WriteLine("\nDate instantiated without an era:");
DateTime date1 = new DateTime(year, month, day, 0, 0, 0, 0, cal);
Console.WriteLine("{0}/{1}/{2} in Japanese Calendar -> {3:d} in Gregorian",
cal.GetMonth(date1), cal.GetDayOfMonth(date1),
cal.GetYear(date1), date1);
Console.WriteLine("\nDates instantiated with eras:");
foreach (int era in cal.Eras) {
DateTime date2 = cal.ToDateTime(year, month, day, 0, 0, 0, 0, era);
Console.WriteLine("{0}/{1}/{2} era {3} in Japanese Calendar -> {4:d} in Gregorian",
cal.GetMonth(date2), cal.GetDayOfMonth(date2),
cal.GetYear(date2), cal.GetEra(date2), date2);
}
}
}
Imports System.Globalization
Module Example
Public Sub Main()
Dim year As Integer = 2
Dim month As Integer = 1
Dim day As Integer = 1
Dim cal As New JapaneseCalendar()
Console.WriteLine("Date instantiated without an era:")
Dim date1 As New Date(year, month, day, 0, 0, 0, 0, cal)
Console.WriteLine("{0}/{1}/{2} in Japanese Calendar -> {3:d} in Gregorian",
cal.GetMonth(date1), cal.GetDayOfMonth(date1),
cal.GetYear(date1), date1)
Console.WriteLine()
Console.WriteLine("Dates instantiated with eras:")
For Each era As Integer In cal.Eras
Dim date2 As Date = cal.ToDateTime(year, month, day, 0, 0, 0, 0, era)
Console.WriteLine("{0}/{1}/{2} era {3} in Japanese Calendar -> {4:d} in Gregorian",
cal.GetMonth(date2), cal.GetDayOfMonth(date2),
cal.GetYear(date2), cal.GetEra(date2), date2)
Next
End Sub
End Module
또한 "g" 사용자 지정 날짜 및 시간 형식 문자열에는 날짜 및 시간의 문자열 표현에 달력의 연대 이름이 포함됩니다. 자세한 내용은 사용자 지정 날짜 및 시간 형식 문자열을 참조하세요.
연대를 사용하여 날짜 인스턴스화
여러 연대를 지원하는 두 Calendar 클래스의 경우 특정 연도, 월 및 일로 구성된 날짜가 모호할 수 있습니다. 예를 들어, JapaneseCalendar이 지원하는 모든 시대에는 연도가 '1'인 해가 있습니다. 일반적으로 연대를 지정하지 않으면 날짜와 시간 및 달력 메서드 모두 값이 현재 연대에 속한다고 가정합니다. 이는 DateTime형식의 매개 변수를 포함하는 DateTimeOffset 및 Calendar 생성자뿐만 아니라 JapaneseCalendar.ToDateTime 및 JapaneseLunisolarCalendar.ToDateTime 메서드도 마찬가지입니다. 다음 예제에서는 지정되지 않은 연대의 2년 1월 1일을 나타내는 날짜를 인스턴스화합니다. 레이와 시대가 현재 시대인 경우 예제를 실행하면 날짜는 레이와 시대의 2년차로 해석됩니다. 令 Era는 DateTime.ToString(String, IFormatProvider) 메서드에서 반환된 문자열의 연도보다 앞이며 그레고리오력에서 2020년 1월 1일에 해당합니다. (레이와 시대는 그레고리오력의 2019년에 시작됩니다.)
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
var japaneseCal = new JapaneseCalendar();
var jaJp = new CultureInfo("ja-JP");
jaJp.DateTimeFormat.Calendar = japaneseCal;
var date = new DateTime(2, 1, 1, japaneseCal);
Console.WriteLine($"Gregorian calendar date: {date:d}");
Console.WriteLine($"Japanese calendar date: {date.ToString("d", jaJp)}");
}
}
Imports System.Globalization
Public Module Example
Public Sub Main()
Dim japaneseCal = New JapaneseCalendar()
Dim jaJp = New CultureInfo("ja-JP")
jaJp.DateTimeFormat.Calendar = japaneseCal
Dim dat = New DateTime(2, 1, 1, japaneseCal)
Console.WriteLine($"Gregorian calendar dat: {dat:d}")
Console.WriteLine($"Japanese calendar dat: {dat.ToString("d", jaJp)}")
End Sub
End Module
그러나 시대가 바뀌면 이 코드의 의도가 모호해집니다. 날짜는 현재 시대의 2 년을 나타내기위한 것입니까, 아니면 헤이세이 시대의 2 년을 대표하기위한 것입니까? 이러한 모호성을 방지하는 방법에는 두 가지가 있습니다.
기본 GregorianCalendar 클래스를 사용하여 날짜 및 시간 값을 인스턴스화합니다. 그런 다음, 다음 예제와 같이 일본어 달력 또는 일본어 Lunisolar 달력을 사용하여 날짜의 문자열 표현을 사용할 수 있습니다.
using System; using System.Globalization; public class Example { public static void Main() { var japaneseCal = new JapaneseCalendar(); var jaJp = new CultureInfo("ja-JP"); jaJp.DateTimeFormat.Calendar = japaneseCal; var date = new DateTime(1905, 2, 12); Console.WriteLine($"Gregorian calendar date: {date:d}"); // Call the ToString(IFormatProvider) method. Console.WriteLine($"Japanese calendar date: {date.ToString("d", jaJp)}"); // Use a FormattableString object. FormattableString fmt = $"{date:d}"; Console.WriteLine($"Japanese calendar date: {fmt.ToString(jaJp)}"); // Use the JapaneseCalendar object. Console.WriteLine($"Japanese calendar date: {jaJp.DateTimeFormat.GetEraName(japaneseCal.GetEra(date))}" + $"{japaneseCal.GetYear(date)}/{japaneseCal.GetMonth(date)}/{japaneseCal.GetDayOfMonth(date)}"); // Use the current culture. CultureInfo.CurrentCulture = jaJp; Console.WriteLine($"Japanese calendar date: {date:d}"); } } // The example displays the following output: // Gregorian calendar date: 2/12/1905 // Japanese calendar date: 明治38/2/12 // Japanese calendar date: 明治38/2/12 // Japanese calendar date: 明治38/2/12 // Japanese calendar date: 明治38/2/12Imports System.Globalization Public Module Example Public Sub Main() Dim japaneseCal = New JapaneseCalendar() Dim jaJp = New CultureInfo("ja-JP") jaJp.DateTimeFormat.Calendar = japaneseCal Dim dat = New DateTime(1905, 2, 12) Console.WriteLine($"Gregorian calendar date: {dat:d}") ' Call the ToString(IFormatProvider) method. Console.WriteLine($"Japanese calendar date: {dat.ToString("d", jaJp)}") ' Use a FormattableString object. Dim fmt As FormattableString = $"{dat:d}" Console.WriteLine($"Japanese calendar date: {fmt.ToString(jaJp)}") ' Use the JapaneseCalendar object. Console.WriteLine($"Japanese calendar date: {jaJp.DateTimeFormat.GetEraName(japaneseCal.GetEra(dat))}" + $"{japaneseCal.GetYear(dat)}/{japaneseCal.GetMonth(dat)}/{japaneseCal.GetDayOfMonth(dat)}") ' Use the current culture. CultureInfo.CurrentCulture = jaJp Console.WriteLine($"Japanese calendar date: {dat:d}") End Sub End Module ' The example displays the following output: ' Gregorian calendar date: 2/12/1905 ' Japanese calendar date: 明治38/2/12 ' Japanese calendar date: 明治38/2/12 ' Japanese calendar date: 明治38/2/12 ' Japanese calendar date: 明治38/2/12연대를 명시적으로 지정하는 날짜 및 시간 메서드를 호출합니다. 여기에는 다음 메서드가 포함됩니다.
ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32) 또는 JapaneseCalendar 클래스의 JapaneseLunisolarCalendar 메서드입니다.
현재 문화권이 Japanese-Japan("ja-JP")이며 그 문화권의 달력이 DateTime인 경우, 선택적으로 DateTimeOffset 인수가 포함되는 문자열을 구문 분석하기 위해 Parse, TryParse, ParseExact또는 TryParseExact와 같은 DateTimeStyles 또는 JapaneseCalendar 구문 분석 메서드입니다. 구문 분석할 문자열에는 연대가 포함되어야 합니다.
DateTime 매개 변수를 DateTimeOffset형식으로 포함하는
provider또는 IFormatProvider 구문 분석 메서드입니다.provider은 CultureInfo인 현재 달력을 가진 Japanese-Japan("ja-JP") 문화권을 나타내는 JapaneseCalendar 개체이거나, DateTimeFormatInfo 속성이 Calendar인 JapaneseCalendar 개체여야 합니다. 구문 분석할 문자열에는 연대가 포함되어야 합니다.
다음 예제에서는 이러한 세 가지 방법을 사용하여 1868년 9월 8일에 시작되어 1912년 7월 29일에 종료된 메이지 시대의 날짜와 시간을 인스턴스화합니다.
using System; using System.Globalization; public class Example { public static void Main() { var japaneseCal = new JapaneseCalendar(); var jaJp = new CultureInfo("ja-JP"); jaJp.DateTimeFormat.Calendar = japaneseCal; // We can get the era index by calling DateTimeFormatInfo.GetEraName. int eraIndex = 0; for (int ctr = 0; ctr < jaJp.DateTimeFormat.Calendar.Eras.Length; ctr++) if (jaJp.DateTimeFormat.GetEraName(ctr) == "明治") eraIndex = ctr; var date1 = japaneseCal.ToDateTime(23, 9, 8, 0, 0, 0, 0, eraIndex); Console.WriteLine($"{date1.ToString("d", jaJp)} (Gregorian {date1:d})"); try { var date2 = DateTime.Parse("明治23/9/8", jaJp); Console.WriteLine($"{date2.ToString("d", jaJp)} (Gregorian {date2:d})"); } catch (FormatException) { Console.WriteLine("The parsing operation failed."); } try { var date3 = DateTime.ParseExact("明治23/9/8", "gyy/M/d", jaJp); Console.WriteLine($"{date3.ToString("d", jaJp)} (Gregorian {date3:d})"); } catch (FormatException) { Console.WriteLine("The parsing operation failed."); } } } // The example displays the following output: // 明治23/9/8 (Gregorian 9/8/1890) // 明治23/9/8 (Gregorian 9/8/1890) // 明治23/9/8 (Gregorian 9/8/1890)Imports System.Globalization Public Module Example Public Sub Main() Dim japaneseCal = New JapaneseCalendar() Dim jaJp = New CultureInfo("ja-JP") jaJp.DateTimeFormat.Calendar = japaneseCal ' We can get the era index by calling DateTimeFormatInfo.GetEraName. Dim eraIndex As Integer = 0 For ctr As Integer = 0 To jaJp.DateTimeFormat.Calendar.Eras.Length - 1 If jaJp.DateTimeFormat.GetEraName(ctr) = "明治" Then eraIndex = ctr Next Dim date1 = japaneseCal.ToDateTime(23, 9, 8, 0, 0, 0, 0, eraIndex) Console.WriteLine($"{date1.ToString("d", jaJp)} (Gregorian {date1:d})") Try Dim date2 = DateTime.Parse("明治23/9/8", jaJp) Console.WriteLine($"{date2.ToString("d", jaJp)} (Gregorian {date2:d})") Catch e As FormatException Console.WriteLine("The parsing operation failed.") End Try Try Dim date3 = DateTime.ParseExact("明治23/9/8", "gyy/M/d", jaJp) Console.WriteLine($"{date3.ToString("d", jaJp)} (Gregorian {date3:d})") Catch e As FormatException Console.WriteLine("The parsing operation failed.") End Try End Sub End Module ' The example displays the following output: ' 明治23/9/8 (Gregorian 9/8/1890) ' 明治23/9/8 (Gregorian 9/8/1890) ' 明治23/9/8 (Gregorian 9/8/1890)
팁 (조언)
여러 연대를 지원하는 달력을 사용하는 경우 항상 그레고리오 날짜를 사용하여 날짜를 인스턴스화하거나 해당 달력에 따라 날짜와 시간을 인스턴스화할 때 연대를 지정할 있습니다.
ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32) 메서드에 연대를 지정할 때 달력의 Eras 속성에 연대의 인덱스도 제공합니다. 그러나 연대가 변경될 수 있는 달력의 경우 이러한 인덱스는 상수 값이 아닙니다. 현재 시대는 인덱스 0이고 가장 오래된 연대는 인덱스 Eras.Length - 1. 달력에 새 연대가 추가되면 이전 연대의 인덱스가 하나씩 증가합니다. 다음과 같이 적절한 연대 인덱스를 제공할 수 있습니다.
현재 시대의 날짜의 경우 항상 달력의 CurrentEra 속성을 사용합니다.
지정된 연대의 날짜에 대해 DateTimeFormatInfo.GetEraName 메서드를 사용하여 지정된 연대 이름에 해당하는 인덱스를 검색합니다. JapaneseCalendar는 ja-JP 문화권을 나타내는 CultureInfo 개체의 현재 달력이어야 합니다. (이 기술은 JapaneseLunisolarCalendar동일한 연대를 지원하므로 JapaneseCalendar에도 작동합니다. 이전 예제에서는 이 방법을 보여 줍니다.
달력, 연대 및 날짜 범위: 완화된 범위 확인
개별 달력에서 지원되는 날짜 범위와 마찬가지로 JapaneseCalendar 및 JapaneseLunisolarCalendar 클래스의 연대도 지원됩니다. 이전에는 .NET에서 엄격한 연대 범위 검사를 사용하여 연대별 날짜가 해당 연대 범위 내에 있는지 확인했습니다. 즉, 날짜가 지정된 연대 범위를 벗어나면 메서드는 ArgumentOutOfRangeExceptionthrow합니다. 현재 .NET은 기본적으로 완화된 범위 검사를 사용합니다. .NET의 모든 버전에 대한 업데이트에서는 완화된 연대 범위 검사를 도입했습니다. 지정된 연대 범위를 벗어난 연대별 날짜를 다음 연대로 인스턴스화하려는 시도에서는 예외가 발생하지 않습니다.
다음 예제에서는 1926년 12월 25일에 시작되어 1989년 1월 7일에 종료된 쇼와 시대의 65번째 해에 날짜를 인스턴스화하려고 시도합니다. 이 날짜는 1990년 1월 9일에 해당하며, 이 날짜는 JapaneseCalendar쇼와 시대의 범위를 벗어났습니다. 예제의 출력에서 알 수 있듯이 예제에 표시된 날짜는 헤이세이 시대 2년차인 1990년 1월 9일입니다.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
var jaJp = new CultureInfo("ja-JP");
var cal = new JapaneseCalendar();
jaJp.DateTimeFormat.Calendar = cal;
string showaEra = "昭和";
var dt = cal.ToDateTime(65, 1, 9, 15, 0, 0, 0, GetEraIndex(showaEra));
FormattableString fmt = $"{dt:d}";
Console.WriteLine($"Japanese calendar date: {fmt.ToString(jaJp)}");
Console.WriteLine($"Gregorian calendar date: {fmt}");
int GetEraIndex(string eraName)
{
foreach (var ctr in cal.Eras)
if (jaJp.DateTimeFormat.GetEraName(ctr) == eraName)
return ctr;
return 0;
}
}
}
// The example displays the following output:
// Japanese calendar date: 平成2/1/9
// Gregorian calendar date: 1/9/1990
Imports System.Globalization
Public Module Example
Dim jaJp As CultureInfo
Dim cal As Calendar
Public Sub Main()
jaJp = New CultureInfo("ja-JP")
cal = New JapaneseCalendar()
jaJp.DateTimeFormat.Calendar = cal
Dim showaEra = "昭和"
Dim dt = cal.ToDateTime(65, 1, 9, 15, 0, 0, 0, GetEraIndex(showaEra))
Dim fmt As FormattableString = $"{dt:d}"
Console.WriteLine($"Japanese calendar date: {fmt.ToString(jaJp)}")
Console.WriteLine($"Gregorian calendar date: {fmt}")
End Sub
Private Function GetEraIndex(eraName As String) As Integer
For Each ctr As Integer In cal.Eras
If jaJp.DateTimeFormat.GetEraName(ctr) = eraName Then Return ctr
Next
Return 0
End Function
End Module
' The example displays the following output:
' Japanese calendar date: 平成2/1/9
' Gregorian calendar date: 1/9/1990
완화된 범위 검사가 바람직하지 않은 경우 애플리케이션이 실행 중인 .NET 버전에 따라 여러 가지 방법으로 엄격한 범위 검사를 복원할 수 있습니다.
.NET Core :.netcore.runtime.json 구성 파일에 다음을 추가합니다.
"runtimeOptions": { "configProperties": { "Switch.System.Globalization.EnforceJapaneseEraYearRanges": true } }.NET Framework 4.6 이상 :app.config 파일에서 다음 AppContext 스위치를 설정합니다.
<?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <AppContextSwitchOverrides value="Switch.System.Globalization.EnforceJapaneseEraYearRanges=true" /> </runtime> </configuration>.NET Framework 4.5.2 이전 버전 : 다음 레지스트리 값을 설정합니다.
가치 키 HKEY_LOCAL_MACHINE\Software\Microsoft\.NETFramework\AppContext 항목 Switch.System.Globalization.일본 연호 연도 범위 강제 적용 유형 REG_SZ 값 맞다
엄격한 범위 검사를 사용하도록 설정한 상태에서 이전 예제에서는 ArgumentOutOfRangeException throw하고 다음 출력을 표시합니다.
Unhandled Exception: System.ArgumentOutOfRangeException: Valid values are between 1 and 64, inclusive.
Parameter name: year
at System.Globalization.GregorianCalendarHelper.GetYearOffset(Int32 year, Int32 era, Boolean throwOnError)
at System.Globalization.GregorianCalendarHelper.ToDateTime(Int32 year, Int32 month, Int32 day, Int32 hour, Int32 minute, Int32 second, Int32 millisecond, Int32 era)
at Example.Main()
여러 연대가 있는 달력의 날짜 표시
Calendar 개체가 연대를 지원하고 CultureInfo 개체의 현재 달력인 경우 해당 연대는 전체 날짜 및 시간, 긴 날짜 및 짧은 날짜 패턴에 대한 날짜 및 시간 값의 문자열 표현에 포함됩니다. 다음은 현재 문화권이 일본(일본)이고 현재 달력이 일본 달력인 경우 이러한 날짜 패턴을 표시하는 예제입니다.
using System;
using System.Globalization;
using System.IO;
using System.Threading;
public class Example
{
public static void Main()
{
StreamWriter sw = new StreamWriter(@".\eras.txt");
DateTime dt = new DateTime(2012, 5, 1);
CultureInfo culture = CultureInfo.CreateSpecificCulture("ja-JP");
DateTimeFormatInfo dtfi = culture.DateTimeFormat;
dtfi.Calendar = new JapaneseCalendar();
Thread.CurrentThread.CurrentCulture = culture;
sw.WriteLine("\n{0,-43} {1}", "Full Date and Time Pattern:", dtfi.FullDateTimePattern);
sw.WriteLine(dt.ToString("F"));
sw.WriteLine();
sw.WriteLine("\n{0,-43} {1}", "Long Date Pattern:", dtfi.LongDatePattern);
sw.WriteLine(dt.ToString("D"));
sw.WriteLine("\n{0,-43} {1}", "Short Date Pattern:", dtfi.ShortDatePattern);
sw.WriteLine(dt.ToString("d"));
sw.Close();
}
}
// The example writes the following output to a file:
// Full Date and Time Pattern: gg y'年'M'月'd'日' H:mm:ss
// 平成 24年5月1日 0:00:00
//
// Long Date Pattern: gg y'年'M'月'd'日'
// 平成 24年5月1日
//
// Short Date Pattern: gg y/M/d
// 平成 24/5/1
Imports System.Globalization
Imports System.IO
Imports System.Threading
Module Example
Public Sub Main()
Dim sw As New StreamWriter(".\eras.txt")
Dim dt As Date = #05/01/2012#
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("ja-JP")
Dim dtfi As DateTimeFormatInfo = culture.DateTimeFormat
dtfi.Calendar = New JapaneseCalendar()
Thread.CurrentThread.CurrentCulture = culture
sw.WriteLine("{0,-43} {1}", "Full Date and Time Pattern:", dtfi.FullDateTimePattern)
sw.WriteLine(dt.ToString("F"))
sw.WriteLine()
sw.WriteLine("{0,-43} {1}", "Long Date Pattern:", dtfi.LongDatePattern)
sw.WriteLine(dt.ToString("D"))
sw.WriteLine()
sw.WriteLine("{0,-43} {1}", "Short Date Pattern:", dtfi.ShortDatePattern)
sw.WriteLine(dt.ToString("d"))
sw.WriteLine()
sw.Close()
End Sub
End Module
' The example writes the following output to a file:
' Full Date and Time Pattern: gg y'年'M'月'd'日' H:mm:ss
' 平成 24年5月1日 0:00:00
'
' Long Date Pattern: gg y'年'M'月'd'日'
' 平成 24年5月1日
'
' Short Date Pattern: gg y/M/d
' 平成 24/5/1
경고
JapaneseCalendar 클래스는 .NET에서 여러 연대의 날짜를 지원하며 CultureInfo 개체의 현재 달력이 될 수 있는 유일한 달력 클래스입니다. 특히, 이는 일본(일본어) 문화권을 나타내는 CultureInfo 개체를 위해 사용됩니다.
모든 달력의 경우 "g" 사용자 지정 형식 지정자에 결과 문자열의 연대가 포함됩니다. 다음 예제에서는 "MM-dd-yyyy g" 사용자 지정 형식 문자열을 사용하여 현재 달력이 그레고리오력인 경우 결과 문자열에 연대를 포함합니다.
DateTime dat = new DateTime(2012, 5, 1);
Console.WriteLine($"{dat:MM-dd-yyyy g}");
// The example displays the following output:
// 05-01-2012 A.D.
Dim dat As Date = #05/01/2012#
Console.WriteLine("{0:MM-dd-yyyy g}", dat)
' The example displays the following output:
' 05-01-2012 A.D.
날짜의 문자열 표현이 현재 달력이 아닌 달력에서 표현되는 경우 Calendar 클래스에는 날짜와 날짜가 속한 연대를 명확하게 나타내기 위해 Calendar.GetEra, Calendar.GetYear및 Calendar.GetMonth 메서드와 함께 사용할 수 있는 Calendar.GetDayOfMonth 메서드가 포함됩니다. 다음 예제에서는 JapaneseLunisolarCalendar 클래스를 사용하여 그림을 제공합니다. 그러나 결과 문자열에서 연대의 정수 대신 의미 있는 이름이나 약어를 포함하려면 DateTimeFormatInfo 개체를 인스턴스화하고 JapaneseCalendar을 현재 달력으로 설정해야 합니다. (JapaneseLunisolarCalendar 달력은 문화권의 현재 달력일 수 없지만, 이 경우 두 달력은 동일한 연대를 공유합니다.)
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
DateTime date1 = new DateTime(2011, 8, 28);
Calendar cal = new JapaneseLunisolarCalendar();
Console.WriteLine("{0} {1:d4}/{2:d2}/{3:d2}",
cal.GetEra(date1),
cal.GetYear(date1),
cal.GetMonth(date1),
cal.GetDayOfMonth(date1));
// Display eras
CultureInfo culture = CultureInfo.CreateSpecificCulture("ja-JP");
DateTimeFormatInfo dtfi = culture.DateTimeFormat;
dtfi.Calendar = new JapaneseCalendar();
Console.WriteLine("{0} {1:d4}/{2:d2}/{3:d2}",
dtfi.GetAbbreviatedEraName(cal.GetEra(date1)),
cal.GetYear(date1),
cal.GetMonth(date1),
cal.GetDayOfMonth(date1));
}
}
// The example displays the following output:
// 4 0023/07/29
// 平 0023/07/29
Imports System.Globalization
Module Example
Public Sub Main()
Dim date1 As Date = #8/28/2011#
Dim cal As New JapaneseLunisolarCalendar()
Console.WriteLine("{0} {1:d4}/{2:d2}/{3:d2}",
cal.GetEra(date1),
cal.GetYear(date1),
cal.GetMonth(date1),
cal.GetDayOfMonth(date1))
' Display eras
Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("ja-JP")
Dim dtfi As DateTimeFormatInfo = culture.DateTimeFormat
dtfi.Calendar = New JapaneseCalendar()
Console.WriteLine("{0} {1:d4}/{2:d2}/{3:d2}",
dtfi.GetAbbreviatedEraName(cal.GetEra(date1)),
cal.GetYear(date1),
cal.GetMonth(date1),
cal.GetDayOfMonth(date1))
End Sub
End Module
' The example displays the following output:
' 4 0023/07/29
' 平 0023/07/29
일본 달력에서 시대의 첫 해는 Gannen (元年)이라고합니다. 예를 들어 헤이세이 1 대신 헤이세이 시대의 첫 해를 헤이세이 가넨으로 묘사할 수 있습니다. .NET은 CultureInfo 클래스를 사용하여 Japanese-Japan("ja-JP") 문화권을 나타내는 JapaneseCalendar 개체와 함께 사용될 때 다음 표준 또는 사용자 지정 날짜 및 시간 형식 문자열로 서식이 지정된 날짜 및 시간에 대한 서식 지정 작업에서 이 규칙을 채택합니다.
- "D" 표준 날짜 및 시간 형식 문자열로 표시된 긴 날짜 패턴.
- "F" 표준 날짜 및 시간 형식 문자열로 표시된 전체 날짜 긴 시간 패턴.
- "f" 표준 날짜 및 시간 형식 문자열로 표시된 전체 날짜 짧은 시간 패턴.
- "Y" 또는 "y" 표준 날짜 및 시간 형식 문자열로 표시된 연도/월 패턴.
- "ggy'年'" 또는 "ggy年"사용자 지정 날짜 및 시간 형식 문자열.
예를 들어, 다음 예제는 JapaneseCalendar형식으로 헤이세이 시대 첫 해의 날짜를 표시합니다.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
var enUs = new CultureInfo("en-US");
var japaneseCal = new JapaneseCalendar();
var jaJp = new CultureInfo("ja-JP");
jaJp.DateTimeFormat.Calendar = japaneseCal;
string heiseiEra = "平成";
var date = japaneseCal.ToDateTime(1, 8, 18, 0, 0, 0, 0, GetEraIndex(heiseiEra));
FormattableString fmt = $"{date:D}";
Console.WriteLine($"Japanese calendar date: {fmt.ToString(jaJp)} (Gregorian: {fmt.ToString(enUs)})");
int GetEraIndex(string eraName)
{
foreach (var ctr in japaneseCal.Eras)
if (jaJp.DateTimeFormat.GetEraName(ctr) == eraName)
return ctr;
return 0;
}
}
}
// The example displays the following output:
// Japanese calendar date: 平成元年8月18日 (Gregorian: Friday, August 18, 1989)
Imports System.Globalization
Module Program
Dim jaJp As CultureInfo
Dim japaneseCal As Calendar
Sub Main()
Dim enUs = New CultureInfo("en-US")
japaneseCal = New JapaneseCalendar()
jaJp = New CultureInfo("ja-JP")
jaJp.DateTimeFormat.Calendar = japaneseCal
Dim heiseiEra = "平成"
Dim dat = japaneseCal.ToDateTime(1, 8, 18, 0, 0, 0, 0, GetEraIndex(heiseiEra))
Dim fmt As FormattableString = $"{dat:D}"
Console.WriteLine($"Japanese calendar date: {fmt.ToString(jaJp)} (Gregorian: {fmt.ToString(enUs)})")
End Sub
Private Function GetEraIndex(eraName As String) As Integer
For Each ctr In japaneseCal.Eras
If jaJp.DateTimeFormat.GetEraName(ctr) = eraName Then
Return ctr
End If
Next
Return 0
End Function
End Module
' The example displays the following output:
' Japanese calendar date: 平成元年8月18日 (Gregorian: Friday, August 18, 1989)
서식 지정 작업에서 이 동작이 바람직하지 않은 경우 .NET 버전에 따라 다음을 수행하여 항상 연대의 첫 해를 "Gannen"이 아닌 "1"로 나타내는 이전 동작을 복원할 수 있습니다.
.NET Core :.netcore.runtime.json 구성 파일에 다음을 추가합니다.
"runtimeOptions": { "configProperties": { "Switch.System.Globalization.FormatJapaneseFirstYearAsANumber": true } }.NET Framework 4.6 이상 :app.config 파일에서 다음 AppContext 스위치를 설정합니다.
<?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <AppContextSwitchOverrides value="Switch.System.Globalization.FormatJapaneseFirstYearAsANumber=true" /> </runtime> </configuration>.NET Framework 4.5.2 이전 버전 : 다음 레지스트리 값을 설정합니다.
가치 키 HKEY_LOCAL_MACHINE\Software\Microsoft\.NETFramework\AppContext 항목 스위치.시스템.세계화.일본첫해를숫자로형식화합니다. 유형 REG_SZ 값 맞다
서식 지정 작업에서 gannen 지원을 사용하지 않도록 설정하면 이전 예제에서는 다음 출력을 표시합니다.
Japanese calendar date: 平成1年8月18日 (Gregorian: Friday, August 18, 1989)
또한 날짜 및 시간 구문 분석 작업이 "1" 또는 Gannen으로 표시된 연도를 포함하는 문자열을 지원하게 되도록 .NET도 업데이트되었습니다. 이 작업을 수행할 필요는 없지만 이전 동작을 복원하여 "1"만 시대의 첫 해로 인식할 수 있습니다. .NET 버전에 따라 다음과 같이 이 작업을 수행할 수 있습니다.
.NET Core :.netcore.runtime.json 구성 파일에 다음을 추가합니다.
"runtimeOptions": { "configProperties": { "Switch.System.Globalization.EnforceLegacyJapaneseDateParsing": true } }.NET Framework 4.6 이상 :app.config 파일에서 다음 AppContext 스위치를 설정합니다.
<?xml version="1.0" encoding="utf-8"?> <configuration> <runtime> <AppContextSwitchOverrides value="Switch.System.Globalization.EnforceLegacyJapaneseDateParsing=true" /> </runtime> </configuration>.NET Framework 4.5.2 이전 버전 : 다음 레지스트리 값을 설정합니다.
가치 키 HKEY_LOCAL_MACHINE\Software\Microsoft\.NETFramework\AppContext 항목 스위치.시스템.글로벌라이제이션.레거시 일본 날짜 분석 적용 유형 REG_SZ 값 맞다
참고하십시오
.NET