Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use unsupported Locale in Java

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

like image 837
ss_Dev Avatar asked Feb 16 '17 07:02

ss_Dev


People also ask

How do I get the locale in Java?

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.

How do I change the locale of a date in Java?

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.

What is locale root in Java?

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.


2 Answers

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
like image 200
Robby Cornelissen Avatar answered Oct 14 '22 10:10

Robby Cornelissen


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.

like image 34
Basil Bourque Avatar answered Oct 14 '22 11:10

Basil Bourque