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
To get equivalent information in Java, use Locale. getDefault() to get the Locale that Java is using, and use methods on the Locale object such as getCountry() , getLanguage() to get details. The information is available using ISO codes and as human readable/displayable names.
Formatting Date and Time Formatting a date including both date and time is done using a date-time instance. You create such an instance using the getDateTimeInstance() method. Here is an example: Locale locale = new Locale("da", "DK"); //Locale locale = new Locale("en", "UK"); DateFormat dateFormat = DateFormat.
The root locale is the locale whose language, country, and variant are empty ("") strings. This is regarded as the base locale of all locales, and is used as the language/country neutral locale for the locale sensitive operations.
You can add additional locales to the Java runtime by creating an extension based on the appropriate service provider interface (SPI).
For example, if you wanted to specify the short date format for Antigua and Barbuda (en_AG), you can implement the java.text.spi.DateFormatProvider
SPI as follows:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.spi.DateFormatProvider;
import java.util.Locale;
public class EnAgDateFormatProvider extends DateFormatProvider {
private static final Locale EN_AG_LOCALE = new Locale("en", "AG");
public DateFormat getDateInstance(int style, Locale locale) {
// if your extension supports multiple locales, you have to take the locale
// parameter into account as well
switch (style) {
case DateFormat.SHORT:
return new SimpleDateFormat("dd/MM/yy");
default:
// TODO implement other styles
return null;
}
}
public DateFormat getTimeInstance(int style, Locale locale) {
// TODO implement this method
return null;
}
public DateFormat getDateTimeInstance(int dateStyle, int timeStyle,
Locale locale) {
// TODO implement this method
return null;
}
public Locale[] getAvailableLocales() {
return new Locale[]{EN_AG_LOCALE};
}
}
This will need to be packaged in a JAR file, and in the META-INF/services
directory of the JAR, you will need to create a file named java.text.spi.DateFormatProvider
. That file needs to contain the fully qualified name of your provider, in my case just:
EnAgDateFormatProvider
Once you have created the JAR, you need to drop it in the extensions directory of your JRE. On my Ubuntu machine this happens to be /usr/lib/jvm/java-8-oracle/jre/lib/ext/
.
After that, the code snippet from your question:
Locale loc = new Locale("en", "AG");
DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT, loc);
System.out.println("Short format: " + df1.format(new Date()));
Will print out:
Short format: 16/02/2017
References:
You are using the troublesome old date-time classes, now legacy, supplanted by the java.time classes.
“Antigua and Barbuda - ISO3166 code - AG” is not a supported locale in Java 8. See the list of supported locales or call:
System.out.println ( "locales: " + Arrays.toString ( Locale.getAvailableLocales () ) );
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 defined Locale
that uses the same appropriate formats.
Or define an Enum
for each undefined locale with methods that return DateTimeFormatter
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 - ISO3166 code - DO” is supported in Java 8.
Locale locale = new Locale ( "es" , "DO" ); // Spanish in Dominican Republic.
LocalDate localDate = LocalDate.now ( ZoneId.of ( "Europe/Paris" ) );
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate ( FormatStyle.SHORT ).withLocale ( locale );
String output = localDate.format ( f );
System.out.println ( "localDate: " + localDate + " is " + output );
localDate: 2017-02-16 is 16/02/17
The result is as you desired, dd/MM/yy.
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.
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