区域依赖是SimpleDateFormat吗? [英] How locale dependent is SimpleDateFormat?

查看:195
本文介绍了区域依赖是SimpleDateFormat吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常创建SimpleDateFormat,其格式如 HH:mm:ss yyyy-MM-dd 以当地的独立方式进行日期。由于还有一个构造函数使用了额外的locale参数,我想知道是否有这样的格式可以是区域设置依赖的情况,或者我应该总是指定Locale.ENGLISH或Locale.GERMANY。假设时区是明确设置的。

解决方案

刚刚找到 getAvailableLocales Locale中的静态方法,事实证明,一个日历的所有字段都可以取决于区域设置:

  public static void main (String [] args){
String pattern =yyyy-MM-dd HH:mm:ss;
Date date = new Date();
String defaultFmt = new SimpleDateFormat(pattern).format(date);

for(Locale locale:Locale.getAvailableLocales()){
String localeFmt = new SimpleDateFormat(pattern,locale).format(date);
if(!localeFmt.equals(defaultFmt)){
System.out.println(locale ++ localeFmt);
}
}
}

在我的系统(德国)运行ubuntu的英文版本),这将输出以下列表,让希望unicode字符完好无损:

  ja_JP_JP 23-03 -03 16:53:09 
hi_IN 2011-03-03 16:53:0 9
th_TH 2554-03-03 16:53:09
th_TH_TH 2554-03-03 16:53 :09

所以日本泰国使用不同的时期,但否则基于 gregorian calendar ,这解释了为什么月份和日期是一样的。



其他语言环境也使用不同的脚本来编写数字,例如 Hindi 说出来n印度和泰国的泰国变种。



为了回答这个问题,当需要一个locale independentant String时,应该将locale指定为一个已知的值。 p>

编辑: Java 1.6添加了一个常量 Locale.ROOT 来指定一个语言/国家中立语言环境。这将更适合为计算机上的输出指定英文区域设置。


根区域是语言,国家和变体为空()字符串。这被视为所有语言环境的基础语言环境,并被用作语言/国家中立语言环境的区域敏感操作。



I often create SimpleDateFormat with a patterns like HH:mm:ss or yyyy-MM-dd to output dates in a locale independant way. Since there is a also constructor taking an additional locale parameter I'm wondering if there are cases where such a format can be locale dependant, or if I should always specify Locale.ENGLISH or Locale.GERMANY. Lets assume that the timezone is set explicitly.

解决方案

Just found the getAvailableLocales static method on Locale, and it turns out that all the fields of a calendar can be locale dependent:

public static void main(String[] args) {
    String pattern = "yyyy-MM-dd HH:mm:ss";
    Date date = new Date();
    String defaultFmt = new SimpleDateFormat(pattern).format(date);

    for (Locale locale : Locale.getAvailableLocales()) {
        String localeFmt = new SimpleDateFormat(pattern, locale).format(date);
        if (!localeFmt.equals(defaultFmt)) {
            System.out.println(locale + " " + localeFmt);
        }
    }
}

On my system (in germany running an english version of ubuntu) this outputs the following list, lets hope the unicode character come through intact:

ja_JP_JP 23-03-03 16:53:09
hi_IN २०११-०३-०३ १६:५३:०९
th_TH 2554-03-03 16:53:09
th_TH_TH ๒๕๕๔-๐๓-๐๓ ๑๖:๕๓:๐๙

So Japan and Thailand use a different epoch but are otherwise based on the gregorian calendar, which explains why month and day are the same.

Other locales also use different scripts for writing numbers, for example Hindi spoken in India and a variant of Thai in Thailand.

To answer the question, the locale should alway be specified to a known value when a locale independant String is needed.

Edit: Java 1.6 added a constant Locale.ROOT to specify a language/country neutral locale. This would be preferred to specifying the English locale for output targeted at a computer.

The root locale is the locale whose language, country, and variant are empty ("") strings. This is regarded as the base locale of all locales, and is used as the language/country neutral locale for the locale sensitive operations.

这篇关于区域依赖是SimpleDateFormat吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆