Get yesterday's date using Date [duplicate]
Asked Answered
M

8

158

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
Marelda answered 11/7, 2012 at 3:47 Comment(1)
The modern/current answer is here.Hurless
I
378

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

Isleen answered 11/7, 2012 at 3:51 Comment(6)
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.Corrupt
Use yyyy-MM-dd as formatIsleen
What would be a good Unit test to that yesterday() method ?Hodgepodge
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)Isleen
To convert Instant to Date use Date.From(instant) static method.Detoxify
How would this work if the todays date is 4/1/2022, will it return 3/31/2022?Des
A
70

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);
}
Argentiferous answered 31/8, 2014 at 23:0 Comment(5)
I wonder what happens during the fringe of a leap second...Cholecalciferol
And you can use TimeUnit.DAYS.toMillis(1) instead calculating dayNuris
and what happen if NASA uses your code to go to Mars ??Induction
I will charge them a lot of money ;)Argentiferous
TimeMillis does not work well fro calendar calculation , of course in long rangeSteatopygia
M
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

Midwest answered 11/7, 2012 at 4:2 Comment(0)
L
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();
}
Linders answered 11/7, 2012 at 4:7 Comment(0)
S
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());
  }
Spectroheliograph answered 11/7, 2012 at 4:53 Comment(0)
P
3

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

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

Puppet answered 11/7, 2012 at 3:54 Comment(0)
K
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.

Kingwood answered 11/7, 2012 at 3:55 Comment(0)
M
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())); 
Morgen answered 11/7, 2012 at 3:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.