tags:

views:

37

answers:

1

Hi. Is there any way to make DateFormat format a date with a full year (eg. 12/12/2010), when using DateFormat.SHORT as the pattern? I have to format dates in both en_US and da_DK.

I know I could use DateFormat.MEDIUM, but the date have to be formatted using only numbers and separators, and DateFormat.MEDIUM for en_US produces something like 'Dec 12, 2010'.

A: 

If both formats are the same, then simply use:

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

If they differ:

private static Map<Locale, String> formats = new HashMap<Locale, String>();

static {
    formats.put(new Locale("en_US"), "dd/MM/yyyy");
    formats.put(new Locale("da_DK"), "dd.MM.yyyy");
}

And then instead of using DateFormat.getDateInstance(..) use

new SimpleDateFormat(formats.get(locale)).format(..);
Bozho
They differ in the separators, and the month and date parts are switched. But I like this solution! :)
ManiSto