WeekUtils { static String[] getWeekdayNames(Locale locale, int day, boolean useThreeLetterAbbreviation){ DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(locale); String[] dayNames = dateFormatSymbols.getShortWeekdays(); if (dayNames == null) { throw new IllegalStateException("Unable to determine weekday names from default locale"); } if (dayNames.length != 8) { throw new IllegalStateException("Expected weekday names from default locale to be of size 7 but: " + Arrays.toString(dayNames) + " with size " + dayNames.length + " was returned."); } String[] weekDayNames = new String[7]; String[] weekDaysFromSunday = {dayNames[1], dayNames[2], dayNames[3], dayNames[4], dayNames[5], dayNames[6], dayNames[7]}; for (int currentDay = day - 1, i = 0; i <= 6; i++, currentDay++) { currentDay = currentDay >= 7 ? 0 : currentDay; weekDayNames[i] = weekDaysFromSunday[currentDay]; } if (!useThreeLetterAbbreviation) { for (int i = 0; i < weekDayNames.length; i++) { weekDayNames[i] = weekDayNames[i].substring(0, 1); } } return weekDayNames; }    }
@Test public void itShouldReturnCorrectWeekDaysWhenSundayIsFirstDay(){ String[] expectedWeekDays = {"S", "M", "T", "W", "T", "F", "S"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.SUNDAY, false); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenWednesdayIsFirstDayWith3Letters(){ String[] expectedWeekDays = {"Wed", "Thu", "Fri", "Sat", "Sun", "Mon", "Tue"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.WEDNESDAY, true); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenThursdayIsFirstDayWith3Letters(){ String[] expectedWeekDays = {"Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.THURSDAY, true); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenFridayIsFirstDayWith3Letters(){ String[] expectedWeekDays = {"Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.FRIDAY, true); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenSaturdayIsFirstDayWith3Letters(){ String[] expectedWeekDays = {"Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.SATURDAY, true); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenMondayIsFirstDay(){ String[] expectedWeekDays = {"M", "T", "W", "T", "F", "S", "S"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.MONDAY, false); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenTuesdayIsFirstDay(){ String[] expectedWeekDays = {"T", "W", "T", "F", "S", "S", "M"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.TUESDAY, false); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenWednesdayIsFirstDay(){ String[] expectedWeekDays = {"W", "T", "F", "S", "S", "M", "T"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.WEDNESDAY, false); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenThursdayIsFirstDay(){ String[] expectedWeekDays = {"T", "F", "S", "S", "M", "T", "W"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.THURSDAY, false); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenFridayIsFirstDay(){ String[] expectedWeekDays = {"F", "S", "S", "M", "T", "W", "T"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.FRIDAY, false); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenSaturdayIsFirstDay(){ String[] expectedWeekDays = {"S", "S", "M", "T", "W", "T", "F"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.SATURDAY, false); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenSundayIsFirstDayWith3Letters(){ String[] expectedWeekDays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.SUNDAY, true); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenMondayIsFirstDayWith3Letters(){ String[] expectedWeekDays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.MONDAY, true); assertArrayEquals(expectedWeekDays, actualWeekDays); }
@Test public void itShouldReturnCorrectWeekDaysWhenTuesdayIsFirstDayWith3Letters(){ String[] expectedWeekDays = {"Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Mon"}; String[] actualWeekDays = WeekUtils.getWeekdayNames(Locale.ENGLISH, Calendar.TUESDAY, true); assertArrayEquals(expectedWeekDays, actualWeekDays); }