private String setDate(int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, + day);
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
String formattedDate = df.format(cal.getTime());
return formattedDate;
}
The above method returns “14/06/12” for Locale.UK and “06/14/12” for Locale.US.
How can I get it to return the year in full, i.e. “14/06/2012” for the US locale and “06/14/2012” for UK locale?
JHS’s answer is almost complete. The only thing needs to be done is to make it work for all locales.
Following Greg’s comments (thanks Greg), here is the relevant note from the javadoc:
“If you want even more control over the format or parsing, (or want to give your users more control), you can try casting the
DateFormatyou get from the factory methods to aSimpleDateFormat. This will work for the majority of countries; just remember to put it in atryblock in case you encounter an unusual one.”