Java-util-calendar-setfield1
提供:Dev Guides
Java.util.Calendar.set()メソッド
説明
- java.util.Calendar.set(int field、int value)メソッドは、指定されたカレンダー *field を指定された value に設定します。
宣言
以下は* java.util.Calendar.set()*メソッドの宣言です
public void set(int field,int value)
パラメーター
- field -変更するフィールド。
- amount -カレンダーフィールドに与えられる値。
戻り値
このメソッドは値を返しません。
例外
*ArrayIndexOutOfBoundsException* -指定されたフィールドが範囲外の場合
例
次の例は、java.util.calendar.set()メソッドの使用法を示しています。
package com.finddevguides;
import java.util.Calendar;
public class CalendarDemo {
public static void main(String[] args) {
//create a calendar
Calendar cal = Calendar.getInstance();
//print current year
System.out.println("Current year is :" + cal.get(Calendar.YEAR));
//set the year to something else
cal.set(Calendar.YEAR, 1997);
//print the result
System.out.println("Altered year is :" + cal.get(Calendar.YEAR));
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Current year is :2012
Altered year is :1997