如何获得本地化的短日常名称(Mo/Tu/We/Th ......)

fhu*_*cho 30 java calendar

我可以在Java中获得本地化的短日历名称(Mo/Tu/We/Th/Fr/Sa/Su)

Rea*_*wTo 60

最好的方法是使用java.text.DateFormatSymbols

DateFormatSymbols symbols = new DateFormatSymbols(new Locale("it"));
// for the current Locale :
//   DateFormatSymbols symbols = new DateFormatSymbols(); 
String[] dayNames = symbols.getShortWeekdays();
for (String s : dayNames) { 
   System.out.print(s + " ");
}
// output :  dom lun mar mer gio ven sab 
Run Code Online (Sandbox Code Playgroud)

  • 请注意,dayNames数组将包含8个元素,第一个是空字符串,因此可以使用日期编号直接索引数组,其中星期日为1,星期六为7. (17认同)
  • 仅供参考,非常麻烦的旧日期时间类,例如 [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html),[ `java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html) 和 `java.text.SimpleDateFormat` 现在是[旧版]( https://en.wikipedia.org/wiki/Legacy_system),由 [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package- Summary.html) Java 8 及更高版本中内置的类。请参阅 Oracle 的 [*教程*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html)。 (2认同)

小智 37

如果您可以使用标准缩写,只需使用Calendar类,如下所示:

myCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);
Run Code Online (Sandbox Code Playgroud)

  • 这是获得它的好方法. (2认同)

Gre*_*ase 23

使用SimpleDateFormat的示例:

Date now = new Date();
// EEE gives short day names, EEEE would be full length.
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.US); 
String asWeek = dateFormat.format(now);
Run Code Online (Sandbox Code Playgroud)

SimpleDateFormat比C风格的String.format和System.out.printf更长,我想你会发现大多数Java开发人员会更熟悉它并且在现有代码库中使用更多,所以我建议你做法.


Arv*_*ash 11

DateTimeFormatter#localizedBy

\n

Java SE 10开始,您可以使用DateTimeFormatter#localizedBy.

\n

演示:

\n
import java.time.LocalDate;\nimport java.time.ZoneId;\nimport java.time.format.DateTimeFormatter;\nimport java.util.Locale;\n\npublic class Main {\n\n    public static void main(String args[]) {\n        DateTimeFormatter dtfHindi = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("hi"));\n        DateTimeFormatter dtfBangla = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("bn"));\n        DateTimeFormatter dtfGerman = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("de"));\n        DateTimeFormatter dtfEnglish = DateTimeFormatter.ofPattern("E").localizedBy(Locale.forLanguageTag("en"));\n\n        // Replace ZoneId.systemDefault() with the applicable timezone e.g.\n        // ZoneId.of("Asia/Calcutta")\n        LocalDate today = LocalDate.now(ZoneId.systemDefault());\n\n        System.out.println(dtfHindi.format(today));\n        System.out.println(dtfBangla.format(today));\n        System.out.println(dtfGerman.format(today));\n        System.out.println(dtfEnglish.format(today));\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
\xe0\xa4\xb6\xe0\xa5\x81\xe0\xa4\x95\xe0\xa5\x8d\xe0\xa4\xb0\n\xe0\xa6\xb6\xe0\xa7\x81\xe0\xa6\x95\xe0\xa7\x8d\xe0\xa6\xb0\nFr.\nFri\n
Run Code Online (Sandbox Code Playgroud)\n

或者,从Java SE 8开始,您可以使用DayOfWeek#getDisplayName适用的Locale.

\n
import java.time.LocalDate;\nimport java.time.ZoneId;\nimport java.time.format.TextStyle;\nimport java.util.Locale;\n\npublic class Main {\n\n    public static void main(String args[]) {\n        // Replace ZoneId.systemDefault() with the applicable timezone e.g.\n        // ZoneId.of("Asia/Calcutta")\n        LocalDate today = LocalDate.now(ZoneId.systemDefault());\n\n        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("hi")));\n        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("bn")));\n        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("de")));\n        System.out.println(today.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.forLanguageTag("en")));\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
\xe0\xa4\xb6\xe0\xa5\x81\xe0\xa4\x95\xe0\xa5\x8d\xe0\xa4\xb0\n\xe0\xa6\xb6\xe0\xa7\x81\xe0\xa6\x95\xe0\xa7\x8d\xe0\xa6\xb0\nFr.\nFri\n
Run Code Online (Sandbox Code Playgroud)\n


Wit*_*ult 5

java.time

针对使用Java 8及更高版本的用户的更新。

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

Instant instant = Instant.now();

ZonedDateTime zDateTime = instant.atZone(zoneId);

DayOfWeek day = zDateTime.getDayOfWeek();
Run Code Online (Sandbox Code Playgroud)

显示输出。

System.out.println(day.getDisplayName(TextStyle.SHORT, Locale.US));
System.out.println(day.getDisplayName(TextStyle.NARROW, Locale.US));
Run Code Online (Sandbox Code Playgroud)

运行时。在IdeOne.com上实时查看类似的代码

周二

Ť