Java Locale – Create and Set Default Locale

You will need to work with java locale api when you want to display numbers, dates, and time in a user-friendly way that conforms to the language and cultural expectations of your customers. In java, java.util.Locale class represents a specific language and region of the world.

If a class varies its behavior according to Locale, it is said to be locale-sensitive. For example, the NumberFormat and DateFormat classes are locale-sensitive; the format of the number and date, it returns depends on the Locale.

Table of Contents

Create Locale Instance
Set Default Locale

Create Locale Instance

You can create java locale instances in following ways:

  1. Static Locale objects

    This one is easiest and uses predefined constants in Locale class.

    Locale usLocale = Locale.US;
    
    long number = 123456789L;
    NumberFormat nf = NumberFormat.getInstance(usLocale);
    System.out.println( nf.format(number) );     //123,456,789
    
    Date now = new Date();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, usLocale);
    System.out.println( df.format(now) );    //July 19, 2016 12:43:12 PM IST
    

    Please note that when locale is build this way then the region portion of the Locale is undefined. So below both statements are essentially equal:

    //Region is missing in both cases
    Locale usLocale = Locale.US;       //1
    Locale usLocale = new Locale.Builder().setLanguage("en").build();        //2
    
  2. Locale constructor

    There are three constructors available in the Locale class:

    • Locale(String language)
    • Locale(String language, String country)
    • Locale(String language, String country, String variant)
    Locale usLocale = new Locale("en");
    //Locale usLocale = new Locale("en", "US");
    
    long number = 123456789L;
    NumberFormat nf = NumberFormat.getInstance(usLocale);
    System.out.println( nf.format(number) );    //123,456,789
    
    Date now = new Date();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, usLocale);
    System.out.println( df.format(now) );    //July 19, 2016 12:43:12 PM IST
    
  3. Locale.Builder class

    The Locale.Builder utility class can be used to construct a Locale object that conforms to the IETF BCP (Best Common Practices) 47 syntax.

    Locale usLocale = new Locale.Builder().setLanguage("en").setRegion("US").build();
    
    long number = 123456789L;
    NumberFormat nf = NumberFormat.getInstance(usLocale);
    System.out.println( nf.format(number) );    //123,456,789
    
    Date now = new Date();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, usLocale);
    System.out.println( df.format(now) );    //July 19, 2016 12:43:12 PM IST
    

    This method will return java.util.IllformedLocaleException error if its argument is not a well-formed element of the BCP 47 standard.

  4. Locale.forLanguageTag() method

    If you have a language tag string that conforms to the IETF BCP 47 standard, you can use the forLanguageTag(String) factory method.

    Locale usLocale = Locale.forLanguageTag("en-US");
    
    long number = 123456789L;
    NumberFormat nf = NumberFormat.getInstance(usLocale);
    System.out.println( nf.format(number) );    //123,456,789
    
    Date now = new Date();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, usLocale);
    System.out.println( df.format(now) );    //July 19, 2016 12:43:12 PM IST
    

Set Default Locale

Though it’s possible to set Locale in all locale sensitive classes in runtime, but if we can set the default locale for each user request in start (or locale specific to application) then we do not need to set locale for each locale-sensitive object in application code and thus we can avoid many lines of code – and so few defects as well.

Use the Locale.setDefault() method to set a Locale instance that all locale-sensitive classes will use by default.

Locale.setDefault(Locale.FRANCE);

The Locale class also allows you to set the default Locale for two different categories.

Locale Categories

Two locale categories are represented by the Locale.Category enumeration:

  1. Locale.Category.DISPLAY – for application’s user interface e.g. resource bundle messages.
  2. Locale.Category.FORMAT – for date and number formatting depending on specific region information
Locale.setDefault(Locale.Category.DISPLAY, Locale.US);
Locale.setDefault(Locale.Category.FORMAT, Locale.FR);

Let’s see an example.

//Set them at application startup or where request cycle begin
Locale.setDefault(Locale.Category.DISPLAY, Locale.US);
Locale.setDefault(Locale.Category.FORMAT, Locale.FRANCE);

ResourceBundle resourceBundle =  ResourceBundle.getBundle("bundleName", Locale.getDefault(Locale.Category.DISPLAY));
String message = resourceBundle.getString("message.key");

Date now = new Date();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
String date = df.format(now);

Drop me your questions in comments section.

Happy Learning !!

Resources:

ISO Country Codes
ISO Language Codes

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

4 thoughts on “Java Locale – Create and Set Default Locale”

  1. I am not able to get japan string properly below is my code and output. This code working fine in case of English but in case of Japanese is it giving question mark….

    public static void main (String args[])

    {

    Locale locale =new Locale(“ja”, “JP”);
    ResourceBundle rb = ResourceBundle.getBundle(“messages”, locale);
    System.out.println(rb.getObject(“help”));

    }

    output is given below
    ?????????????

    Reply
  2. when an application got request for any specific locale(eg french,japan) then content should be in corresponding locale if that locale is not available it should be in English how could u please help.

    Reply
  3. HI,

    we have an application which creates CSV file for end-user with a comma(,) separated columns. Now the problem with this csv is that if the user belongs different location like France etc. then the file columns get clubs to a single row.

    How should I make the csv independent of locale or delimeter of the different users?

    thanks in advance.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.