user3747092
user3747092

Reputation: 99

Get yesterday date from android

  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

Answers (3)

Zohab Ali
Zohab Ali

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

bebeto123
bebeto123

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

Leonardo
Leonardo

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

Related Questions