importjava.util.*;publicclassMain{publicstaticvoidmain(String[] args)throwsException{Date d1 =newDate();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 MonJun2202:44:36 IST 2009
date after a month will be ThuOct2202:44:36 IST 2009
date after 7 hrs will be ThuOct2200:44:36 IST 2009
以下是滚动月份的另一个示例。
importjava.util.Calendar;publicclassCalendarExample{publicstaticvoidmain(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:FriNov1107:01:31 UTC 2016Time rolling down the year:WedNov1107:01:31 UTC 2015Time rolling up the hour:WedNov1108:01:31 UTC 2015