Java Formatter locale() 方法



描述

Java Formatter locale() 方法返回此格式化程式構造時設定的區域設定。 此物件(具有區域設定引數)的 format 方法不會更改此值。

宣告

以下是 java.util.Formatter.locale() 方法的宣告

public Locale locale()

引數

返回值

如果未應用任何本地化,則此方法返回 null,否則返回一個區域設定。

異常

FormatterClosedException − 如果此格式化程式已透過呼叫其 close() 方法關閉

獲取美國區域設定的 Formatter 物件的區域設定示例

以下示例演示了 Java Formatter locale() 方法的用法,用於列印格式化程式的區域設定。我們使用 StringBuffer 和區域設定建立了一個格式化程式物件。Formatter 用於使用 format() 方法列印字串。然後列印其區域設定。我們在這裡使用 Locale.US 作為區域設定。

package com.tutorialspoint;

import java.util.Formatter;
import java.util.Locale;

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

      // create a new formatter
      StringBuffer buffer = new StringBuffer();
      Formatter formatter = new Formatter(buffer, Locale.US);

      // format a new string
      String name = "World";
      formatter.format("Hello %s !", name);

      // print the formatted string with default locale
      System.out.println("" + formatter);

      // print locale
      System.out.println("" + formatter.locale());
	  
      formatter.close();
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

Hello World !
en_US

獲取加拿大區域設定的 Formatter 物件的區域設定示例

以下示例演示了 Java Formatter locale() 方法的用法,用於列印格式化程式的區域設定。我們使用 StringBuffer 和區域設定建立了一個格式化程式物件。Formatter 用於使用 format() 方法列印字串。然後列印其區域設定。我們在這裡使用 Locale.CANADA 作為區域設定。

package com.tutorialspoint;

import java.util.Formatter;
import java.util.Locale;

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

      // create a new formatter
      StringBuffer buffer = new StringBuffer();
      Formatter formatter = new Formatter(buffer, Locale.CANADA);

      // format a new string
      String name = "World";
      formatter.format("Hello %s !", name);

      // print the formatted string with default locale
      System.out.println("" + formatter);

      // print locale
      System.out.println("" + formatter.locale());
	  
      formatter.close();
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

Hello World !
en_CA

獲取法語區域設定的 Formatter 物件的區域設定示例

以下示例演示了 Java Formatter locale() 方法的用法,用於列印格式化程式的區域設定。我們使用 StringBuffer 和法語區域設定建立了一個格式化程式物件。Formatter 用於使用 format() 方法列印字串。然後列印其區域設定。我們在這裡使用 Locale.FRENCH 作為區域設定。

package com.tutorialspoint;

import java.util.Formatter;
import java.util.Locale;

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

      // create a new formatter
      StringBuffer buffer = new StringBuffer();
      Formatter formatter = new Formatter(buffer, Locale.FRENCH);

      // format a new string
      String name = "World";
      formatter.format("Hello %s !", name);

      // print the formatted string with default locale
      System.out.println("" + formatter);

      // print locale
      System.out.println("" + formatter.locale());
	  
      formatter.close();
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

Hello World !
fr
java_util_formatter.htm
廣告