HebrewCalendar.AddYears(DateTime, Int32) Método

Definição

Devolve um DateTime que é o número de anos especificado a partir do especificado DateTime.

public:
 override DateTime AddYears(DateTime time, int years);
public override DateTime AddYears(DateTime time, int years);
override this.AddYears : DateTime * int -> DateTime
Public Overrides Function AddYears (time As DateTime, years As Integer) As DateTime

Parâmetros

time
DateTime

O DateTime que se acrescenta years.

years
Int32

O número de anos a acrescentar.

Devoluções

O DateTime que resulta da soma do número especificado de anos ao especificado DateTime.

Exceções

O resultado DateTime está fora do alcance suportado.

Exemplos

O seguinte exemplo de código mostra os valores de vários componentes de a DateTime em termos do calendário hebraico.

using System;
using System.Globalization;

public class SamplesHebrewCalendar  {

   public static void Main()  {

      // Sets a DateTime to April 3, 2002 of the Gregorian calendar.
      DateTime myDT = new DateTime( 2002, 4, 3, new GregorianCalendar() );

      // Creates an instance of the HebrewCalendar.
      HebrewCalendar myCal = new HebrewCalendar();

      // Displays the values of the DateTime.
      Console.WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Hebrew calendar:" );
      DisplayValues( myCal, myDT );

      // Adds two years and ten months.
      myDT = myCal.AddYears( myDT, 2 );
      myDT = myCal.AddMonths( myDT, 10 );

      // Displays the values of the DateTime.
      Console.WriteLine( "After adding two years and ten months:" );
      DisplayValues( myCal, myDT );
   }

   public static void DisplayValues( Calendar myCal, DateTime myDT )  {
      Console.WriteLine( "   Era:        {0}", myCal.GetEra( myDT ) );
      Console.WriteLine( "   Year:       {0}", myCal.GetYear( myDT ) );
      Console.WriteLine( "   Month:      {0}", myCal.GetMonth( myDT ) );
      Console.WriteLine( "   DayOfYear:  {0}", myCal.GetDayOfYear( myDT ) );
      Console.WriteLine( "   DayOfMonth: {0}", myCal.GetDayOfMonth( myDT ) );
      Console.WriteLine( "   DayOfWeek:  {0}", myCal.GetDayOfWeek( myDT ) );
      Console.WriteLine();
   }
}

/*
This code produces the following output.

April 3, 2002 of the Gregorian calendar equals the following in the Hebrew calendar:
   Era:        1
   Year:       5762
   Month:      7
   DayOfYear:  198
   DayOfMonth: 21
   DayOfWeek:  Wednesday

After adding two years and ten months:
   Era:        1
   Year:       5765
   Month:      5
   DayOfYear:  138
   DayOfMonth: 21
   DayOfWeek:  Monday

*/
Imports System.Globalization


Public Class SamplesHebrewCalendar   

   Public Shared Sub Main()

      ' Sets a DateTime to April 3, 2002 of the Gregorian calendar.
      Dim myDT As New DateTime(2002, 4, 3, New GregorianCalendar())

      ' Creates an instance of the HebrewCalendar.
      Dim myCal As New HebrewCalendar()

      ' Displays the values of the DateTime.
      Console.WriteLine("April 3, 2002 of the Gregorian calendar equals the following in the Hebrew calendar:")
      DisplayValues(myCal, myDT)

      ' Adds two years and ten months.
      myDT = myCal.AddYears(myDT, 2)
      myDT = myCal.AddMonths(myDT, 10)

      ' Displays the values of the DateTime.
      Console.WriteLine("After adding two years and ten months:")
      DisplayValues(myCal, myDT)

   End Sub

   Public Shared Sub DisplayValues(myCal As Calendar, myDT As DateTime)
      Console.WriteLine("   Era:        {0}", myCal.GetEra(myDT))
      Console.WriteLine("   Year:       {0}", myCal.GetYear(myDT))
      Console.WriteLine("   Month:      {0}", myCal.GetMonth(myDT))
      Console.WriteLine("   DayOfYear:  {0}", myCal.GetDayOfYear(myDT))
      Console.WriteLine("   DayOfMonth: {0}", myCal.GetDayOfMonth(myDT))
      Console.WriteLine("   DayOfWeek:  {0}", myCal.GetDayOfWeek(myDT))
      Console.WriteLine()
   End Sub

End Class


'This code produces the following output.
'
'April 3, 2002 of the Gregorian calendar equals the following in the Hebrew calendar:
'   Era:        1
'   Year:       5762
'   Month:      7
'   DayOfYear:  198
'   DayOfMonth: 21
'   DayOfWeek:  Wednesday
'
'After adding two years and ten months:
'   Era:        1
'   Year:       5765
'   Month:      5
'   DayOfYear:  138
'   DayOfMonth: 21
'   DayOfWeek:  Monday

Observações

Esta implementação da HebrewCalendar classe reconhece apenas os anos hebraicos de 5343 a 5999 (1583 a 2239 no calendário gregoriano).

A parte do dia resultante DateTime é afetada se o dia resultante não for válido no mês resultante do ano resultante. É alterado para o último dia válido do mês resultante do ano seguinte. Por exemplo, Cheshvan pode ter 29 ou 30 dias, dependendo da localização dos feriados judaicos. Suponha que Cheshvan tem 30 dias no ano corrente e 29 no ano seguinte. Se a data especificada for o 30.º dia de Cheshvan no ano corrente e o valor de years for 1, a data resultante será o 29.º dia de Cheshvan no ano seguinte.

A parte do mês DateTime resultante é afetada se o mês resultante não for válido no ano seguinte. É alterado para o último mês válido do ano resultante. Por exemplo, se o mês no time parâmetro for o 13.º mês de um ano bissexto e o valor de years for 1, o mês no resultado DateTime é o 12.º mês do ano seguinte, que não é um ano bissexto. Note que, mesmo quando a parte do mês não muda, pode ainda referir-se a um mês diferente. Por exemplo, Adar Beit é o 7.º mês em anos bissextos, mas Nissan é o 7.º mês em anos comuns.

Esta implementação suporta apenas a era atual. Portanto, ArgumentException é lançado se o ano resultante estiver fora da era do especificado DateTime.

A parte do dia do resultado DateTime mantém-se igual à especificada DateTime.

Se years for negativo, o resultado DateTime é anterior ao especificado DateTime.

A Kind propriedade do valor devolvido DateTime é DateTimeKind.Unspecifiedsempre igual a . Pode preservar a Kind propriedade do time parâmetro chamando o DateTime.SpecifyKind método, como mostra o exemplo seguinte.

returnTime = DateTime.SpecifyKind(cal.AddYears(time, years), time.Kind);
returnTime = DateTime.SpecifyKind(cal.AddYears(time, years), time.Kind)

Aplica-se a

Ver também