Get yesterday date from android

15,393

Solution 1

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 !

Solution 2

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());
}

Solution 3

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));
Share:
15,393
user3747092
Author by

user3747092

Updated on September 15, 2022

Comments

  • user3747092
    user3747092 over 1 year
      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

  • Dushyant Suthar
    Dushyant Suthar about 4 years
    I don't think that's correct because System.currentTimeMillis() is universal and you don't know from the local timezone's perspective what was the date yesterday by using current millies