DecimalStyle getAvailableLocales() Method in Java with Examples

2025年5月2日 | 阅读3分钟

java.time.format.DecimalStyle 类包含 getAvailableLocales() 方法。Java DecimalStyle 类用于检索与此 DecimalStyle 兼容的所有区域设置的列表。此方法返回可访问区域设置的集合。

语法

参数:方法不接受任何参数。

返回值:此方法的结果是与此 DecimalStyle 兼容的所有区域设置的数组

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

示例 1

代码使用 java.time.format 包中的 DecimalStyle 类来检索和显示可用的区域设置。由于 DecimalStyle 没有 getAvailableLocales() 方法,会出现编译问题。它不直接与获取可用区域设置相关;其目的可能是为了展示 DecimalStyle 如何与区域设置协同工作。更恰当的做法是,应该使用 Locale 类或任何等效类来列出可访问的区域设置。DecimalStyle 的正确用法通常是自定义数字格式化,而不是直接获取区域设置。

实施

文件名:DecimalAvailableExample1.java

输出

 
The Available Locales is given by : [, he, th_TH_#Thai, nds, tk_TM_#Latn, ti_ET, ta_SG, lv, en_NU, zh_SG_#Hans, ff_LR_#Adlm, en_JM, kkj, se_NO_#Latn, sd__#Arab, dz_BT, es_BO, mni, yi, bs_BA, dsb_DE, lb_LU_#Latn, en_LR, ar_TD, cs, el, nus_SS_#Latn, af, ff_MR_#Latn, sw_UG, smn, tk_TM, sr_ME_#Cyrl, ar_EG, dsb, os_GE_#Cyrl, yo_NG, se_NO, khq, en_PW, pl_PL, sr_CS, agq_CM_#Latn, ne_IN, ar_EH, bs_BA_#Latn, nds_DE, nb_SJ, es_US, sa, en_US_POSIX, pt_MO, zh__#Hans, lb_LU, dsb_DE_#Latn, is_IS_#Latn, so_KE, dje_NE, hy_AM_#Armn, ff_GH_#Latn, fr_PM, ar_KM, no_NO_NY, pl_PL_#Latn, agq_CM, gu_IN_#Gujr, teo, tr_TR, eu, kl_GL_#Latn, ar_MR, es_DO, ru, az, su__#Latn, fa, kl_GL, yrl_BR, en_NR, nd, kk, nds_DE_#Latn, rw_RW_#Latn, hy, en_AU, en_CY, tr_TR_#Latn, ti_ER, nus_SS, en_RW, ksb, luo, lb, hr_HR_#Latn, su, ln_CD, nnh_CM, no, dje_NE_#Latn, ar_IL, mgh, or_IN, ps_AF_#Arab, az__#Latn, ta, lag, ar_SY, ksf_CM, luo_KE_#Latn, bo, tt_RU, om_KE, en_AS, fr_VU, zh_TW, sd_IN, kln, nnh_CM_#Latn, mai, pt_MZ, fr_NE, fur_IT_#Latn, ast_ES_#Latn, bez_TZ_#Latn, ksb_TZ, my_MM_#Mymr, en_JE, uz_AF, ln_CF, en_CX, da_DK_#Latn, ak_GH_#Latn, en_AT, gl, sr__#Cyrl, yue_CN_#Hans, yrl_VE, kn_IN, ff__#Adlm, ga, qu, sc, en_TZ, be__TARASK, en_PR, mua, jv, ps, sn, km, zgh, fr_NC, gd_GB, jgo, es, fr_CM, gsw, wae_CH_#Latn, pa_IN_#Guru, ur_PK_#Arab,………..]   

示例 2

以下是如何使用提供的 Java 代码收集和显示各种区域设置的 DecimalStyle 配置信息的示例。通过使用 Locale.getAvailableLocales(),它首先收集并将所有可访问的区域设置存储在一个数组中。之后,程序调用 DecimalStyle.of(locale) 来循环遍历前八个区域设置,为每个区域设置创建一个 DecimalStyle 对象。此对象根据其区域设置表示十进制分隔符和正负号等数字符号。通过打印区域设置及其关联的 DecimalStyle,应用程序会给出区域之间数字格式差异的摘要。

实施

文件名:DecimalAvailableExample2.java

输出

 
The Locale is given by : 
The DecimalStyle is given by : DecimalStyle[0+-.]
The Locale is given by : he
The DecimalStyle is given by : DecimalStyle[0+-.]
The Locale is given by : th_TH_#Thai
The DecimalStyle is given by : DecimalStyle[0+-.]
The Locale is given by : nds
The DecimalStyle is given by : DecimalStyle[0+-.]
The Locale is given by : tk_TM_#Latn
The DecimalStyle is given by : DecimalStyle[0+-,]
The Locale is given by : ti_ET
The DecimalStyle is given by : DecimalStyle[0+-.]
The Locale is given by : ta_SG
The DecimalStyle is given by : DecimalStyle[0+-.]
The Locale is given by : lv
The DecimalStyle is given by : DecimalStyle[0+-,]