JapaneseCalendar.AddYears(DateTime, Int32) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
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
パラメーター
- years
- Int32
追加する年数。
返品
指定した年数を指定したDateTimeに加算した結果のDateTime。
例外
結果として得られる DateTime は、サポートされている範囲外です。
例
次のコード例では、日本語カレンダーの観点から、 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と同じままです。
たとえば、閏年 (2 月) は、閏年を除いて 29 日間です。 指定した日付が閏年の閏年の 29 日であり、 years の値が 1 の場合、結果の日付は次の年の日月 (2 月) の 28 日になります。
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)