Could anyone tell me how to increment or decrement only the month in a date?
Printable View
Could anyone tell me how to increment or decrement only the month in a date?
[QUOTE=3roses;18200]Could anyone tell me how to increment or decrement only the month in a date?[/QUOTE]
Try this
say u have object cal of type Calender.
Calendar cal= Calendar.getInstance();
cal.add(cal.MONTH,1); // to increment month by one
cal.add(cal.MONTH,-1); // to decrement month by one
Increment and decrement months can be done using the Calendar Class as:
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println("Now : " + cal.getTime());
int monthsToDecrement = -1;
cal.add(Calendar.MONTH, monthsToDecrement);
System.out.println("Date after decrement: " + cal.getTime());
}