Java ZoneId getDisplayName()方法及示例
ZoneId 类的 getDisplayName() 方法用于获取适合向用户展示的区域的文本表示,如 “英国时间 “或 “+02:00″。如果没有找到文本映射,则返回完整的ID。
语法
public String getDisplayName(TextStyle style, Locale locale)
参数: 该方法接受两个参数style和 locale ,其中style代表需要的文本长度,locale代表要使用的locale。
返回值: 该方法返回该区的文本值。
下面的程序说明了getDisplayName()方法。
程序1 :
// Java program to demonstrate
// ZoneId.getDisplayName() method
import java.time.*;
import java.time.format.TextStyle;
import java.util.Locale;
public class GFG {
public static void main(String[] args)
{
// create ZoneId object
ZoneId zoneId
= ZoneId.of("Europe/Paris");
// get Zone id in style TextStyle.SHORT and
// Locale = Locale.ENGLISH
String response = zoneId.getDisplayName(TextStyle.SHORT, Locale.ENGLISH);
// print result
System.out.println("ZoneId:"
+ response);
}
}
输出。
ZoneId:CET
程序2
// Java program to demonstrate
// ZoneId.getDisplayName() method
import java.time.*;
import java.time.format.TextStyle;
import java.util.Locale;
public class GFG {
public static void main(String[] args)
{
// create ZoneId object
ZoneId zoneId
= ZoneId.of("Asia/Calcutta");
// get Zone id in style TextStyle.FULL and
// Locale = Locale.FRENCH
String response = zoneId.getDisplayName(TextStyle.FULL,
Locale.FRENCH);
// print result
System.out.println("ZoneId: "
+ response);
}
}
输出。
ZoneId: Inde
参考: https: //docs.oracle.com/javase/10/docs/api/java/time/ZoneId.html#getDisplayName(java.time.format.TextStyle, java.util.Locale)