Java Time's week-of-week-based-year pattern parsing with DateTimeFormatter
<p>I need to output the current date in the format <code>week-based-year</code>-<code>week-of-week-based-year</code>, i.e. using the <a href="https://en.wikipedia.org/wiki/ISO_week_date" rel="nofollow noreferrer">ISO week date</a> where the week always starts on a Monday and the first week of the year is the first one that has at least four days in January (so the week with the first Thursday in January).</p>
<p>Since 31 December 2015 was a Thursday, the Friday through Sunday, i.e. 1st through 3rd of January 2016, all belong in the 53rd week of 2015 (a &quot;long year&quot;), and the first week of 2016 starts on Monday, 4 January.</p>
<p>From the <a href="https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html" rel="nofollow noreferrer">DateTimeFormatter spec</a>, I would have expected that I can just use the pattern <code>YYYY-ww</code> to do that (<code>Y</code> is <code>week-based-year</code> and <code>w</code> is <code>week-of-week-based-year</code>).</p>
<p>However, when I try something like the following simplified test case:</p>
<pre><code>String dateString = &quot;2016-01-03&quot;;
LocalDate date = LocalDate.parse(dateString, DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd&quot;));
String expectedOutput = &quot;2015-53&quot;;
String actualOutput = date.format(DateTimeFormatter.ofPattern(&quot;YYYY-ww&quot;));
System.out.println(&quot;Parsing &quot; + dateString + &quot;, expected &quot;
        + expectedOutput + &quot; but got &quot; + actualOutput);
System.out.println(dateString + &quot; as ISO week date: &quot;
        + date.format(DateTimeFormatter.ISO_WEEK_DATE));
</code></pre>
<p>I get this:</p>
<blockquote>
<p><code>Parsing 2016-01-03, expected 2015-53 but got 2016-02</code></p>
<p><code>2016-01-03 as ISO week date: 2015-W53-7</code></p>
</blockquote>
<p>So using the built-in <code>ISO_WEEK_DATE</code> formatter does what I expected, but using the pattern <code>YYYY-ww</code> seems to give me the <em>calendar</em> year and week.</p>
<p>Have I misunderstood something about ISO week dates, or is there an error in my code, or... dare I say it... is this a bug in the <code>java.time</code> library (as was the case for <a href="https://stackoverflow.com/q/22874374/983430">this question</a>)?</p>
<p>I know I could work around this using a custom formatter like below, but in my case I really need this to work using a pattern.</p>
<pre><code>new DateTimeFormatterBuilder()
    .parseCaseInsensitive()
    .appendValue(IsoFields.WEEK_BASED_YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
    .appendLiteral(&quot;-&quot;)
    .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 2)
    .toFormatter();
</code></pre>