Calendar cal = Calendar.getInstance(); // create Calendar object cal.roll(Calendar.MONTH, 1); // increment month by 1 System.out.println(cal.getTime()); // output updated date
Calendar cal = Calendar.getInstance(); // create Calendar object cal.roll(Calendar.YEAR, -2); // decrement year by 2 System.out.println(cal.getTime()); // output updated dateThis code decrements the current year by 2 and outputs the new date. The Calendar class is located in the java.util package.