Create Yesterday's Date from a Date in the String Format of MM/DD/YYYY : Date « Data Type « Java Tutorial






import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class MainClass {

  public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    GregorianCalendar gc = new GregorianCalendar();
    java.util.Date d = sdf.parse("12/12/2003");
    gc.setTime(d);
    System.out.println("Input Date = " + sdf.format(d));
    int dayBefore = gc.get(Calendar.DAY_OF_YEAR);
    gc.roll(Calendar.DAY_OF_YEAR, -1);
    int dayAfter = gc.get(Calendar.DAY_OF_YEAR);
    if(dayAfter > dayBefore) {
        gc.roll(Calendar.YEAR, -1);
    }
    gc.get(Calendar.DATE);
    java.util.Date yesterday = gc.getTime();
    System.out.println("Yesterdays Date = " + sdf.format(yesterday));

  }

}
Input Date = 12/12/2003
Yesterdays Date = 12/11/2003








2.38.Date
2.38.1.The java.util.Date Class
2.38.2.Show date and time using only Date methods.
2.38.3.Obtaining a Date Object From a String
2.38.4.Convert from a java.util.Date Object to a java.sql.Date Object
2.38.5.Convert a String Date Such as 2003/01/10 into a java.util.Date Object
2.38.6.Create Yesterday's Date from a Date in the String Format of MM/DD/YYYY
2.38.7.Create a java.util.Date Object from a Year, Month, Day Format
2.38.8.Create a java.sql.Time Object from java.util.Date
2.38.9.Convert the Current Time to a java.sql.Date Object
2.38.10.Convert Date into milliseconds example
2.38.11.Create java Date from specific time example
2.38.12.Determine the Day of the Week from Today's Date
2.38.13.ISO 8601 date parsing utility.
2.38.14.Parses a string representing a date by trying a variety of different parsers.
2.38.15.Perform date validations