How can I get yesterday's date without using Calendar in Java and without a timestamp just the date? [duplicate]
Asked Answered
H

6

22

I have wrote a method to get the current date in the format of yyyy-MM-dd and want to be able to also create another method for getting yesterday's date, the day before the current date. All this needs is the date and not the timestamp. I am not trying to use Calendar as well. I have set up the current date this way:

public class DateWithNoTimestamp
{
       private static final String CURRENT_DATE_FORMAT = "yyyy-MM-dd";

       public final static String getCurrentDate()
       {
               DateFormat dateFormat = new SimpleDateFormat(CURRENT_DATE_FORMAT);
               Date date = new Date();
               return dateFormat.format(date);
       } 
}

This works to get the current date, now the separate method getYesertdayDate() I'm having trouble with. How can I set it up in a similar way as I did with getCurrentDate() while subtracting one day ?

Hardily answered 17/3, 2014 at 15:12 Comment(2)
"How do I cross a river in a row boat without using oars?" Idunno, I guess you can paddle with your hands?.. In Java that would mean writing a subset of Calendar.Labarbera
Duplicate of many Questions. Search on terms such as: (a) Java date-only, and (b) Java yesterday or Java subtract days. Specifically, see this Answer by Przemek for the modern solution.Rutland
S
27

You could simply subtract 1000 * 60 * 60 * 24 milliseconds from the date and format that value:

Date yesterday = new Date(System.currentTimeMillis() - 1000L * 60L * 60L * 24L));

That's the quick-and-dirty way, and, as noted in the comments, it might break when daylight savings transitions happen (two days per year). Recommended alternatives are the Calendar API, the Joda API or the new JDK 8 time API:

LocalDate today = LocalDate.now();
LocalDate yesterday = today.minusDays(1);
Scheller answered 17/3, 2014 at 15:16 Comment(3)
Damn, beat me to it...Isia
Good point! Didn't come to my mind :)Vocalic
"That's the quick-and-dirty way" You forgot to add that it's going to break on at least two days of the year - specifically, on days when the daylight saving time is activated or deactivated.Labarbera
A
16

I found org.apache.commons.lang3.time.DateUtils is more intuitive and easy to use.

For your question

Date date = DateUtils.addDays(new Date(), -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);

will do the trick. And it works well also in JDK7 or below.

Aquiline answered 2/12, 2014 at 13:20 Comment(0)
K
16

With Java 8 and later, use java.time.LocalDate class:

LocalDate yesterday = LocalDate.now().minusDays(1L);
Kinsella answered 25/8, 2016 at 19:36 Comment(1)
I suggest always passing the optional ZoneId to now rather than rely implicitly on the JVM’s current default time zone. That default can change at any moment even during runtime. LocalDate.now( ZoneId.of( "America/Montreal" ) )Rutland
B
3

You can create a Date() object for "yesterday":

private static final String CURRENT_DATE_FORMAT = "yyyy-MM-dd";

public final static String format(Date date) {
    DateFormat dateFormat = new SimpleDateFormat(CURRENT_DATE_FORMAT);
    return dateFormat.format(date);
}

public final static String formatToday() {
    return format(new Date());
}

public final static String formatYesterday() {
    return format(new Date(new Date().getTime() - 24*3600*1000));
}
Bresee answered 17/3, 2014 at 15:18 Comment(0)
F
0

Try this:

public class DateWithNoTimestamp
{
       private static final String CURRENT_DATE_FORMAT = "yyyy-MM-dd";

       public final static String getCurrentDate()
       {
           DateFormat dateFormat = new SimpleDateFormat(CURRENT_DATE_FORMAT);
           Date date = new Date();
           date .setTime(date.getTime()-24*60*60*1000);  // Subtract 24*60*60*1000 milliseconds
           return dateFormat.format(date);
       } 
}
Fugato answered 17/3, 2014 at 15:23 Comment(1)
Thanks for this, it is on par with what I am trying to do. A machine takes a Date down to a millisecond so this makes a lot of sense. I appreciate it!Hardily
J
0

Please Try This to get the Date of Yesterday:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    long yourDateMillis = System.currentTimeMillis()- (24 * 60 * 60 * 1000);
                 Time yourDate = new Time();
         yourDate.set(yourDateMillis);

    String formattedDate = yourDate.format("%d-%m-%Y");
    txtViewYesterday.setText(formattedDate);}
Jurel answered 24/3, 2014 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.