Locale.Builder

public static final class Locale.Builder extends Object

Builder is used to build instances of Locale from values configured by the setters. Unlike the Locale constructors, the Builder checks if a value configured by a setter satisfies the syntax requirements defined by the Locale class. A Locale object created by a Builder is well-formed and can be transformed to a well-formed IETF BCP 47 language tag without losing information.

Note: The Locale class does not provide any syntactic restrictions on variant, while BCP 47 requires each variant subtag to be 5 to 8 alphanumerics or a single numeric followed by 3 alphanumerics. The method setVariant throws IllformedLocaleException for a variant that does not satisfy this restriction. If it is necessary to support such a variant, use a Locale constructor. However, keep in mind that a Locale object created this way might lose the variant information when transformed to a BCP 47 language tag.

The following example shows how to create a Locale object with the Builder.

     Locale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build();
 

Builders can be reused; clear() resets all fields to their default values.

Public Constructor Summary

Locale.Builder()
Constructs an empty Builder.

Public Method Summary

Locale.Builder
addUnicodeLocaleAttribute(String attribute)
Adds a unicode locale attribute, if not already present, otherwise has no effect.
Locale
build()
Returns an instance of Locale created from the fields set on this builder.
Locale.Builder
clear()
Resets the builder to its initial, empty state.
Locale.Builder
clearExtensions()
Resets the extensions to their initial, empty state.
Locale.Builder
removeUnicodeLocaleAttribute(String attribute)
Removes a unicode locale attribute, if present, otherwise has no effect.
Locale.Builder
setExtension(char key, String value)
Sets the extension for the given key.
Locale.Builder
setLanguage(String language)
Sets the language.
Locale.Builder
setLanguageTag(String languageTag)
Resets the Builder to match the provided IETF BCP 47 language tag.
Locale.Builder
setLocale(Locale locale)
Resets the Builder to match the provided locale.
Locale.Builder
setRegion(String region)
Sets the region.
Locale.Builder
setScript(String script)
Sets the script.
Locale.Builder
setUnicodeLocaleKeyword(String key, String type)
Sets the Unicode locale keyword type for the given key.
Locale.Builder
setVariant(String variant)
Sets the variant.

Inherited Method Summary

Public Constructors

public Locale.Builder ()

Constructs an empty Builder. The default value of all fields, extensions, and private use information is the empty string.

Public Methods

public Locale.Builder addUnicodeLocaleAttribute (String attribute)

Adds a unicode locale attribute, if not already present, otherwise has no effect. The attribute must not be null and must be well-formed or an exception is thrown.

Parameters
attribute the attribute
Returns
  • This builder.
Throws
NullPointerException if attribute is null
IllformedLocaleException if attribute is ill-formed

public Locale build ()

Returns an instance of Locale created from the fields set on this builder.

This applies the conversions listed in forLanguageTag(String) when constructing a Locale. (Grandfathered tags are handled in setLanguageTag(String).)

Returns
  • A Locale.

public Locale.Builder clear ()

Resets the builder to its initial, empty state.

Returns
  • This builder.

public Locale.Builder clearExtensions ()

Resets the extensions to their initial, empty state. Language, script, region and variant are unchanged.

Returns
  • This builder.

public Locale.Builder removeUnicodeLocaleAttribute (String attribute)

Removes a unicode locale attribute, if present, otherwise has no effect. The attribute must not be null and must be well-formed or an exception is thrown.

Attribute comparision for removal is case-insensitive.