0

I want to get yesterday date or date which is of not today but of the past. I am using this code -

Calendar calendarMessage = Calendar.getInstance();
        Calendar calendarToday = Calendar.getInstance();
        calendarMessage.setTime(date);
        return calendarMessage.get(Calendar.YEAR) == calendarToday.get(Calendar.YEAR) &&
                calendarMessage.get(Calendar.DAY_OF_YEAR) == calendarToday.get(Calendar.DAY_OF_YEAR);

Can you please help me in this?

Mariana
  • 31
  • 2
  • 2
  • 4

2 Answers2

3

Try on this way ..

public static String getYesterdayDate() {

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, -1);
    return dateFormat.format(cal.getTime());
}
Hardik Parmar
  • 712
  • 2
  • 13
  • 28
0

The following should work for you:

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

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