I want to calculate the date 30 days back from today's date.
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()));
}
Above you can see that I'm extracting today date using Calendar cal = Calendar.getInstance();
How do I calculate the date of 30 days before the extracted date?
Thanks for any help given.
Just use add()
method with -30
days
calReturn.add(Calendar.DATE, -30);
You need to add -30
which will be subtraction.
calReturn.add(Calendar.DATE, -30);
Use a negative number in add()
method as -30
, which will work like date+(-30)
==> date-30
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With