I am using
DateFormat dateFormat =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = new Date();
String fromdate = dateFormat.format(date);
to get the current date, how can I get the date 7 days back. for example, if today is 7th June 2013 how can I get 31th May 2013 i the same format as defined in date formater.
Got the solution.. Thanks everyone :)
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date date = new Date();
String todate = dateFormat.format(date);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -7);
Date todate1 = cal.getTime();
String fromdate = dateFormat.format(todate1);
解决方案
You can use Calendar class :
Calendar cal = Calendar.getInstance()
cal.add(Calendar.DATE, -7);
System.out.println("Date = "+ cal.getTime());
But as @Sean Patrick Floyd mentioned , Joda-time is the best Java library for Date.