0

Well, the title says it all. What would be reliable way to determine the current calendar week offset starting from 01.01.1970 UTC using only java.util.Date?

I am using GWT and therefore I cannot use joda-time oder Calendar.

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • 2
    Do you mean the number of weeks since the epoch? The most reliable way would be to use a library designed for the task, like Java 8's Time API, JodaTime or even `Calendar` if you were pushed to it – MadProgrammer Sep 16 '15 at 22:17
  • @MadProgrammer The problem is that I can only use `Date` since I am using GWT that does not support e.g. `Calendar`. – Stefan Falk Sep 17 '15 at 08:17
  • GWT (sigh) does have a "kind of" `Calendar` - [for example](http://www.gwtproject.org/javadoc/latest/com/google/gwt/user/datepicker/client/CalendarUtil.html) or mayeb [something more like](http://stackoverflow.com/questions/10311754/date-time-library-for-gwt) – MadProgrammer Sep 17 '15 at 08:23
  • I wonder how you can get curent calendar week offset with joda-time – yelliver Sep 17 '15 at 08:38

1 Answers1

0

Jan-01-1970 is Thu, Jan-01-1970 is Sun. Suppose that from Jan, 01 to 04 is week 0.

    int oneDaySeconds = 24 * 3600 * 1000;
    long offset = new Date().getTime() + 4 * oneDaySeconds; // assume that Monday is first day of week
    System.out.println(offset / (7 * oneDaySeconds));

This function has a problem, it's the first day of a week. When use Calendar, denpend on Locale, the first day of week can be Sunday or Monday. If the first day of week is Sunday, the 2nd line should be plus 3 days instead of 4 days

yelliver
  • 5,648
  • 5
  • 34
  • 65