JapaneseCalendar.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
abstract member AddYears : DateTime * int -> DateTime
override this.AddYears : DateTime * int -> DateTime
Public Overrides Function AddYears (time As DateTime, years As Integer) As DateTime

매개 변수

time
DateTime

DateTime 연도를 추가할 수 있습니다.

years
Int32

추가할 연도 수입니다.

반품

DateTime 지정된 연도 수에 지정된 연도를 추가한 DateTime결과입니다.

예외

결과는 DateTime 지원되는 범위를 벗어났습니다.

time 가 지원되는 형식 범위를 벗어났습니다 JapaneseCalendar .

-또는-

years 가 -10,000보다 작거나 10,000보다 큰 경우

예제

다음 코드 예제에서는 일본 달력을 기준으로 여러 DateTime 구성 요소의 값을 표시합니다.

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 연도의 결과 월에서 유효한 날짜가 아닌 경우 결과 날짜의 일 부분이 영향을 받습니다. 결과 연도의 결과 월에서 마지막 유효한 날짜로 변경됩니다. 결과 DateTime 월 부분은 지정된 월 부분과 동일하게 유지됩니다 DateTime. 결과 연도가 지정된 연대를 벗어나면 결과 DateTime 연도의 연대 부분이 영향을 받습니다 DateTime. 결과 DateTime 에서 하루 중 시간 부분은 지정된 부분과 동일하게 유지됩니다 DateTime.

예를 들어 Nigatsu(2월)에는 29일이 있는 윤년을 제외하고 28일이 있습니다. 지정된 날짜가 윤년의 니가쓰 29일(2월)이고 값 years 이 1이면 결과 날짜는 다음 해에 니가쓰의 28일(2월)이 됩니다.

음수이면 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)

적용 대상

추가 정보