Issue
: Using SimpleDateFormat directly without an explicit locale Id
: SimpleDateFormat
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Why is the "To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates" error coming on this line.
http://developer.android.com/reference/java/text/SimpleDateFormat.html
Locale loc = new Locale("en", "US"); DateFormat dateFormat = DateFormat. getDateInstance(DateFormat. DEFAULT, loc); As shown in the example above, the getDateInstance method of DateFormat needs two input parameters, the first parameter specifies the DateFormat to use and the second parameter is the locale.
To format a date for the current Locale, use one of the static factory methods: myString = DateFormat. getDateInstance(). format(myDate);
If not specified otherwise, the time zone associated with a new SimpleDateFormat is the default one, corresponding to the time zone reported by the operating system. Consider the following code: SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); Date date = sdf.
To remove the warning just add Locale.getDefault() as the second argument while instantiating the date format object. Eg.
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", java.util.Locale.getDefault());
Careful about getDefault
though, as it might not be appropriate for all use-cases, especially machine-readable output. From the docs:
The default locale is not appropriate for machine-readable output. The best choice there is usually Locale.US – this locale is guaranteed to be available on all devices, and the fact that it has no surprising special cases and is frequently used (especially for computer-computer communication) means that it tends to be the most efficient choice too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With