DateUtils { public static String toWeekCode(LocalDate date) { if (date != null) { WeekFields weekFields = WeekFields.ISO; int year = date.getYear(); int week = date.get(weekFields.weekOfWeekBasedYear()); Month month = date.getMonth(); if (week == FIRST_WEEK_NUMBER && month == Month.DECEMBER) { year++; } if ((week == LAST_WEEK_NUMBER || week == LAST_WEEK_NUMBER - 1) && month == Month.JANUARY) { year--; } return year + "-" + StringUtils.leftPad(Integer.toString(week), 2, "0"); } return null; } 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 testToWeekCode() { assertEquals("2013-51", DateUtils.toWeekCode(DateUtils.createLocalDate("16122013"))); assertEquals("2013-52", DateUtils.toWeekCode(DateUtils.createLocalDate("23122013"))); assertEquals("2014-01", DateUtils.toWeekCode(DateUtils.createLocalDate("30122013"))); assertEquals("2014-02", DateUtils.toWeekCode(DateUtils.createLocalDate("06012014"))); assertEquals("2015-52", DateUtils.toWeekCode(DateUtils.createLocalDate("21122015"))); assertEquals("2015-53", DateUtils.toWeekCode(DateUtils.createLocalDate("28122015"))); assertEquals("2016-01", DateUtils.toWeekCode(DateUtils.createLocalDate("04012016"))); assertEquals("2016-02", DateUtils.toWeekCode(DateUtils.createLocalDate("11012016"))); }