@Test public void testSampleISOWeekNumbers() { for (Date d : isoWeekNumbers.keySet()) { int expected = isoWeekNumbers.get(d); int calculated = DateTimeService.getISOWeekNumber(d); Assert.assertEquals(d + " should be week " + expected + " (Java Calendar is wrong?)", expected, getCalendarISOWeekNr(d)); Assert.assertEquals(d + " should be week " + expected, expected, calculated); } }
public static int getISOWeekNumber(Date date) { final long MILLISECONDS_PER_DAY = 24 * 3600 * 1000; int dayOfWeek = date.getDay(); if (dayOfWeek == 0) { dayOfWeek = 7; } int nearestThursdayDiff = 4 - dayOfWeek; Date nearestThursday = new Date(date.getTime() + nearestThursdayDiff * MILLISECONDS_PER_DAY); Date firstOfJanuary = new Date(nearestThursday.getYear(), 0, 1); long timeDiff = nearestThursday.getTime() - firstOfJanuary.getTime(); int daysSinceFirstOfJanuary = (int) Math.round((double) timeDiff / MILLISECONDS_PER_DAY); int weekNumber = (daysSinceFirstOfJanuary) / 7 + 1; return weekNumber; }
DateTimeService { public static int getISOWeekNumber(Date date) { final long MILLISECONDS_PER_DAY = 24 * 3600 * 1000; int dayOfWeek = date.getDay(); if (dayOfWeek == 0) { dayOfWeek = 7; } int nearestThursdayDiff = 4 - dayOfWeek; Date nearestThursday = new Date(date.getTime() + nearestThursdayDiff * MILLISECONDS_PER_DAY); Date firstOfJanuary = new Date(nearestThursday.getYear(), 0, 1); long timeDiff = nearestThursday.getTime() - firstOfJanuary.getTime(); int daysSinceFirstOfJanuary = (int) Math.round((double) timeDiff / MILLISECONDS_PER_DAY); int weekNumber = (daysSinceFirstOfJanuary) / 7 + 1; return weekNumber; } }
DateTimeService { public static int getISOWeekNumber(Date date) { final long MILLISECONDS_PER_DAY = 24 * 3600 * 1000; int dayOfWeek = date.getDay(); if (dayOfWeek == 0) { dayOfWeek = 7; } int nearestThursdayDiff = 4 - dayOfWeek; Date nearestThursday = new Date(date.getTime() + nearestThursdayDiff * MILLISECONDS_PER_DAY); Date firstOfJanuary = new Date(nearestThursday.getYear(), 0, 1); long timeDiff = nearestThursday.getTime() - firstOfJanuary.getTime(); int daysSinceFirstOfJanuary = (int) Math.round((double) timeDiff / MILLISECONDS_PER_DAY); int weekNumber = (daysSinceFirstOfJanuary) / 7 + 1; return weekNumber; } DateTimeService();  DateTimeService(String locale); }
DateTimeService { public static int getISOWeekNumber(Date date) { final long MILLISECONDS_PER_DAY = 24 * 3600 * 1000; int dayOfWeek = date.getDay(); if (dayOfWeek == 0) { dayOfWeek = 7; } int nearestThursdayDiff = 4 - dayOfWeek; Date nearestThursday = new Date(date.getTime() + nearestThursdayDiff * MILLISECONDS_PER_DAY); Date firstOfJanuary = new Date(nearestThursday.getYear(), 0, 1); long timeDiff = nearestThursday.getTime() - firstOfJanuary.getTime(); int daysSinceFirstOfJanuary = (int) Math.round((double) timeDiff / MILLISECONDS_PER_DAY); int weekNumber = (daysSinceFirstOfJanuary) / 7 + 1; return weekNumber; } DateTimeService();  DateTimeService(String locale); void setLocale(String locale); String getLocale(); String getMonth(int month); String getShortMonth(int month); String getDay(int day); String getShortDay(int day); int getFirstDayOfWeek(); boolean isTwelveHourClock(); String getClockDelimeter(); String[] getAmPmStrings(); int getStartWeekDay(Date date); static void setMilliseconds(Date date, int ms); static int getMilliseconds(Date date); static int getNumberOfDaysInMonth(Date date); static boolean isLeapYear(Date date); static boolean isSameDay(Date d1, Date d2); static boolean isInRange(Date date, Date rangeStart, Date rangeEnd,
            Resolution resolution); static int getISOWeekNumber(Date date); String formatDate(Date date, String formatStr); Date parseDate(String dateString, String formatString,
            boolean lenient); }
DateTimeService { public static int getISOWeekNumber(Date date) { final long MILLISECONDS_PER_DAY = 24 * 3600 * 1000; int dayOfWeek = date.getDay(); if (dayOfWeek == 0) { dayOfWeek = 7; } int nearestThursdayDiff = 4 - dayOfWeek; Date nearestThursday = new Date(date.getTime() + nearestThursdayDiff * MILLISECONDS_PER_DAY); Date firstOfJanuary = new Date(nearestThursday.getYear(), 0, 1); long timeDiff = nearestThursday.getTime() - firstOfJanuary.getTime(); int daysSinceFirstOfJanuary = (int) Math.round((double) timeDiff / MILLISECONDS_PER_DAY); int weekNumber = (daysSinceFirstOfJanuary) / 7 + 1; return weekNumber; } DateTimeService();  DateTimeService(String locale); void setLocale(String locale); String getLocale(); String getMonth(int month); String getShortMonth(int month); String getDay(int day); String getShortDay(int day); int getFirstDayOfWeek(); boolean isTwelveHourClock(); String getClockDelimeter(); String[] getAmPmStrings(); int getStartWeekDay(Date date); static void setMilliseconds(Date date, int ms); static int getMilliseconds(Date date); static int getNumberOfDaysInMonth(Date date); static boolean isLeapYear(Date date); static boolean isSameDay(Date d1, Date d2); static boolean isInRange(Date date, Date rangeStart, Date rangeEnd,
            Resolution resolution); static int getISOWeekNumber(Date date); String formatDate(Date date, String formatStr); Date parseDate(String dateString, String formatString,
            boolean lenient);  }