Javaexamples-date-roll-month
提供:Dev Guides
Javaの例-数時間と数か月のロールスルー
問題の説明
時間と月をどのように進めるか?
溶液
この例では、クラスカレンダーのroll()メソッドを使用して、月(年を変更せずに)または時間(月または年を変更せずに)をロールスルーする方法を示します。
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Date d1 = new Date();
Calendar cl = Calendar. getInstance();
cl.setTime(d1);
System.out.println("today is "+ d1.toString());
cl. roll(Calendar.MONTH, 100);
System.out.println("date after a month will be " + cl.getTime().toString() );
cl. roll(Calendar.HOUR, 70);
System.out.println("date after 7 hrs will be "+ cl.getTime().toString() );
}
}
結果
上記のコードサンプルは、次の結果を生成します。
today is Mon Jun 22 02:44:36 IST 2009
date after a month will be Thu Oct 22 02:44:36 IST 2009
date after 7 hrs will be Thu Oct 22 00:44:36 IST 2009
以下は、ロール月の別の例です。
import java.util.Calendar;
public class CalendarExample {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println("Time:" + cal.getTime());
cal.roll(Calendar.YEAR, false);
System.out.println("Time rolling down the year:" + cal.getTime());
cal.roll(Calendar.HOUR, true);
System.out.println("Time rolling up the hour:" + cal.getTime());
}
}
上記のコードサンプルは、次の結果を生成します。
Time:Fri Nov 11 07:01:31 UTC 2016
Time rolling down the year:Wed Nov 11 07:01:31 UTC 2015
Time rolling up the hour:Wed Nov 11 08:01:31 UTC 2015