Note that there are some explanatory texts on larger screens.

plurals
  1. POJoda time week calculation reasoning
    text
    copied!<p>The code below demonstrates the problematic joda-time implementation of week calculation. This behavior is not a bug but a design decision <a href="https://stackoverflow.com/questions/1801907/joda-time-first-day-of-week/1808242#1808242">Joda-Time uses the ISO standard Monday to Sunday week.</a> (perhaps it <em>should</em> be a bug?)</p> <p>Given a date I need to calculate the week number, this calculation must be i18n in nature. Meaning <strong>I must</strong> take into consideration the correct week numbering based on the regional settings of the user.</p> <p>The demo code below shows wrong calculation by Joda-Time and correct calculation by the JDK, in the application we try to stick with Joda-Time being a superior solution for date manipulations. So, should I be mixing the two Time calculation libraries? I would obviously prefer not to, is this even a safe thing to do or would I come into corner cases (having experience with Date, Calendar I know for a fact that this is a painful issue for Java).</p> <p>Bottom line: What is the recommended best-practice for the described requirement?</p> <h2>Problem demonstration code</h2> <p>Please see this <a href="http://www.timeanddate.com/calendar/custom.html?year=2009&amp;country=34&amp;cols=0&amp;wno=1&amp;cdt=1&amp;typ=0&amp;display=3&amp;df=1" rel="nofollow noreferrer">online calendar displaying week numbers</a> for correct week calculation example.</p> <pre><code>public class JodaTest { static DateTimeFormatter formatter = DateTimeFormat.forPattern("ww yyyy"); static SimpleDateFormat jdkFormatter = new SimpleDateFormat("ww yyyy"); public static void main(String[] args) { DateTime time = new DateTime(/*year*/2009, /*monthOfYear*/12, /*dayOfMonth*/6, /*hourOfDay*/23, /*minuteOfHour*/0, /*secondOfMinute*/0, /*millisOfSecond*/0); StringBuilder buffer = new StringBuilder() .append("Testing date ").append(time.toString()).append("\n") .append("Joda-Time timezone is ").append(DateTimeZone.getDefault()).append(" yet joda wrongly thinks week is ").append(formatter.print(time)).append("\n") .append("JDK timezone is ").append(TimeZone.getDefault().getID()).append(" yet jdk rightfully thinks week is ").append(jdkFormatter.format(time.toDate())).append(" (jdk got it right ?!?!)"); System.out.println(buffer.toString()); } } </code></pre> <p>Output:</p> <pre><code>Testing date 2009-12-06T23:00:00.000+02:00 Joda-Time timezone is Asia/Jerusalem yet joda wrongly thinks week is 49 2009 JDK time zone is Asia/Jerusalem yet jdk rightfully thinks week is 50 2009 (jdk got it right ?!?!) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload