DateUtils { public static LocalDate getStartDateOfWeek(String weekCode) { if (weekCode == null) { return null; } if (weekCode.matches(WEEK_CODE_PATTERN)) { int year = getYearFromWeekCode(weekCode); int week = getWeekFromWeekCode(weekCode); WeekFields weekFields = WeekFields.ISO; LocalDate firstDayOfWeek = LocalDate.ofYearDay(year, 1).with(weekFields.weekOfYear(), week) .with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); return firstDayOfWeek; } throw new OCSRuntimeException("Cannot convert"); } private  DateUtils(); @SuppressWarnings("unchecked") static T createJava8Date(Class<T> clazz, String dateStr, String format); static LocalDate createLocalDate(String dateStr); static LocalDate createLocalDate(String dateStr, String format); static LocalDateTime createLocalDateTime(String dateTimeStr); static LocalDateTime createLocalDateTime(String dateTimeStr, String format); static LocalTime createLocalTime(String timeStr); static LocalTime createLocalTime(String timeStr, String format); static ZonedDateTime createZonedDateTime(String dateTimeStr); static ZonedDateTime createZonedDateTime(String dateTimeStr, String format); static String formatDate(LocalDate date, String format); static String formatDateTime(LocalDateTime dateTime, String format); static String formatJava8Date(Class<?> clazz, Object value, String format); static String formatTime(LocalTime time, String format); static String formatZonedDateTime(ZonedDateTime dateTime, String format); static int getLastWeekOfYear(int year); static String getNextWeekCode(String weekCode); static int getQuarter(LocalDate date); static LocalDate getStartDateOfWeek(String weekCode); static boolean isJava8DateType(Class<?> clazz); static boolean isSupportedDateType(Class<?> clazz); static boolean isValidWeekCode(String weekCode); static Date toLegacyDate(LocalDate d); static Date toLegacyDate(LocalDateTime ldt); static String toWeekCode(LocalDate date); static final String WEEK_CODE_PATTERN; }
@Test public void testGetStartDateOfWeek() { assertEquals(DateUtils.createLocalDate("30122013"), DateUtils.getStartDateOfWeek("2014-01")); assertEquals(DateUtils.createLocalDate("06012014"), DateUtils.getStartDateOfWeek("2014-02")); assertEquals(DateUtils.createLocalDate("29122014"), DateUtils.getStartDateOfWeek("2015-01")); assertEquals(DateUtils.createLocalDate("05012015"), DateUtils.getStartDateOfWeek("2015-02")); }
@Test public void testGetStartDayOfWeek() { LocalDate ld = DateUtils.getStartDateOfWeek("2013-51"); assertEquals("2013-12-16", ld.toString()); ld = DateUtils.getStartDateOfWeek("2013-52"); assertEquals("2013-12-23", ld.toString()); ld = DateUtils.getStartDateOfWeek("2014-01"); assertEquals("2013-12-30", ld.toString()); ld = DateUtils.getStartDateOfWeek("2015-53"); assertEquals("2015-12-28", ld.toString()); }