Locale.Builder setRegion() method in Java with Examples
The setRegion(String) method of java.util.Locale.Builder class in Java is used to set this Locale.Builder to the specified region. It means that this method will set the current region of Locale.Builder instance to match the provided region and return it. If the specified region is null or empty, then the region of this LocaleBuilder is removed. A well-formed region value comprises of a 2-letter region code as defined by the ISO 3166 standard or a 3-digit UN M.49 area code.
Syntax:
public Locale.Builder setRegion(String region)
Parameter: This method accepts the region as a parameter which is the String that is to be set to this Locale.Builder instance.
Return Value: This method returns an Locale.Builder instance with the region set of this Locale.Builder to the specified region.
Exception: This method throws following Exceptions:
- IllformedLocaleException: if the specified region has any ill formed fields
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 region of Locale.Builder String region = "IN" ; System.out.println( "Setting the region: " + region); localeBuilder = localeBuilder.setRegion(region); // Displaying Locale.Builder System.out.println( "Updated LocaleBuilder: " + localeBuilder); } } |
LocaleBuilder: java.util.Locale$Builder@232204a1 Setting the region: IN 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 region of Locale.Builder String region = "asdasf@!vsd#" ; System.out.println( "Setting the region: " + region); try { localeBuilder = localeBuilder.setRegion(region); // Displaying Locale.Builder System.out.println( "Updated LocaleBuilder: " + localeBuilder); } catch (Exception e) { System.out.println(e); } } } |
LocaleBuilder: java.util.Locale$Builder@232204a1 Setting the region: asdasf@!vsd# java.util.IllformedLocaleException: Ill-formed region: asdasf@!vsd# [at index 0]
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Locale.Builder.html#setRegion-java.lang.String-
Recommended Posts:
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Map get() method in Java with Examples
- Map put() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.