156

The following function produces today's date; how can I make it produce only yesterday's date?

private String toDate() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();    
        return dateFormat.format(date).toString();
}

This is the output:

2012-07-10

I only need yesterday's date like below. Is it possible to do this in my function?

2012-07-09
jmj
  • 237,923
  • 42
  • 401
  • 438
AKIWEB
  • 19,008
  • 67
  • 180
  • 294

8 Answers8

375

Update

There has been recent improvements in datetime API with JSR-310.

Instant now = Instant.now();
Instant yesterday = now.minus(1, ChronoUnit.DAYS);
System.out.println(now);
System.out.println(yesterday);

https://ideone.com/91M1eU

Outdated answer

You are subtracting the wrong number:

Use Calendar instead:

private Date yesterday() {
    final Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, -1);
    return cal.getTime();
}

Then, modify your method to the following:

private String getYesterdayDateString() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return dateFormat.format(yesterday());
}

See

jmj
  • 237,923
  • 42
  • 401
  • 438
  • this code return date in Wed Jul 11 04:21:43 GMT 2012 format. but i want 2012-7-11, please help me how to extract this from given output. – Jaydipsinh Jan 05 '13 at 09:19
  • 9
    Use `yyyy-MM-dd` as format – jmj Jan 06 '13 at 00:36
  • What would be a good Unit test to that `yesterday()` method ? – prime Feb 19 '18 at 07:20
  • run it through 365/366 days for current year. get previous date for each of them and see second delta between two dates. You don't need to run it for each day may be pick up selective corner dates (1st Jan, 1st Mar, 28 or 29th Feb) – jmj Feb 20 '18 at 19:15
  • To convert `Instant` to `Date` use `Date.From(instant)` static method. – Mahozad Apr 25 '21 at 16:40
  • How would this work if the todays date is 4/1/2022, will it return 3/31/2022? – JpersaudCodezit Mar 31 '22 at 19:03
68

You can do following:

private Date getMeYesterday(){
     return new Date(System.currentTimeMillis()-24*60*60*1000);
}

Note: if you want further backward date multiply number of day with 24*60*60*1000 for example:

private Date getPreviousWeekDate(){
     return new Date(System.currentTimeMillis()-7*24*60*60*1000);
}

Similarly, you can get future date by adding the value to System.currentTimeMillis(), for example:

private Date getMeTomorrow(){
     return new Date(System.currentTimeMillis()+24*60*60*1000);
}
Ankit Chaudhary
  • 747
  • 6
  • 8
12
   Calendar cal = Calendar.getInstance();
   DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
   System.out.println("Today's date is "+dateFormat.format(cal.getTime()));

   cal.add(Calendar.DATE, -1);
   System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime()));  

Use Calender Api

swapy
  • 1,616
  • 1
  • 15
  • 31
9

Try this one:

private String toDate() {
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    // Create a calendar object with today date. Calendar is in java.util pakage.
    Calendar calendar = Calendar.getInstance();

    // Move calendar to yesterday
    calendar.add(Calendar.DATE, -1);

    // Get current date of calendar which point to the yesterday now
    Date yesterday = calendar.getTime();

    return dateFormat.format(yesterday).toString();
}
Kat
  • 475
  • 1
  • 6
  • 21
Meisam
  • 435
  • 1
  • 4
  • 12
7

Try this;

   public String toDate() {
       DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
       Calendar cal = Calendar.getInstance();
       cal.add(Calendar.DATE, -1);
       return dateFormat.format(cal.getTime());
  }
Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65
3

There is no direct function to get yesterday's date.

To get yesterday's date, you need to use Calendar by subtracting -1.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
3

changed from your code :

private String toDate(long timestamp) {
    Date date = new Date (timestamp * 1000 -  24 * 60 * 60 * 1000);
   return new SimpleDateFormat("yyyy-MM-dd").format(date).toString();

}

but you do better using calendar.

Jason
  • 1,241
  • 8
  • 16
1
Calendar cal = Calendar.getInstance();
   DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
   System.out.println("Today's date is "+dateFormat.format(cal.getTime()));

   cal.add(Calendar.DATE, -1);
   System.out.println("Yesterday's date was "+dateFormat.format(cal.getTime())); 
Sree
  • 746
  • 6
  • 21