📜  Java中的 Locale.Category valueOf() 方法及示例

📅  最后修改于: 2022-05-13 01:54:52.154000             🧑  作者: Mango

Java中的 Locale.Category valueOf() 方法及示例

Java中Java .util.Locale.Category枚举valueOf()方法用于获取该Locale.Category枚举类型的指定常量的值。该方法以常量名作为参数。

句法:

public static Locale.Category valueOf(String name)

参数:此方法接受一个参数,该参数名是要从 Locale.Category 枚举中检索其值的常量名。

返回类型:此方法返回具有指定名称值的此Locale.Category枚举类型的实例。

异常:此方法不抛出任何异常。

方案一:

// Java program to demonstrate
// the above method
  
import java.util.*;
import java.util.Locale.*;
  
public class LocaleCategoryDemo {
    public static void main(String[] args)
    {
  
        String name = "DISPLAY";
  
        // Getting the constant
        // of Locale.Category
        System.out.println("Locale.Category "
                           + "value of "
                           + name + ": "
                           + Locale.Category
                                 .valueOf(name));
    }
}
输出:
Locale.Category value of DISPLAY: DISPLAY

方案二:

// Java program to demonstrate
// the above method
  
import java.util.*;
import java.util.Locale.*;
  
public class LocaleCategoryDemo {
    public static void main(String[] args)
    {
  
        String name = "FORMAT";
  
        // Getting the constant
        // of Locale.Category
        System.out.println("Locale.Category "
                           + "value of "
                           + name + ": "
                           + Locale.Category
                                 .valueOf(name));
    }
}
输出:
Locale.Category value of FORMAT: FORMAT

参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/Locale.Category.html#valueOf-java.lang.String-