Reputation: 99
Date now = new Date();
String JobReceivedDate= new SimpleDateFormat("yyyy-MM-dd").format(now);
By this I can get today's date. How can I date yesterday date??? I want that to be in string and in the same format. Thanks
Upvotes: 7
Views: 20054
Reputation: 9574
I mostly use this
Date mydate = new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24));
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String yestr = dateFormat.format(mydate);
Now you can get date from "yestr"
Similarly for Tomorrow's date you can change first line like this (just change negative sign to positive)
Date mydate = new Date(System.currentTimeMillis() + (1000 * 60 * 60 * 24));
Upvotes: 3
Reputation: 230
try this:
private String getYesterdayDateString() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance()
cal.add(Calendar.DATE, -1);
return dateFormat.format(cal.getTime());
}
Upvotes: 14
Reputation: 3191
Try this:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
dateFormat.format(cal.getTime()); //your formatted date here
You will get the day before always !
Upvotes: 25