Small question on an issue reported by SonarQube please.
On a very simple piece of code:
Instant.now().atZone(ZoneId.systemDefault()).toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"))
I got flagged with something very strange to me: i18n-java:V1009
Unicode ICU and not java.time.DateTimeFormatter should be used for international dates and times
Java's DateTimeFormatter is quite error prone prior to Java version 9. Version 9 and greater with Unicode CLDR data is much improved, but still has errors for key locales. For ideal results, use Unicode ICU DateTimePatternGenerator.
Noncompliant Code Example
Locale userPreferredLocale = Locale.forLanguageTag("zh-Hans");
...
DateTimeFormatter mediumFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(userPreferredLocale);
Compliant Solution
import com.ibm.icu.text.DateTimePatternGenerator;
import com.ibm.icu.text.SimpleDateFormat;
import com.ibm.icu.util.ULocale;
...
ULocale userPreferredLocale = ULocale.forLanguageTag("zh-Hans");
...
DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance(userPreferredLocale);
//pattern for com.ibm.icu.text.DateFormat.FULL="MMM DD,YYYY,h:mm A"
SimpleDateFormat sdf = new SimpleDateFormat(dtpg.getBestPattern("MMM dd, YYYY, h:mm A"), uloc);
I am using Java 11, and would like to avoid importing this IBM package. I am quite unsure what is the current problem (code working fine so far).
But I am indeed quite interested in solving this. Is there a “good way” to achieve it please?
I will understand comments like: “Disable SonarQube, disable this rule, don’t pay attention too much at those false positive, don't put too much weight on analysis tools etc”
But I am quite interested how to solve this "Unicode ICU and not java.time.DateTimeFormatter should be used for international dates and times" please.
Thank you