【问题标题】:How to get date in "mmm dd" format for nl_NL locale?如何以“mmm dd”格式获取 nl_NL 语言环境的日期?
【发布时间】:2020-07-17 15:18:12
【问题描述】:

我想在 jave 中获取 nl_NL 语言环境的日期

Calendar calendar = Calendar.getInstance(); 
DateFormat df = new SimpleDateFormat(Pattern, new Locale("nl_NL"));
df.setTimeZone(TimeZone.getTimeZone("PST"));
String expectedDate = df.format(calendar.getTime()).toLowerCase();
System.out.println("Date in PST Timezone : " + expectedDate);
return expectedDate;

我没有得到正确的 nl_NL 格式!

有人可以帮我吗?

【问题讨论】:

  • 这能回答你的问题吗? stackoverflow.com/questions/8770726/…
  • MonthDay.now(ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("MMM dd", Locale.forLanguageTag("nl-NL"))) 刚刚给了我jul. 20 这就是你想要的吗?

标签: java date locale


【解决方案1】:

切勿使用 2-4 个字符的伪区域,例如 PSTCSTIST。这些值不是标准化的,甚至不是唯一的!

Real time zone names 的格式为Continent/ Region,例如Africa/TunisAsia/Tokyo。对于美国西海岸大部分地区的时区,如果这就是您所说的PST,请使用America/Los_Angeles

ZoneId z = ZoneId.of( "America/Los_Angeles" ) ; 

永远不要使用糟糕的遗留日期时间类,例如Calendar。仅使用 java.time 类。

ZonedDateTime zdt = ZonedDateTime.now( z ) ;

以标准ISO 8601 格式生成文本,通过在方括号中附加时区名称来明智地扩展。

String output = zdt.toString() ;

将荷兰语和荷兰文化的文本表示本地化。

Locale locale = new Locale( "nl" , "NL" ) ;
DateTimeFormatter f = 
    DateTimeFormatter
    .ofLocalizedDateTime( FormatStyle.FULL )
    .withLocale( locale ) 
;
String output = zdt.format( f ) ;

看到这个code run live at IdeOne.com

2020-07-17T09:30:58.183568-07:00[美国/洛杉矶]

vrijdag 2020 年 7 月 17 日上午 09:30:58 Pacific-zomertijd

搜索以了解更多信息。 Stack Overflow 上已经多次解决所有这些问题。

【讨论】:

    【解决方案2】:

    我没有得到正确的 nl_NL 格式

    那是因为new Locale("nl_NL") 是错误的。它必须是 new Locale("nl", "NL") 并带有单独的 languagecountry 参数,或者 Locale.forLanguageTag("nl-NL") 带有 languageTag 参数并在值中使用破折号。它从不带下划线。

    这也是因为日期模式"mmm dd" 是错误的。它必须是"MMM dd",其中 M 大写代表,而不是小写代表分钟

    最后,使用时区PST 是错误的,因为它没有明确定义。它可以表示“皮特凯恩标准时间”或“太平洋标准时间”,甚至第二个也是错误的,因为美国太平洋海岸目前正在观察PDT(太平洋夏令时间)。正确的时区是America/Los_Angeles

    将这些修复应用于问题代码:

    Calendar calendar = Calendar.getInstance(); 
    DateFormat df = new SimpleDateFormat("MMM dd", new Locale("nl", "NL"));
    df.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
    String expectedDate = df.format(calendar.getTime()).toLowerCase();
    System.out.println("Date in PST Timezone : " + expectedDate);
    

    输出

    Date in PST Timezone : jul. 17
    

    当然,对于仅日期值,时区并不真正适用,但确实适用。


    不过,您应该使用较新的 Java 8 Time API。

    DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMM dd", new Locale("nl", "NL"));
    String date = MonthDay.now(ZoneId.of("America/Los_Angeles")).format(fmt);
    System.out.println("Date : " + date);
    

    输出

    Date : jul. 17
    

    您也可以使用LocaleDate.now(...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多