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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16541438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:22:07  来源:igfitidea点击:

In Java, how to get strings of days of week (Sun, Mon, ..., Sat) with system's default Locale (language)

javaandroidcalendarjodatimedayofweek

提问by Naetmul

The simplest way:

最简单的方法:

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.

此方法不使用 Locale。因此,如果系统的语言不是英语,则此方法无法正常工作。

Using Joda time, we can do like this:

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

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.

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

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

有没有一种简单的方法可以使用系统的默认语言环境获取诸如"Sun", "Mon", ... 之类的字符串"Sat"

采纳答案by Blackbelt

If I have not misunderstood you

如果我没有误解你

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

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

就是你要找的。在这里你可以找到文档,

Or you can also use, getShortWeekdays()

或者你也可以使用getShortWeekdays()

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

回答by Juned Ahsan

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);

You can create the date with your desired date and time. And achieve what you want.

您可以使用所需的日期和时间创建日期。并实现你想要的。

回答by Nitin Karale

Please try this

请试试这个

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


 int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);

System.out.println("Day := "+namesOfDays[day-1]);

回答by Basil Bourque

tl;dr

tl;博士

DayOfWeek.MONDAY.getDisplayName( 
    TextStyle.SHORT , 
    Locale.getDefault() 
)

java.time

时间

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses. Much of java.time is back-ported to Android (see below).

现在处于维护模式Joda-Time项目建议迁移到java.time类。java.time 的大部分内容都向后移植到 Android(见下文)。

DayOfWeek

DayOfWeek

The DayOfWeekenum defines seven objects, one for each day-of-week. The class offers several handy methods including getDisplayNameto generate a string with localized day name.

DayOfWeek枚举定义了七个对象,每天一个-的一周。该类提供了几种方便的方法,包括getDisplayName生成具有本地化日期名称的字符串。

To localize, specify:

要本地化,请指定:

  • TextStyleto determine how long or abbreviated should the string be.
  • Localeto determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.
  • TextStyle确定字符串的长度或缩写。
  • Locale确定 (a) 用于翻译日期名称、月份名称等的人类语言,以及 (b) 决定缩写、大写、标点符号、分隔符等问题的文化规范。

Example:

例子:

String output = DayOfWeek.MONDAY.getDisplayName( TextStyle.SHORT , Locale.CANADA_FRENCH );

You can use the JVM's current default time zone rather than specify one. But keep in mind the risk: The default zone can be changed at any moment during executionby any code in any thread of any app running within the JVM.

您可以使用 JVM 的当前默认时区而不是指定一个时区。但请记住风险:在 JVM 中运行的任何应用程序的任何线程中的任何代码在执行期间的任何时刻都可以更改默认区域。

Locale locale = Locale.getDefault() ;
String output = DayOfWeek.MONDAY.getDisplayName( TextStyle.SHORT , locale );


About java.time

关于 java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

Where to obtain the java.time classes?

从哪里获得 java.time 类?

回答by yincrash

Without 1.8, you can use DateFormatSymbols(which also works with Android).

如果没有 1.8,您可以使用DateFormatSymbols(也适用于 Android)。

DateFormatSymbols.getWeekdays()

DateFormatSymbols.getWeekdays()

Returns: the weekday strings. Use Calendar.SUNDAY, Calendar.MONDAY, etc. to index the result array.

返回: 工作日字符串。使用 Calendar.SUNDAY、Calendar.MONDAY 等索引结果数组。

回答by mazend

I think my code is useful. You can change it for your purpose easily.

我认为我的代码很有用。您可以根据自己的目的轻松更改它。

Result string array is "Sat", "Sun", "Mon", "Tue", "Wed", "Thu" and "Fri".

结果字符串数组是“周六”、“周日”、“周一”、“周二”、“周三”、“周四”和“周五”。

    public String[] getNameOfDays(){
        SimpleDateFormat sdf_day_of_week = new SimpleDateFormat("EEE", Locale.getDefault());
        String[] nameOfDays = new String[7];

        Calendar calendar = Calendar.getInstance();

        for(int i=0; i<7; i++) {
            calendar.set(Calendar.DAY_OF_WEEK, i);
            nameOfDays[i] = sdf_day_of_week.format(calendar.getTime());
        }

        return nameOfDays;
    }