aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/terminal/gwt/client/DateTimeService.java
diff options
context:
space:
mode:
authorArtur Signell <artur.signell@itmill.com>2010-03-22 11:34:39 +0000
committerArtur Signell <artur.signell@itmill.com>2010-03-22 11:34:39 +0000
commit5abcfae71f8c32d5209f1d9cc6870f50b69dd6fa (patch)
treec60e54e437ef9c60407b48ea260a07b9418277f8 /src/com/vaadin/terminal/gwt/client/DateTimeService.java
parenta77594d16f1bdb951a2c0247214e44aadd6d9a78 (diff)
downloadvaadin-framework-5abcfae71f8c32d5209f1d9cc6870f50b69dd6fa.tar.gz
vaadin-framework-5abcfae71f8c32d5209f1d9cc6870f50b69dd6fa.zip
Fix for #3492 - DateField should have an option to show week numbers
svn changeset:12004/svn branch:6.3
Diffstat (limited to 'src/com/vaadin/terminal/gwt/client/DateTimeService.java')
-rw-r--r--src/com/vaadin/terminal/gwt/client/DateTimeService.java31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/com/vaadin/terminal/gwt/client/DateTimeService.java b/src/com/vaadin/terminal/gwt/client/DateTimeService.java
index e16d9b078d..7bc8e8eba4 100644
--- a/src/com/vaadin/terminal/gwt/client/DateTimeService.java
+++ b/src/com/vaadin/terminal/gwt/client/DateTimeService.java
@@ -234,4 +234,35 @@ public class DateTimeService {
return ((y + 1900) * 10000 + m * 100 + d) * 1000000000;
}
+ /**
+ * Returns the ISO-8601 week number of the given date.
+ *
+ * @param date
+ * The date for which the week number should be resolved
+ * @return The ISO-8601 week number for {@literal date}
+ */
+ public static int getISOWeekNumber(Date date) {
+ final long MILLISECONDS_PER_DAY = 24 * 3600 * 1000;
+ int dayOfWeek = date.getDay(); // 0 == sunday
+
+ // ISO 8601 use weeks that start on monday so we use
+ // mon=1,tue=2,...sun=7;
+ if (dayOfWeek == 0) {
+ dayOfWeek = 7;
+ }
+ // Find nearest thursday (defines the week in ISO 8601). The week number
+ // for the nearest thursday is the same as for the target date.
+ int nearestThursdayDiff = 4 - dayOfWeek; // 4 is thursday
+ 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) (timeDiff / MILLISECONDS_PER_DAY);
+
+ int weekNumber = (daysSinceFirstOfJanuary) / 7 + 1;
+
+ return weekNumber;
+ }
+
}