Why does parsing a date with weekyear return the w

2020-04-21 09:04发布

问题:

DateTime time=DateTimeFormat.forPattern("yyyy-ww").parseDateTime("2013-01"); 

where DateTimeFormat is org.joda.time.format.DateTimeFormat.

When I execute the above, time becomes 2013-12-30T00:00:00.000 what am I doing wrong? Is this a bug? I expect it to be the first week of 2013, not the last.

回答1:

See here.

The w, week of weekyear, pattern letter is meant to be used with x, weekyear. From the javadoc of DateTimeFormat

 x       weekyear                     year          1996
 w       week of weekyear             number        27

Change your pattern to

DateTimeFormat.forPattern("xxxx-ww")

and you'll parse to

2012-12-31T00:00:00.000-08:00

Depending on your Locale, the first week of 2013 started in 2012.



标签: java jodatime