How to find yesterday's date on Java?

For example, today is 12/16/2012. And you need to calculate 15/12/2012.

    2 answers 2

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

      You need a standard java.util.Calendar class that can do all sorts of date / time conversions. For your case, the add method will suffice. But remember that date conversions are not always such a trivial task and can sometimes give very strange results. For example, sometimes adding an hour to the current time, you can get the same time (and then it turns out that it was autumn and the clock was transferred an hour ago).