JapaneseCalendar.ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32) JapaneseCalendar.ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32) JapaneseCalendar.ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32) JapaneseCalendar.ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32) Method

定义

返回设置为指定纪元中指定日期和时间的 DateTimeReturns a DateTime that is set to the specified date and time in the specified era.

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 Int32 Int32 Int32

一个整数,用于表示年份。An integer that represents the year.

month
Int32 Int32 Int32 Int32

1 到 12 之间的一个整数,用于表示月份。An integer from 1 to 12 that represents the month.

day
Int32 Int32 Int32 Int32

1 到 31 之间的一个整数,用于表示天。An integer from 1 to 31 that represents the day.

hour
Int32 Int32 Int32 Int32

0 到 23 之间的一个整数,用于表示小时。An integer from 0 to 23 that represents the hour.

minute
Int32 Int32 Int32 Int32

0 到 59 之间的一个整数,用于表示分钟。An integer from 0 to 59 that represents the minute.

second
Int32 Int32 Int32 Int32

0 到 59 之间的一个整数,用于表示秒。An integer from 0 to 59 that represents the second.

millisecond
Int32 Int32 Int32 Int32

0 到 999 之间的一个整数,用于表示毫秒。An integer from 0 to 999 that represents the millisecond.

era
Int32 Int32 Int32 Int32

一个整数,用于表示纪元。An integer that represents the era.

返回

设置为当前纪元中指定日期和时间的 DateTimeThe DateTime that is set to the specified date and time in the current era.

异常

year 超出了日历支持的范围。year is outside the range supported by the calendar.

-or- month 超出了日历支持的范围。month is outside the range supported by the calendar.

-or- day 超出了日历支持的范围。day is outside the range supported by the calendar.

-or- hour 小于 0 或大于 23。hour is less than zero or greater than 23.

-or- minute 小于 0 或大于 59。minute is less than zero or greater than 59.

-or- second 小于 0 或大于 59。second is less than zero or greater than 59.

- 或 --or- millisecond 小于 0 或大于 999。millisecond is less than zero or greater than 999.

-or- era 超出了日历支持的范围。era is outside the range supported by the calendar.

示例

下面的示例实例化两个日期。The following example instantiates two dates. 第一个始终是当前纪元中第二年的第一天, 而第二个则标识 Taisho 纪元中的特定日期。The first is always the first day of the second year in the current era, while the second identifies a specific day in the Taisho era. 该示例的输出是通过 Heisei 纪元作为当前纪元生成的。The output from the example was produced with the Heisei era as the current era.

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方法非常有用, 因为它可以将当前日历中的任何日期转换为公历日期。The ToDateTime method is useful because it can convert any date in the current calendar to a Gregorian calendar date. 以后可以使用公历日期来比较不同日历中的日期或在特定日历中创建等效日期。The Gregorian date can subsequently be used, for example, to compare dates in different calendars or create an equivalent date in a particular calendar.

由于基于高层的 reign支持多个纪元,因此应始终调用此方法并显式指定纪元以避免意外的日期,并使代码的意图清晰。JapaneseCalendarBecause the JapaneseCalendar supports multiple eras based on the reign of the emperor, you should always call this method and explicitly specify an era to avoid an unintended date and to make the intent of your code clear. 该示例演示如何实例化始终在当前纪元中的日期和属于指定纪元的日期。The example shows how to instantiate a date that is always in the current era and one that belongs to a specified era.

适用于

另请参阅