HebrewCalendar.AddYears(DateTime, Int32) メソッド

定義

指定した DateTime から指定した年数が経過した後の 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

パラメーター

time
DateTime

years を加算する対象の DateTime

years
Int32

加算する年数。

戻り値

指定した DateTime に指定した年数を加算した結果の DateTime

例外

結果として得られる DateTime がサポートされている範囲外の値です。

次のコード例では、 の複数のコンポーネントの値を DateTime ヘブライ暦の観点から表示します。

using namespace System;
using namespace System::Globalization;
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();
}

int main()
{
   
   // Sets a DateTime to April 3, 2002 of the Gregorian calendar.
   DateTime myDT = DateTime(2002,4,3,gcnew GregorianCalendar);
   
   // Creates an instance of the HebrewCalendar.
   HebrewCalendar^ myCal = gcnew 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 );
}

/*
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

*/
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

注釈

このクラスの HebrewCalendar 実装では、ヘブライ語の年 5343 年から 5999 年 (グレゴリオ暦では 1583 年から 2239 年) のみが認識されます。

結果の日が、結果 DateTime の年の結果の月の有効な日でない場合、結果の日の部分が影響を受ける。 結果の年の結果の月の最後の有効な日に変更されます。 たとえば、Cheshvan は、ユダヤ人の祝日の配置に応じて、29 日または 30 日を指定できます。 Cheshvan が現在の年に 30 日、次の年に 29 日あるとします。 指定した日付が現在の年の Cheshvan の 30 日目で、 の years 値が 1 の場合、結果の日付は次の年の Cheshvan の 29 日目になります。

結果の月が結果 DateTime の年の有効な月でない場合、結果の月の部分が影響を受ける。 結果の年の最後の有効な月に変更されます。 たとえば、 パラメーターの time 月が閏年の 13 番目の月で、 の years 値が 1 の場合、結果の DateTime 月は次の年の 12 番目の月になります。これは閏年ではありません。 月の部分が変わらない場合でも、別の月を参照している可能性があることに注意してください。 たとえば、Adar Beit は閏年の 7 か月目ですが、日産は共通年の 7 か月目です。

この実装では、現在の時代 (年号) のみがサポートされます。 したがって、 は、 ArgumentException 結果として得られる年が、指定した の時代 (年号) を外れた場合にスローされます DateTime

結果 DateTime の時刻部分は、指定した DateTimeと同じままです。

が負の場合 years 、結果 DateTime は指定した DateTimeよりも前になります。

返されるDateTime値の プロパティはKind常に とDateTimeKind.Unspecified等しくなります。 次の例に Kind 示すように、 メソッドを time 呼び出 DateTime.SpecifyKind すことで、 パラメーターの プロパティを保持できます。

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

適用対象

こちらもご覧ください