Get the last 7 days record from Today

Daniel John :

I am writing an API , it has fromDate and toDate as a queryParameter. requirement is if no date has been passed to API then by default I need to show only 7 days record.

I was trying to do something like this.

Date currentDate = new Date();
Date lastSevenDays=currentDate-7

But It didn't worked. Any help would be appreciated.

Vinay Hegde :

No You can't simple do -7 , Because currentDate datatype is Date. Use Calendar for that. Try this :

  Date currentDate = new Date();  // Current Date 
  Calendar c = Calendar.getInstance();
  c.setTime(currentDate);
  c.add(Calendar.DATE, -7);
  Date lastSevenDays = c.getTime();   // 7 days back from currentDate

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=296394&siteId=1