Pra*_*eek 2 java datetime java-6
I want names of all days in a week, but i have to create Enum(can be achieved by other ways too).
另一种方法是从日历类中获取常量并将其手动存储在Collection中.
但是,Java Date-Time API中是否有任何方法可以Collection of all Days name在一周或Collection of all months name一年内获得.
是的你可以使用DateFormatSymbols:
DateFormatSymbols symbols = new DateFormatSymbols(new Locale("en"));
String[] days = symbols.getWeekdays();
String[] months = symbols.getMonths();
Run Code Online (Sandbox Code Playgroud)
还有短名称的方法(因为en它Sat等),当然你可以选择你的语言环境.适用于Java 1.6+.
@Edit:至于为什么这会返回8天的值,如果你检查Calendar它们的类,他们开始计算从1到7的天数,所以我想DateFormatSymbols不想撇去减去一个,他们只是去了一个大小为8的数组.
@ Edit2:在源代码中你可以看到:
* Weekday strings. For example: "Sunday", "Monday", etc. An array
* of 8 strings, indexed by <code>Calendar.SUNDAY</code>,
* <code>Calendar.MONDAY</code>, etc.
* The element <code>weekdays[0]</code> is ignored.
Run Code Online (Sandbox Code Playgroud)
因此,正如我所说他们只是使用Calendar值作为索引,这就是全部.