views:

785

answers:

5

Building a java application that supports different Locales, but would like to customize the DateFormat display beyond what is available between FULL,LONG,MEDIUM, and SHORT DateFormat options. Would like to do things like place a character between the date and time components of a DateFormat.getDateTimeFormat(), lowercase the AM/PM, etc, at least for english.

can think of 3 ways to do it:

1) if locale is english, use my custom format string on a new SimpleDateFormat object.

2) modify the default format strings for existing locales

3) create a new locale variant that specifies the format strings I want

Can't figure out how to do 2 or 3 (or if it's even possible), and would rather not do 1... has anyone dealt with anything like this before?

also, seems like 2 or 3 would be necessary for lowercasing the AM/PM ? (Specifiying the AmPmMarkers resource for the locale's dateformat settings)

A: 

The only thing similar I've dealt with is the fact that "strftime" and "locale" say that Italian should use colons between the time fields, but Java puts full-stops between them. So I've added the following code:

  // This is an incredibly ugly hack, but it's based on the fact that
  // Java for some reason decided that Italy uses "." between
  // hours.minutes.seconds, even though "locale" and strftime say
  // something different.
      hmsTimeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
      if (hmsTimeFormat instanceof SimpleDateFormat)
      {
     String str = ((SimpleDateFormat)hmsTimeFormat).toPattern();
     str = str.replace('.', ':');
     hmsTimeFormat = new SimpleDateFormat(str);
      }
Paul Tomblin
A: 

Why not use a MessageFormat instead?

Use the pattern "{0,date,short} your text here {0,time,short}" to do what you want.

because sometimes it should be "{0,time,short} your text here {0,date,short}", depending on the locale.
A: 

Most satisfying way to solve this that we've figured out is to load Strings am,pm,formatString from a locale-specific resource bundle, and then:

SimpleDateFormat sdf = (SimpleDateFormat)sdf.getDateTimeInstance(DateTime.SHORT,DateTime.SHORT, locale);
if (formatString != null) {
 sdf = new SimpleDateFormat(formatString);
}
if (am!= null && pm != null) {
 DateFormatSymbols symbols = sdf.getDateFormatSymbols();
 symbols.setAmPmStrings(new String[]{am, pm});
 sdf.setDateFormatSymbols(symbols);
}

Paul: not sure there's a separator in the DateFormatSymbols, though... so you probably need to keep the str.replace

A: 

I recommend using Joda Time for your date formatting. It is has powerful yet elegant flexibility in its formatting. You'll probably find that its formatters make what you want to do extremely simple.

BTW: once you go Joda you'll never go back!

Steve McLeod
+1  A: 

Java has a Class just for this, it is the ResourceBundle Class. Back it with a properties file and you have all that you need plus more.

Even without the ResourceBundle Class you could use properties files to hold all the SimpleDateFormat formats.

Settings formats = new Settings();
Properties SDFFormats = formats.load(propertiesFile);

String SDFAmerica = SDFFormats.getProperty("FormatAmerica");

While the entry into the properties file might read

FormatAmerica = MMM-dd-yyyy
WolfmanDragon