(#) Implied locale in date format !!! WARNING: Implied locale in date format This is a warning. Id : `SimpleDateFormat` Summary : Implied locale in date format Severity : Warning Category : Correctness Platform : Any Vendor : Android Open Source Project Feedback : https://issuetracker.google.com/issues/new?component=192708 Affects : Kotlin and Java files Editing : This check runs on the fly in the IDE editor See : https://developer.android.com/reference/java/text/SimpleDateFormat.html Implementation : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/DateFormatDetector.kt) Tests : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/DateFormatDetectorTest.kt) Copyright Year : 2014 Almost all callers should use `getDateInstance()`, `getDateTimeInstance()`, or `getTimeInstance()` to get a ready-made instance of SimpleDateFormat suitable for the user's locale. The main reason you'd create an instance this class directly is because you need to format/parse a specific machine-readable format, in which case you almost certainly want to explicitly ask for US to ensure that you get ASCII digits (rather than, say, Arabic digits). Therefore, you should either use the form of the SimpleDateFormat constructor where you pass in an explicit locale, such as Locale.US, or use one of the get instance methods, or suppress this error if really know what you are doing. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text src/test/pkg/LocaleTest.java:32:Warning: 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. [SimpleDateFormat] new SimpleDateFormat(); // WRONG ---------------------- src/test/pkg/LocaleTest.java:33:Warning: 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. [SimpleDateFormat] new SimpleDateFormat("yyyy-MM-dd"); // WRONG ---------------------------------- src/test/pkg/LocaleTest.java:34:Warning: 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. [SimpleDateFormat] new SimpleDateFormat("yyyy-MM-dd", DateFormatSymbols.getInstance()); // WRONG ------------------------------------------------------------------- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `src/test/pkg/LocaleTest.java`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers package test.pkg; import java.text.*; import java.util.*; public class LocaleTest { public void testStrings() { System.out.println("OK".toUpperCase(Locale.getDefault())); System.out.println("OK".toUpperCase(Locale.US)); System.out.println("OK".toUpperCase(Locale.CHINA)); System.out.println("WRONG".toUpperCase()); System.out.println("OK".toLowerCase(Locale.getDefault())); System.out.println("OK".toLowerCase(Locale.US)); System.out.println("OK".toLowerCase(Locale.CHINA)); System.out.println("WRONG".toLowerCase()); String.format(Locale.getDefault(), "OK: %f", 1.0f); String.format("OK: %x %A %c %b %B %h %n %%", 1, 2, 'c', true, false, 5); String.format("WRONG: %f", 1.0f); // Implies locale String.format("WRONG: %1$f", 1.0f); String.format("WRONG: %e", 1.0f); String.format("WRONG: %d", 1.0f); String.format("WRONG: %g", 1.0f); String.format("WRONG: %g", 1.0f); String.format("WRONG: %1$tm %1$te,%1$tY", new GregorianCalendar(2012, GregorianCalendar.AUGUST, 27)); } @android.annotation.SuppressLint("NewApi") // DateFormatSymbols requires API 9 public void testSimpleDateFormat() { new SimpleDateFormat(); // WRONG new SimpleDateFormat("yyyy-MM-dd"); // WRONG new SimpleDateFormat("yyyy-MM-dd", DateFormatSymbols.getInstance()); // WRONG new SimpleDateFormat("yyyy-MM-dd", Locale.US); // OK } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also visit the [source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/DateFormatDetectorTest.kt) for the unit tests for this check to see additional scenarios. The above example was automatically extracted from the first unit test found for this lint check, `DateFormatDetector.testSpecifyingLocale`. To report a problem with this extracted sample, visit https://issuetracker.google.com/issues/new?component=192708. (##) Suppressing You can suppress false positives using one of the following mechanisms: * Using a suppression annotation like this on the enclosing element: ```kt // Kotlin @Suppress("SimpleDateFormat") fun method() { ofPattern(...) } ``` or ```java // Java @SuppressWarnings("SimpleDateFormat") void method() { ofPattern(...); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection SimpleDateFormat problematicStatement() ``` * Using a special `lint.xml` file in the source tree which turns off the check in that folder and any sub folder. A simple file might look like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <lint> <issue id="SimpleDateFormat" severity="ignore" /> </lint> ``` Instead of `ignore` you can also change the severity here, for example from `error` to `warning`. You can find additional documentation on how to filter issues by path, regular expression and so on [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html). * In Gradle projects, using the DSL syntax to configure lint. For example, you can use something like ```gradle lintOptions { disable 'SimpleDateFormat' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore SimpleDateFormat ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).