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.add(Calendar.MONTH,1);System.out.println("date after a month will be "+ cl.getTime().toString());
cl.add(Calendar.HOUR,70);System.out.println("date after 7 hrs will be "+ cl.getTime().toString());
cl.add(Calendar.YEAR,3);System.out.println("date after 3 years will be "+ cl.getTime().toString());}}
结果
上面的代码示例将产生以下结果。
today is MonJun2202:47:02 IST 2009
date after a month will be WedJul2202:47:02 IST 2009
date after 7 hrs will be WedJul2209:47:02 IST 2009
date after 3 years will be SunJul2209:47:02 IST 2012