JapaneseCalendar.ToDateTime 方法

定義

傳回在指定紀元中設定為指定日期和時間的 DateTime

public:
 override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era);
public override DateTime ToDateTime (int year, int month, int day, int hour, int minute, int second, int millisecond, int era);
override this.ToDateTime : int * int * int * int * int * int * int * int -> DateTime
Public Overrides Function ToDateTime (year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, millisecond As Integer, era As Integer) As DateTime

參數

year
Int32

表示年份的整數。

month
Int32

從 1 到 12 的整數,表示月份。

day
Int32

從 1 到 31 的整數,表示日期。

hour
Int32

從 0 到 23 的整數,表示小時。

minute
Int32

從 0 到 59 的整數,表示分鐘。

second
Int32

從 0 到 59 的整數,表示秒鐘。

millisecond
Int32

從 0 到 999 的整數,表示毫秒。

era
Int32

表示紀元的整數。

傳回

DateTime

DateTime,設定為目前紀元中指定的日期和時間。

例外狀況

year 不在曆法支援的範圍內。

-或- month 不在曆法支援的範圍內。

-或- day 不在曆法支援的範圍內。

-或- hour 小於 0 或大於 23。

-或- minute 小於 0 或大於 59。

-或- second 小於 0 或大於 59。

-或- millisecond 小於 0 或大於 999。

-或- era 不在曆法支援的範圍內。

範例

下列範例會具現化兩個日期。 第一個是目前紀元中第二年的第一天,而第二個會識別 Taisho 紀元中的特定日期。 範例的輸出是以 Heisei 紀元作為目前紀元產生。

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        var cal = new JapaneseCalendar();
        var jaJp = new CultureInfo("ja-JP");
        jaJp.DateTimeFormat.Calendar = cal;
        var date1 = cal.ToDateTime(2,1,1,0,0,0,0,JapaneseCalendar.CurrentEra);
        Console.WriteLine($"Japanese calendar date: {date1.ToString("D", jaJp)}, " +
                          $"Gregorian calendar date: {date1.ToString("D", CultureInfo.InvariantCulture)}");

        var date2 = cal.ToDateTime(6,11,7,0,0,0,0,GetEraIndex("大正"));
        Console.WriteLine($"Japanese calendar date: {date2.ToString("D", jaJp)}, " +
                          $"Gregorian calendar date: {date2.ToString("D", CultureInfo.InvariantCulture)}");

        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月1日, Gregorian calendar date: Monday, 01 January 1990
//   Japanese calendar date: 大正6年11月7日, Gregorian calendar date: Wednesday, 07 November 1917
Imports System.Globalization

Module Program
    Dim cal As Calendar
    Dim jaJp As CultureInfo
    
    Public Sub Main()
        cal = New JapaneseCalendar()
        jaJp = New CultureInfo("ja-JP")
        jaJp.DateTimeFormat.Calendar = cal
        Dim date1 = cal.ToDateTime(2,1,1,0,0,0,0,JapaneseCalendar.CurrentEra)
        Console.WriteLine($"Japanese calendar date: {date1.ToString("D", jaJp)}, " +
                          $"Gregorian calendar date: {date1.ToString("D", CultureInfo.InvariantCulture)}")

        Dim date2 = cal.ToDateTime(6,11,7,0,0,0,0,GetEraIndex("大正"))
        Console.WriteLine($"Japanese calendar date: {date2.ToString("D", jaJp)}, " +
                          $"Gregorian calendar date: {date2.ToString("D", CultureInfo.InvariantCulture)}")
    End Sub

    Private Function GetEraIndex(eraName As String) As Integer
        For Each ctr 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月1日, Gregorian calendar date: Monday, 01 January 1990
'   Japanese calendar date: 大正6年11月7日, Gregorian calendar date: Wednesday, 07 November 1917

備註

方法 ToDateTime 很有用,因為它可以將目前行事曆中的任何日期轉換成西曆日期。 例如,您可以後續使用西曆日期來比較不同行事曆中的日期,或在特定行事曆中建立相等的日期。

JapaneseCalendar由於 支援根據主旨的多個紀元,因此您應該一律呼叫此方法,並明確指定紀元以避免非預期的日期,並清楚表達程式碼的意圖。 此範例示範如何具現化一律在目前紀元中的日期,以及屬於指定紀元的日期。

適用於

另請參閱