我可以在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)
小智 37
如果您可以使用标准缩写,只需使用Calendar类,如下所示:
myCal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);
Run Code Online (Sandbox Code Playgroud)
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从Java SE 10开始,您可以使用DateTimeFormatter#localizedBy.
演示:
\nimport 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}\nRun 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\nRun Code Online (Sandbox Code Playgroud)\n或者,从Java SE 8开始,您可以使用DayOfWeek#getDisplayName适用的Locale.
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}\nRun 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\nRun Code Online (Sandbox Code Playgroud)\n
针对使用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上实时查看类似的代码。
周二
Ť
| 归档时间: |
|
| 查看次数: |
48217 次 |
| 最近记录: |