I often create SimpleDateFormat with a patterns like HH:mm:ss
or yyyy-MM-dd
to output dates in a locale independant way. Since there is a also constructor taking an additional locale parameter I'm wondering if there are cases where such a format can be locale dependant, or if I should always specify Locale.ENGLISH or Locale.GERMANY. Lets assume that the timezone is set explicitly.

- 33,639
- 11
- 75
- 118
3 Answers
Just found the getAvailableLocales
static method on Locale, and it turns out that all the fields of a calendar can be locale dependent:
public static void main(String[] args) {
String pattern = "yyyy-MM-dd HH:mm:ss";
Date date = new Date();
String defaultFmt = new SimpleDateFormat(pattern).format(date);
for (Locale locale : Locale.getAvailableLocales()) {
String localeFmt = new SimpleDateFormat(pattern, locale).format(date);
if (!localeFmt.equals(defaultFmt)) {
System.out.println(locale + " " + localeFmt);
}
}
}
On my system (in germany running an english version of ubuntu) this outputs the following list, lets hope the unicode character come through intact:
ja_JP_JP 23-03-03 16:53:09
hi_IN २०११-०३-०३ १६:५३:०९
th_TH 2554-03-03 16:53:09
th_TH_TH ๒๕๕๔-๐๓-๐๓ ๑๖:๕๓:๐๙
So Japan and Thailand use a different epoch but are otherwise based on the gregorian calendar, which explains why month and day are the same.
Other locales also use different scripts for writing numbers, for example Hindi spoken in India and a variant of Thai in Thailand.
To answer the question, the locale should alway be specified to a known value when a locale independant String is needed.
Edit: Java 1.6 added a constant Locale.ROOT to specify a language/country neutral locale. This would be preferred to specifying the English locale for output targeted at a computer.
The root locale is the locale whose language, country, and variant are empty ("") strings. This is regarded as the base locale of all locales, and is used as the language/country neutral locale for the locale sensitive operations.

- 180
- 1
- 11

- 33,639
- 11
- 75
- 118
Yes, SimpleDateFormat
is absolutely locale-sensitive. Certain fields, like hours and minutes, are locale-independent.
SimpleDateFormat
also supports localized date and time pattern strings. In these strings, the pattern letters described above may be replaced with other, locale dependent, pattern letters.SimpleDateFormat
does not deal with the localization of text other than the pattern letters; that's up to the client of the class.
Or, you can use the localization-friendly DateFormat#getDateInstance()
factory method instead, since:
public SimpleDateFormat(String pattern, Locale locale)
Constructs a
SimpleDateFormat
using the given pattern and the default date format symbols for the given locale. Note: This constructor may not support all locales. For full coverage, use the factory methods in theDateFormat
class.

- 354,903
- 100
- 647
- 710
-
I'm asking about patterns which don't contain any month or day names, for example `yyyy-MM-dd`, and I want a locale independant output, possibly saving some keystrokes by omitting the locale parameter. – Jörn Horstmann Mar 02 '11 at 23:21
-
@Jörn: ah! In that specific example, you should have no problems omitting the extra parameter. Time zone formatting might be a different story, though. Still, you may be better off letting Java worry about the locale-specific formats (for example, deciding when to use `mm/dd/yyyy` vs `dd/mm/yyyy`). – Matt Ball Mar 02 '11 at 23:23
-
1I've taken the habit of always specifying a locale (at least when the application isn't a client application where the system locale is always used), to make sure I (or someone else) don't forget to specify one if the pattern had to change. A bit like the rule to always use curly brackets around blocks, even if it only has one line. – JB Nizet Mar 02 '11 at 23:32
-
@JB Nizet, good guideline. That would also make me think about whether the date is for display purposes or for consumption by a computer. – Jörn Horstmann Mar 02 '11 at 23:40
This works for me, maybe you should try it out:
String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
Console output:
11:52:45