Configuring java.text formatters for different locales

Creating a java application that supports different locales, but I would like to customize the display of DateFormat beyond what is available between FULL, LONG, MEDIUM and SHORT DateFormat. I would like to do such things as putting a character between the date and time components of DateFormat.getDateTimeFormat (), in lower case AM / PM, etc., At least for English.

can think of 3 ways to do this:

1) if locale is English, use my own format string for the new SimpleDateFormat object.

2) change the default format strings for existing locales

3) create a new locale option that sets the format strings that I want

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

also, it seems that 2 or 3 would be necessary to reduce AM / PM? (AmPmMarkers resource specification for locale date and time settings)

+5
java internationalization localization locale
Oct 21 '08 at 20:15
source share
5 answers

Why not use MessageFormat instead?

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

+1
Oct 21 '08 at 23:53
source share

Java has a class just for that, it's a ResourceBundle class. Back with the properties file, and you have everything you need, plus more.

Even without the ResourceBundle class, you can use properties files to store all SimpleDateFormat formats.

Settings formats = new Settings(); Properties SDFFormats = formats.load(propertiesFile); String SDFAmerica = SDFFormats.getProperty("FormatAmerica"); 

While writing to the properties file can be read

 FormatAmerica = MMM-dd-yyyy 
+1
Oct 23 '08 at 17:32
source share

The only thing I came across was the fact that "strftime" and "locale" say that the Italian should use colons between time fields, but Java puts complete stops between them. So I added the following code:

  // This is an incredibly ugly hack, but it 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); } 
0
Oct 21 '08 at 20:19
source share

The most satisfactory way to resolve this issue that we found out is to load the am, pm, formatString lines from the local resource package, 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 if there is a separator in DateFormatSymbols, though ... so you probably need to keep str.replace

0
Oct 22 '08 at 15:55
source share

I recommend using Joda Time to format the date. It has powerful but elegant flexibility in its formatting. You will probably find that its formatting elements do what you want to do very simply.

By the way: as soon as you go to Yoda, you will never return!

0
Oct 23 '08 at 9:19
source share



All Articles