How to calculate 30 days ago using Calendar in Java

I want to calculate a date for 30 days from today.

public void dateSetup(){ DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd "); Calendar cal = Calendar.getInstance(); Calendar calReturn = Calendar.getInstance(); jDate_timeOfExpectedReturn1.setText(dateFormat.format(cal.getTime())); calReturn.add(Calendar.DATE, 30); jDate_timeOfLoan1.setText(dateFormat.format(calReturn.getTime())); } 

You can see that I am retrieving a date today using Calendar cal = Calendar.getInstance();

How to calculate the date 30 days before the date displayed?

Thanks for the help provided.

+7
source share
3 answers

Just use the add() method with -30 days

  calReturn.add(Calendar.DATE, -30); 
+24
source

You need to add -30 , which will be a subtraction.

 calReturn.add(Calendar.DATE, -30); 
+6
source

Use a negative number in add() as the -30 method, which will work like date+(-30) ==> date-30

+3
source

All Articles