Java Locale hasExtensions() Method



Description

The Java Locale hasExtensions() method returns true if this Locale has any extensions.

Declaration

Following is the declaration for java.util.Locale.hasExtensions() method

public boolean hasExtensions()

Parameters

NA

Return Value

This method returns true if this Locale has any extensions.

Exception

NA

Checking if Locale has Extension or Not Example

The following example shows the usage of Java Locale hasExtensions() method. We're creating two locale objects, one with with extension and another without extension. Then we'll check their status using hasExtensions() method and printed.

package com.tutorialspoint;

import java.util.Locale;
import java.util.Locale.Builder;

public class LocaleDemo {
   public static void main(String[] args) {

      // create a new locale
      Locale locale1 = new Builder().setExtension('a', "sample-ex-tension").build();
      Locale locale2 = Locale.US;

      if(locale1.hasExtensions()){
         System.out.println("Locale 1 has extensions");
      }
      if(locale2.hasExtensions()){
         System.out.println("Locale 2 has extensions");
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Locale 1 has extensions
java_util_locale.htm
Advertisements