The code below demonstrates the problematic implementation of weekly billing. This behavior is not an error, but the Joda-Time design solution uses the ISO standard from Monday to Sunday. (maybe this should be a mistake?)
Given the date I need to calculate the week number, this calculation should be i18n in nature. Value I must consider the correct numbering per week based on the user's regional settings.
The demo code below shows the incorrect Joda-Time calculation and the correct JDK calculation, in the application that we are trying to stick to Joda-Time, it is an excellent solution for manipulating dates. So, should you mix the two time calculation libraries? I would prefer not to do this, itβs even a safe thing, or Iβll come to corner affairs (having experience with Date, Calendar, I know that this is a very painful problem for Java).
Bottom line: What are the recommended best practices for the described requirement?
Problem Demo Code
Please see this online calendar showing week numbers for the correct week calculation example.
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(2009, 12, 6, 23, 0, 0, 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()); } }
Conclusion:
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 ?!?!)
source share