karthi karthi - 2 months ago 15
Java Question

How to reduce one month from current date and stored in date variable using java?

How to reduce one month from current date and want to sore in

java.util.Date
variable
im using this code but it's shows error in 2nd line

java.util.Date da = new Date();
da.add(Calendar.MONTH, -1); //error


How to store this date in
java.util.Date
variable?

Answer

Use Calendar:

Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);
Date result = cal.getTime();