How to get previous date in java

date, java

Solution

I would rewrite these answers a bit.

You can use

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

        // Get a Date object from the date string
        Date myDate = dateFormat.parse(dateString);

        // this calculation may skip a day (Standard-to-Daylight switch)...
        //oneDayBefore = new Date(myDate.getTime() - (24 * 3600000));

        // if the Date->time xform always places the time as YYYYMMDD 00:00:00
        //   this will be safer.
        oneDayBefore = new Date(myDate.getTime() - 2);

        String result = dateFormat.format(oneDayBefore);

To get the same results as those that are being computed by using Calendar.

Problem

I have a String Object in format yyyyMMdd.Is there a simple way to get a String with previous date in the same format? Thanks

Original source