在 Java 中,如何使用系统的默认语言环境(语言)获取星期几(Sun、Mon、...、Sat)的字符串

In Java, how to get strings of days of week (Sun, Mon, ..., Sat) with system#39;s default Locale (language)(在 Java 中,如何使用系统的默认语言环境(语言)获取星期几(Sun、Mon、...、Sat)的字符串)
本文介绍了在 Java 中,如何使用系统的默认语言环境(语言)获取星期几(Sun、Mon、...、Sat)的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

最简单的方法:

String[] namesOfDays = new String[7] {
    "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"
};

此方法不使用语言环境.因此,如果系统语言不是英文,这种方法就不能正常工作.

This method does not use Locale. Therefore, if the system's language is not English, this method does not work properly.

使用 Joda 时间,我们可以这样做:

Using Joda time, we can do like this:

String[] namesOfDays = new String[7];
LocalDate now = new LocalDate();

for (int i=0; i<7; i++) {
    /* DateTimeConstants.MONDAY = 1, TUESDAY = 2, ..., SUNDAY = 7 */
    namesOfDays[i] = now.withDayOfWeek((DateTimeConstants.SUNDAY + i - 1) % 7 + 1)
        .dayOfWeek().getAsShortText();
}

但是,此方法使用今天的日期和日历计算,这对于最终目的是无用的.另外,它有点复杂.

However, this method uses today's date and calendar calculations, which are useless for the final purpose. Also, it is a little complicated.

有没有一种简单的方法来获取像 "Sun", "Mon", ..., "Sat" 这样的字符串和系统的默认语言环境?

Is there an easy way to get Strings like "Sun", "Mon", ..., "Sat" with system's default locale?

推荐答案

如果我没有误会你

 calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US);

是您正在寻找的.这里 你可以找到文档,

is what you are looking for. Here you can find the documentation,

或者您也可以使用 getShortWeekdays()

String[] namesOfDays = DateFormatSymbols.getInstance().getShortWeekdays()

这篇关于在 Java 中,如何使用系统的默认语言环境(语言)获取星期几(Sun、Mon、...、Sat)的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)