问题
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
回答1:
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:
- https://blogs.oracle.com/naotoj/entry/about_the_locale_sensitive_services
- http://docs.oracle.com/javase/8/docs/api/java/util/spi/LocaleServiceProvider.html
- https://docs.oracle.com/javase/tutorial/ext/basics/install.html
回答2:
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:
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
“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.
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?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use ThreeTenABP….
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.
来源:https://stackoverflow.com/questions/42267331/how-to-use-unsupported-locale-in-java