//Display the date now: Date now = calendar.getTime(); DateFormat fmt = DateFormat.getDateInstance(DateFormat.FULL, Locale.UK); String formattedDate = fmt.format(now); System.out.println(formattedDate);
//Advance the calendar one day: calendar.add(calendar.DAY_OF_MONTH, 1); Date tomorrow = calendar.getTime(); formattedDate = fmt.format(tomorrow); System.out.println(formattedDate);
//Advance the calendar 30 more days: calendar.add(calendar.DAY_OF_MONTH, 30); Date futureDay = calendar.getTime(); formattedDate = fmt.format(futureDay); System.out.println(formattedDate);
//////////////////Display the date now: Date now = calendar.getTime(); SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); Date formattedDate = sdf.parse(now); System.out.println(formattedDate);
Use format() instead of parse(). The format() method is for formatting a Date object into a String. The parse() is to parse a String into a Date object.
Another issue is that SimpleDateFormat uses the format string in a case-sensitive manner. That is, 'M' is months, 'm' is minutes. 'd' and 'y' are date and year respectively, while 'D' is something else entirely (day in year), and 'Y' isn't anything (it just gets treated as a literal 'Y'). So if MM/DD/YYYY and MM.DD.YYYY represent day, months, and years, you'll need to modify the strings to use the correct case. One way to do this is:
If you ever want to refer to minutes or day-in-year, you'll need to do something else. Of course if MM/DD/YYYY and MM.DD.YYYY are the only two options, I'd probably just prepare formatters for those two options, held in static final variables, and let the user choose between those two choices somehow. No need to create a new SimpleDateFormat each time. But the choice will probably depend on other needs, in terms of how the program is to be used.