如何获取 nl_NL 语言环境的 "mmm dd" 格式的日期?

How to get date in "mmm dd" format for nl_NL locale?

我想在 Java 中获取 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 格式!

有人可以帮我解决这个问题吗?

切勿使用 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[America/Los_Angeles]

vrijdag 17 juli 2020 om 09:30:58 Pacific-zomertijd

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

I am not getting it in correct nl_NL format

那是因为new Locale("nl_NL")是错误的。它必须是 new Locale("nl", "NL") with separate language and country parameters, or 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(...)