HomeLanguagesJavaLocale.Builder setLocale(Locale) method in Java with Examples

Locale.Builder setLocale(Locale) method in Java with Examples

The setLocale(Locale) method of java.util.Locale.Builder class in Java is used to reset this Locale.Builder to the specified Locale. It means that this method will reset the current state of Locale.Builder instance to match the provided Locale and return it.

Syntax:

public Locale.Builder setLocale(Locale locale)

Parameter: This method accepts the locale as the parameter which is the Locale that is to be set to this Locale.Builder instance.

Return Value: This method returns an Locale.Builder instance which is the state of this Locale.Builder set to the specified Locale.

Exception: This method throws following Exceptions:

  • IllformedLocaleException: if the specified locale has any ill formed fields
  • NullPointerException: if the specified locale is null

Program 1:




// Java program to demonstrate
// the above method
  
import java.util.*;
import java.util.Locale.*;
  
public class LocaleBuilderDemo {
    public static void main(String[] args)
    {
  
        // Creating a new Locale.Builder
        Locale.Builder localeBuilder
            = new Builder();
  
        // Displaying Locale.Builder
        System.out.println("LocaleBuilder: "
                           + localeBuilder);
  
        // setting the locale of Locale.Builder
        Locale locale = Locale.FRANCE;
        System.out.println("Setting the Locale: "
                           + locale);
  
        localeBuilder
            = localeBuilder.setLocale(locale);
  
        // Displaying Locale.Builder
        System.out.println("Updated LocaleBuilder: "
                           + localeBuilder);
    }
}


Output:

LocaleBuilder: java.util.Locale$Builder@232204a1
Setting the Locale: fr_FR
Updated LocaleBuilder: java.util.Locale$Builder@232204a1

Program 2:




// Java program to demonstrate
// the above method
  
import java.util.*;
import java.util.Locale.*;
  
public class LocaleBuilderDemo {
    public static void main(String[] args)
    {
  
        // Creating a new Locale.Builder
        Locale.Builder localeBuilder
            = new Builder();
  
        // Displaying Locale.Builder
        System.out.println("LocaleBuilder: "
                           + localeBuilder);
  
        // setting the locale of Locale.Builder
        Locale locale = Locale.ENGLISH;
        System.out.println("Setting the Locale: "
                           + locale);
  
        localeBuilder
            = localeBuilder.setLocale(locale);
  
        // Displaying Locale.Builder
        System.out.println("Updated LocaleBuilder: "
                           + localeBuilder);
    }
}


Output:

LocaleBuilder: java.util.Locale$Builder@232204a1
Setting the Locale: en
Updated LocaleBuilder: java.util.Locale$Builder@232204a1

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Locale.Builder.html#setLocale-java.util.Locale-

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32631 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7009 POSTS0 COMMENTS
Nicole Veronica
12138 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12236 POSTS0 COMMENTS
Shaida Kate Naidoo
7133 POSTS0 COMMENTS
Ted Musemwa
7402 POSTS0 COMMENTS
Thapelo Manthata
7092 POSTS0 COMMENTS
Umr Jansen
7106 POSTS0 COMMENTS