Java ZoneOffset getDisplayName()方法及示例

Java ZoneOffset getDisplayName()方法及示例

ZoneOffset 类的 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)
    {
  
        // Get the ZoneOffset instance
        ZoneOffset zoneOffset
            = ZoneOffset.of("+05:30");
  
        // get Zone id in style TextStyle.SHORT and
        // Locale = Locale.ENGLISH
        String response
            = zoneOffset.getDisplayName(TextStyle.SHORT,
                                        Locale.ENGLISH);
  
        // print result
        System.out.println("Display Name: "
                           + response);
    }
}

输出。

Display Name: +05:30

程序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)
    {
  
        // Get the ZoneOffset instance
        ZoneOffset zoneOffset
            = ZoneOffset.of("+05:30");
  
        // get Zone id in style TextStyle.FULL and
        // Locale = Locale.FRENCH
        String response = zoneOffset.getDisplayName(TextStyle.FULL,
                                                    Locale.FRENCH);
  
        // print result
        System.out.println("Display Name: "
                           + response);
    }
}

输出。

Display Name: +05:30

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程