Justification of the calculation of time in Iodine

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(/*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()); } } 

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 ?!?!) 
+4
source share
1 answer

The best solution available is to write a DateTimeField implementation that completes the logic to extract the desired value based on the locale. Inside, you'll probably still rely on JDK data. The goal is to wrap all of the JDK code in one reusable class. Then you use it as follows:

 int value = dateTime.get(new LocaleAwareWeekField("en_GB")); 
+2
source

Source: https://habr.com/ru/post/1300519/


All Articles