JapaneseCalendar.ToDateTime(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32) Método

Definición

Devuelve un DateTime que se establece en la fecha y hora de la era especificada.Returns 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

Parámetros

year
Int32

Entero que representa el año.An integer that represents the year.

month
Int32

Entero de 1 a 12 que representa el mes.An integer from 1 to 12 that represents the month.

day
Int32

Entero de 1 a 31 que representa el día.An integer from 1 to 31 that represents the day.

hour
Int32

Entero de 0 a 23 que representa la hora.An integer from 0 to 23 that represents the hour.

minute
Int32

Entero de 0 a 59 que representa el minuto.An integer from 0 to 59 that represents the minute.

second
Int32

Entero de 0 a 59 que representa el segundo.An integer from 0 to 59 that represents the second.

millisecond
Int32

Entero de 0 a 999 que representa el milisegundo.An integer from 0 to 999 that represents the millisecond.

era
Int32

Entero que representa la era.An integer that represents the era.

Devoluciones

Objeto DateTime que se establece en la fecha y hora especificadas de la era actual.The DateTime that is set to the specified date and time in the current era.

Excepciones

year está fuera del intervalo que admite el calendario.year is outside the range supported by the calendar.

o bien-or- month está fuera del intervalo que admite el calendario.month is outside the range supported by the calendar.

o bien-or- day está fuera del intervalo que admite el calendario.day is outside the range supported by the calendar.

o bien-or- hour es menor que cero o mayor que 23.hour is less than zero or greater than 23.

o bien-or- minute es menor que cero o mayor que 59.minute is less than zero or greater than 59.

o bien-or- second es menor que cero o mayor que 59.second is less than zero or greater than 59.

o bien-or- millisecond es menor que cero o mayor que 999.millisecond is less than zero or greater than 999.

o bien-or- era está fuera del intervalo que admite el calendario.era is outside the range supported by the calendar.

Ejemplos

En el ejemplo siguiente se crean instancias de dos fechas.The following example instantiates two dates. El primero es siempre el primer día del segundo año de la era actual, mientras que el segundo identifica un día concreto en la era 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. La salida del ejemplo se generó con la era Heisei como la era actual.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

Comentarios

El método ToDateTime es útil porque puede convertir cualquier fecha del calendario actual en una fecha del calendario gregoriano.The ToDateTime method is useful because it can convert any date in the current calendar to a Gregorian calendar date. Posteriormente, la fecha Gregoriana se puede usar, por ejemplo, para comparar fechas de calendarios diferentes o crear una fecha equivalente en un calendario determinado.The Gregorian date can subsequently be used, for example, to compare dates in different calendars or create an equivalent date in a particular calendar.

Dado que el JapaneseCalendar admite varias eras basadas en el Reino del emperador, siempre debe llamar a este método y especificar explícitamente una era para evitar una fecha no deseada y para que la intención del código sea clara.Because 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. En el ejemplo se muestra cómo crear una instancia de una fecha que siempre está en la era actual y otra que pertenece a una era especificada.The example shows how to instantiate a date that is always in the current era and one that belongs to a specified era.

Se aplica a

Consulte también: