--- old/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java 2016-12-20 09:30:19.171491116 +0000 +++ new/src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java 2016-12-20 09:30:18.835459688 +0000 @@ -1774,16 +1774,20 @@ if (count > 1) { throw new IllegalArgumentException("Too many pattern letters: " + cur); } - appendInternal(new WeekBasedFieldPrinterParser(cur, count)); + appendValue(new WeekBasedFieldPrinterParser(cur, count, count, count)); } else if (cur == 'w') { // Fields defined by Locale if (count > 2) { throw new IllegalArgumentException("Too many pattern letters: " + cur); } - appendInternal(new WeekBasedFieldPrinterParser(cur, count)); + appendValue(new WeekBasedFieldPrinterParser(cur, count, count, 2)); } else if (cur == 'Y') { // Fields defined by Locale - appendInternal(new WeekBasedFieldPrinterParser(cur, count)); + if (count == 2) { + appendValue(new WeekBasedFieldPrinterParser(cur, count, count, 2)); + } else { + appendValue(new WeekBasedFieldPrinterParser(cur, count, count, 19)); + } } else { throw new IllegalArgumentException("Unknown pattern letter: " + cur); } @@ -1843,7 +1847,10 @@ } break; case 'c': - if (count == 2) { + if (count == 1) { + appendValue(new WeekBasedFieldPrinterParser(cur, count, count, count)); + break; + } else if (count == 2) { throw new IllegalArgumentException("Invalid pattern \"cc\""); } /*fallthrough*/ @@ -1858,8 +1865,8 @@ switch (count) { case 1: case 2: - if (cur == 'c' || cur == 'e') { - appendInternal(new WeekBasedFieldPrinterParser(cur, count)); + if (cur == 'e') { + appendValue(new WeekBasedFieldPrinterParser(cur, count, count, 2)); } else if (cur == 'E') { appendText(field, TextStyle.SHORT); } else { @@ -4771,7 +4778,7 @@ * The locale is needed to select the proper WeekFields from which * the field for day-of-week, week-of-month, or week-of-year is selected. */ - static final class WeekBasedFieldPrinterParser implements DateTimePrinterParser { + static final class WeekBasedFieldPrinterParser extends NumberPrinterParser { private char chr; private int count; @@ -4781,11 +4788,42 @@ * @param chr the pattern format letter that added this PrinterParser. * @param count the repeat count of the format letter */ - WeekBasedFieldPrinterParser(char chr, int count) { + WeekBasedFieldPrinterParser(char chr, int count, int minWidth, int maxWidth) { + this(chr, count, minWidth, maxWidth, 0); + } + + WeekBasedFieldPrinterParser(char chr, int count, int minWidth, int maxWidth, + int subsequentWidth) { + super(null, minWidth, maxWidth, SignStyle.NOT_NEGATIVE, subsequentWidth); this.chr = chr; this.count = count; } + /** + * Returns a new instance with fixed width flag set. + * + * @return a new updated printer-parser, not null + */ + @Override + WeekBasedFieldPrinterParser withFixedWidth() { + if (subsequentWidth == -1) { + return this; + } + return new WeekBasedFieldPrinterParser(chr, count, minWidth, maxWidth, -1); + } + + /** + * Returns a new instance with an updated subsequent width. + * + * @param subsequentWidth the width of subsequent non-negative numbers, 0 or greater + * @return a new updated printer-parser, not null + */ + @Override + WeekBasedFieldPrinterParser withSubsequentWidth(int subsequentWidth) { + return new WeekBasedFieldPrinterParser(chr, count, minWidth, maxWidth, + this.subsequentWidth + subsequentWidth); + } + @Override public boolean format(DateTimePrintContext context, StringBuilder buf) { return printerParser(context.getLocale()).format(context, buf); @@ -4810,10 +4848,12 @@ case 'Y': field = weekDef.weekBasedYear(); if (count == 2) { - return new ReducedPrinterParser(field, 2, 2, 0, ReducedPrinterParser.BASE_DATE, 0); + return new ReducedPrinterParser(field, 2, 2, 0, ReducedPrinterParser.BASE_DATE, + this.subsequentWidth); } else { return new NumberPrinterParser(field, count, 19, - (count < 4) ? SignStyle.NORMAL : SignStyle.EXCEEDS_PAD, -1); + (count < 4) ? SignStyle.NORMAL : SignStyle.EXCEEDS_PAD, + this.subsequentWidth); } case 'e': case 'c': @@ -4828,7 +4868,7 @@ default: throw new IllegalStateException("unreachable"); } - return new NumberPrinterParser(field, (count == 2 ? 2 : 1), 2, SignStyle.NOT_NEGATIVE); + return new NumberPrinterParser(field, minWidth, maxWidth, SignStyle.NOT_NEGATIVE, this.subsequentWidth); } @Override