Language

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

DateTimeを追加するyears。

years
Int32

追加する年数。

戻り値

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

例外

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

例

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

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よりも前になります。

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

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

適用対象

関連項目