Java TimeZone getDisplayName(Locale locale)方法及示例


发布日期 : 2024-09-04 03:51:06 UTC

访问量: 10 次浏览

Java TimeZone getDisplayName(Locale locale) 方法及示例

Java 中 TimeZone 类的 getDisplayName(Locale locale) 方法用于获得该 TimeZone 的一个特定名称,该名称在用户传递的指定 Locale 中容易被用户理解。该名称适合于演示和显示的目的。

语法

public final String getDisplayName(Locale locale)

参数

该方法需要一个 Locale 对象类型的参数 locale,并且指的是要提供显示名称的 Locale。

返回值

该方法返回 TimeZone 在指定 Locale 中的显示名称

下面的程序说明了 TimeZone 的 getDisplayName() 方法的工作原理:

示例 1

import java.util.*;

public class TimeZone_Demo {
    public static void main(String args[])
    {
        // Creating a time zone object
        TimeZone timezone = TimeZone.getTimeZone("Asia/India");

        // Creating the locale
        Locale locale = new Locale("ENGLISH", "USA");

        // Getting a display name for specified locale
        String display_name = timezone.getDisplayName(locale);

        // Display name
        System.out.println("The Display name"
                           + " for the locale is: "
                           + display_name);
    }
}

输出:

The Display name for the locale is: Greenwich Mean Time

示例 2

import java.util.*;

public class TimeZone_Demo {
    public static void main(String args[])
    {
        // Creating a time zone object
        TimeZone timezone = TimeZone.getTimeZone("Australia/Sydney");

        // Creating the locale
        Locale locale = new Locale("ENGLISH", "Australia");

        // Getting a display name for specified locale
        String display_name = timezone.getDisplayName(locale);

        // Display name
        System.out.println("The Display name"
                           + " for the locale is: "
                           + display_name);
    }
}

输出:

The Display name for the locale is: Australian Eastern Standard Time (New South Wales)