當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.util.Locale.setDefault()用法及代碼示例


描述

這個java.util.Locale.setDefault(Locale newLocale)方法為此 Java 虛擬機實例設置默認語言環境。這不會影響主機語言環境。

聲明

以下是聲明java.util.Locale.setDefault()方法

public static void setDefault(Locale newLocale)

參數

newLocale- 新的默認語言環境

返回值

此方法返回此對象的哈希碼值。

異常

  • SecurityException- 如果存在安全管理器並且其 checkPermission 方法不允許該操作。

  • NullPointerException- 如果 newLocale 為空

示例

下麵的例子展示了 java.util.Locale.setDefault() 方法的用法。

package com.tutorialspoint;

import java.util.*;

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

      // create a new locale
      Locale locale1 = new Locale("en", "US", "WIN");

      // print locale
      System.out.println("Locale:" + locale1);

      // set another default locale
      Locale.setDefault(new Locale("fr", "FRANCE", "MAC"));

      // create a new locale based on new default settings
      Locale locale2 = Locale.getDefault();

      // print the new locale
      System.out.println("Locale::" + locale2);
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果 -

Locale:en_US_WIN
Locale::fr_FRANCE_MAC

相關用法


注:本文由純淨天空篩選整理自 Java.util.Locale.setDefault() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。