SimpleDateFormat (String pattern, Locale locale), e.g. Locale.US for ASCII dates

Issue : Using SimpleDateFormat directly without explicit Id : SimpleDateFormat

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

Why " To get local formatting, use getDateInstance (), getDateTimeInstance () or getTimeInstance () or use the new SimpleDateFormat (String template, Locale locale), for example Locale.US for ASCII dates, a strong>" error on this line.

http://developer.android.com/reference/java/text/SimpleDateFormat.html

+42
android
Jul 23 '13 at 10:37
source share
3 answers

To remove the warning, simply add Locale.getDefault () as the second argument when instantiating the date format object. For example.

  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.getDefault()); 
+102
Jul 23 '13 at 10:49 on
source share
— -

Beware of getDefault , although this may not be suitable for all use cases, especially for machine reading. From docs :

The default locale is not suitable for machine-readable output. The best choice there, as a rule, is Locale.US - this locale is guaranteed to be available on all devices, and the fact that it does not have unexpected special cases and is often used (especially for computer-computer communication) means that it is usually , the most effective choice.

+10
Jun 30 '15 at 7:25
source share

This is a dumb warning. If you look at the source code of the SimpleDateFormat constructor, it will get the default locale.

 public SimpleDateFormat(String pattern) { this(pattern, Locale.getDefault()); } 

Therefore, adding it to your code is redundant and overly detailed. Locale.getDefault () is almost always what you want, as that is what the user device is configured for. If for some reason you need to always return, for example, "Monday", no matter what user language is set, than you can specify Locale.US, but this seems like a rare situation.

The best thing to do is disable dumb inspection.

+7
Nov 12 '15 at 23:48
source share



All Articles