Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get yesterday date from android

Tags:

date

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

like image 237
user3747092 Avatar asked Jun 17 '14 15:06

user3747092


People also ask

How do I find yesterday's date?

How do you get yesterdays' date using JavaScript? We use the setDate() method on yesterday , passing as parameter the current day minus one. Even if it's day 1 of the month, JavaScript is logical enough and it will point to the last day of the previous month.


3 Answers

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 !

like image 172
Leonardo Avatar answered Nov 08 '22 10:11

Leonardo


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());
}
like image 22
bebeto123 Avatar answered Nov 08 '22 08:11

bebeto123


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));
like image 42
Zohab Ali Avatar answered Nov 08 '22 09:11

Zohab Ali