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

karthi picture karthi · May 6, 2013 · Viewed 89.8k times · Source

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

Aleksander Blomskøld picture Aleksander Blomskøld · May 6, 2013

Use Calendar:

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