As part of Internationalisation, got a requirement to support few countries like
Antigua and Barbuda - ISO3166 code - AG & Dominican Republic - ISO3166 code - DO
Locale loc = new Locale("en", "AG");
DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT, loc);
System.out.println("Short format: " + df1.format(new Date()));
Java will display date in format mm/dd/yy, where as date format in those countries are dd/mm/yy.
Is there any way we can achieve the pattern dd/mm/yy? Even ICU4J libraries also not supporting those countries?
Thanks in advance
You are using the troublesome old date-time classes, now legacy, supplanted by the java.time classes.
Antigua and Barbuda
“Antigua and Barbuda - ISO3166 code - AG” is not a supported locale in Java 8. See the list of supported locales or call:
You can build your own list of explicit formatting patterns for such unsupported locales. For example,
DateTimeFormatter.ofPattern( "dd/MM/yy" )
.Better yet, define a
Map
of each undefined locale using language-country string such as "en-AG" to a similar definedLocale
that uses the same appropriate formats.Or define an
Enum
for each undefined locale with methods that returnDateTimeFormatter
objects for each length (FULL, SHORT etc.).Or consider the Answer by Comelissen, though I have not studied that well enough to recommend it yet.
Dominican Republic
“Dominican Republic - ISO3166 code - DO” is supported in Java 8.
The result is as you desired, dd/MM/yy.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.