按本地化顺序列出<DayOfWeek>

List<DayOfWeek> in localized order

我们有 DayOfWeek enum defining the days of the week in standard ISO 8601 订单。

我想要一个List of those objects in the order appropriate to a Locale.

我们可以很容易地确定语言环境一周的第一天。

Locale locale = Locale.CANADA_FRENCH ;
DayOfWeek firstDayOfWeek =  WeekFields.of( locale ).getFirstDayOfWeek() ;

设置 List.

List< DayOfWeek > dows = new ArrayList<>( 7 ) ;  // Set initial capacity to 7, for the seven days of the week.
dows.add( firstDayOfWeek ) ;

➥ 要将一周中的其他六天添加到该列表中,simplest/shortest/most 优雅的方法是什么?

可以使用DayOfWeekplus方法。

The calculation rolls around the end of the week from Sunday to Monday.

IntStream and its range方法增加数字(包含开始,不包含结束。

Locale locale = Locale.CANADA_FRENCH;
DayOfWeek firstDayOfWeek = WeekFields.of(locale).getFirstDayOfWeek();

List<DayOfWeek> dows = IntStream.range(0, 7)
        .mapToObj(firstDayOfWeek::plus)
        .collect(Collectors.toList());