Java – get localized week number using jodatime
I tried to get the current number of weeks with jodatime
In France, week is defined as follows:
>One week from Monday (weekend starts in the United States). > The first week of the year is the week of January 4 (and IMO, that's the week in the United States on January 1, isn't it?
Example: January 1, 2012 is Sunday So,
>According to the French calendar, it belongs to week 52 of 2011. > According to the U.S. calendar, it belongs to the first week of 2012
Using jodatime, I found that I can get the number of weeks datetime#getweekofweekyear()
I think by specifying the correct timezone, I will get a localized result:
DateTime dtFr = new DateTime(2012,1,11,DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/Paris"))); DateTime dtUS = new DateTime(2012,DateTimeZone.forTimeZone(TimeZone.getTimeZone("US/Arizona"))); LOGGER.info("weekYear (FR) : " + dtFr.weekyear().get()); LOGGER.info("weekOfWeekYear (FR) : " + dtFr.getWeekOfWeekyear()); LOGGER.info("weekYear (US) : " + dtUS.weekyear().get()); LOGGER.info("weekOfWeekYear (US) : " + dtUS.getWeekOfWeekyear());
Output is:
2014-03-05 11:28:08,708 - INFO - c.g.s.u.JodaTest - weekYear (FR) : 2011 2014-03-05 11:28:08,709 - INFO - c.g.s.u.JodaTest - weekOfWeekYear (FR) : 52 2014-03-05 11:28:08,709 - INFO - c.g.s.u.JodaTest - weekYear (US) : 2011 2014-03-05 11:28:08,709 - INFO - c.g.s.u.JodaTest - weekOfWeekYear (US) : 52
I expected:
> weekYear(US):2012 > WeekOfWeekYear(US):1
Is there a problem with my code?
Solution
Sorry for your question about localized week number: jodatime does not support it. Only iso-8601 defines the number of weeks I know many users, it's like a program stop Here's the old Java util.* – Things are obviously better
The only way to implement this function in jodatime is to introduce a special date time period, but it is certainly not easy to implement when the project leads itself to admit This has been noted in year 2007, but it hasn't happened since
By the way, the time zone has nothing to do with the problem of localized weeks, so you can't solve this old joda problem with any time zone configuration