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

定義

指定した DateTime から指定した年数が経過した後の DateTime を返します。Returns a DateTime that is the specified number of years away from the specified 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

年数を加算する対象の DateTimeThe DateTime to which to add years.

years
Int32

加算する年数。The number of years to add.

戻り値

指定した DateTime に指定した年数を加算した結果の DateTimeThe DateTime that results from adding the specified number of years to the specified DateTime.

例外

結果として得られる DateTime がサポートされている範囲外の値です。The resulting DateTime is outside the supported range.

timeJapaneseCalendar 型でサポートされている範囲外の値です。time is outside the supported range of the JapaneseCalendar type.

または-or- years が -10,000 未満であるか、または 10,000 を超えています。years is less than -10,000 or greater than 10,000.

次のコード例では、 DateTimeの複数のコンポーネントの値を、日本語の暦に基づいて表示します。The following code example displays the values of several components of a DateTime in terms of the Japanese calendar.

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 JapaneseCalendar.
   JapaneseCalendar^ myCal = gcnew JapaneseCalendar;
   
   // Displays the values of the DateTime.
   Console::WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Japanese 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 Japanese calendar:
   Era:        4
   Year:       14
   Month:      4
   DayOfYear:  93
   DayOfMonth: 3
   DayOfWeek:  Wednesday

After adding two years and ten months:
   Era:        4
   Year:       17
   Month:      2
   DayOfYear:  34
   DayOfMonth: 3
   DayOfWeek:  Thursday

*/
using System;
using System.Globalization;


public class SamplesJapaneseCalendar  {

   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 JapaneseCalendar.
      JapaneseCalendar myCal = new JapaneseCalendar();

      // Displays the values of the DateTime.
      Console.WriteLine( "April 3, 2002 of the Gregorian calendar equals the following in the Japanese 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 Japanese calendar:
   Era:        4
   Year:       14
   Month:      4
   DayOfYear:  93
   DayOfMonth: 3
   DayOfWeek:  Wednesday

After adding two years and ten months:
   Era:        4
   Year:       17
   Month:      2
   DayOfYear:  34
   DayOfMonth: 3
   DayOfWeek:  Thursday

*/
Imports System.Globalization


Public Class SamplesJapaneseCalendar   

   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 JapaneseCalendar.
      Dim myCal As New JapaneseCalendar()

      ' Displays the values of the DateTime.
      Console.WriteLine("April 3, 2002 of the Gregorian calendar equals the following in the Japanese 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 Japanese calendar:
'   Era:        4
'   Year:       14
'   Month:      4
'   DayOfYear:  93
'   DayOfMonth: 3
'   DayOfWeek:  Wednesday
'
'After adding two years and ten months:
'   Era:        4
'   Year:       17
'   Month:      2
'   DayOfYear:  34
'   DayOfMonth: 3
'   DayOfWeek:  Thursday

注釈

結果として得らDateTimeれるの日部分は、結果として得られる年の月の有効な日ではない場合に影響を受けます。The day part of the resulting DateTime is affected if the resulting day is not a valid day in the resulting month of the resulting year. 結果として得られた年の月の最後の有効日に変更されます。It is changed to the last valid day in the resulting month of the resulting year. 結果DateTimeのの月の部分は、指定したDateTimeと同じままです。The month part of the resulting DateTime remains the same as the specified DateTime. 結果のの時代 ( DateTime年号) は、結果として得られる年が、指定さDateTimeれたの時代 (年号) の外にある場合に影響を受けます。The era part of the resulting DateTime is affected if the resulting year is outside the era of the specified DateTime. 結果DateTimeのの日付と時刻の部分は、指定したDateTimeと同じままです。The time-of-day part of the resulting DateTime remains the same as the specified DateTime.

たとえば、Nigatsu (2 月) の場合、うるう年が29日ある場合を除き、28日間です。For example, Nigatsu (February) has 28 days except during leap years when it has 29 days. 指定された日付が閏年の Nigatsu (2 月) の29日で、のyears値が1の場合、結果の日付は、次の年の Nigatsu (2 月) の28日になります。If the specified date is the 29th day of Nigatsu (February) in a leap year and the value of years is 1, the resulting date will be the 28th day of Nigatsu (February) in the following year.

years負の値の場合DateTime 、結果は指定しDateTimeたよりも前になります。If years is negative, the resulting DateTime is earlier than the specified DateTime.

DateTimeKind.Unspecified戻りKind 値のプロパティは、常にとDateTime等しくなります。The Kind property of the returned DateTime value always equals DateTimeKind.Unspecified. 次の例にKind示すようにtimeDateTime.SpecifyKindメソッドを呼び出すことによって、パラメーターのプロパティを保持できます。You can preserve the Kind property of the time parameter by calling the DateTime.SpecifyKind method, as the following example shows.

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

適用対象

こちらもご覧ください