How language dependent is SimpleDateFormat?

I often create SimpleDateFormat with patterns like HH:mm:ss or yyyy-MM-dd to output dates regardless of the locale. Since there is also a constructor that accepts an additional locale parameter, I am wondering if there are cases where such a format may be locale dependent, or if I should always specify Locale.ENGLISH or Locale.GERMANY. Suppose the time zone is set explicitly.

+7
source share
3 answers

I just found the static getAvailableLocales method in Locale, and it turns out that all calendar fields 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, the English version of ubuntu is running), this displays the following list, it is hoped that the unicode character will fall into the 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 era, but otherwise based on, which explains why the month and day coincide.

Other locales also use different scripts to record numbers, such as the Hindi that are spoken in India and the Thai language variant in Thailand.

To answer the question, the locale should always be given to a known value when a String independent string is needed.

Edit: Java 1.6 added the Locale.ROOT constant to indicate the language / country of a neutral locale. It would be preferable to specify English for computer-oriented output.

A root language is a language, a language whose country and variant are empty ("") strings. This is considered as the basic localization of all locales and is used as a language neutral language standard for language operations.

+15
source

Yes, SimpleDateFormat completely language dependent. Some fields, such as hours and minutes, are locale-independent.

SimpleDateFormat also supports localized date and time strings. In these lines, the letters of the patterns described above can be replaced with other language-dependent pattern letters. SimpleDateFormat does not concern the localization of text other than the letters of the template; what up customer class.

Or you can use the localized DateFormat#getDateInstance() factory method, because:

public SimpleDateFormat(String pattern, Locale locale)

Creates a SimpleDateFormat using the specified template and default date format characters for this locale. Note. This constructor may not support all locales. For full coverage, use the factory methods in the DateFormat class.

+4
source

This works for me, maybe you should try:

String time = new SimpleDateFormat("HH:mm:ss").format(new Date());

Console output:

 11:52:45 
0
source

All Articles