< prev index next >

src/java.base/share/classes/java/time/format/DateTimeFormatterBuilder.java

Print this page

        

*** 1772,1791 **** } else if (cur == 'W') { // Fields defined by Locale if (count > 1) { throw new IllegalArgumentException("Too many pattern letters: " + cur); } ! appendInternal(new WeekBasedFieldPrinterParser(cur, 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)); } else if (cur == 'Y') { // Fields defined by Locale ! appendInternal(new WeekBasedFieldPrinterParser(cur, count)); } else { throw new IllegalArgumentException("Unknown pattern letter: " + cur); } pos--; --- 1772,1795 ---- } else if (cur == 'W') { // Fields defined by Locale if (count > 1) { throw new IllegalArgumentException("Too many pattern letters: " + cur); } ! 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); } ! appendValue(new WeekBasedFieldPrinterParser(cur, count, count, 2)); } else if (cur == 'Y') { // Fields defined by Locale ! 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); } pos--;
*** 1841,1851 **** } else { appendValue(field, count, 19, SignStyle.EXCEEDS_PAD); } break; case 'c': ! if (count == 2) { throw new IllegalArgumentException("Invalid pattern \"cc\""); } /*fallthrough*/ case 'L': case 'q': --- 1845,1858 ---- } else { appendValue(field, count, 19, SignStyle.EXCEEDS_PAD); } break; case 'c': ! if (count == 1) { ! appendValue(new WeekBasedFieldPrinterParser(cur, count, count, count)); ! break; ! } else if (count == 2) { throw new IllegalArgumentException("Invalid pattern \"cc\""); } /*fallthrough*/ case 'L': case 'q':
*** 1856,1867 **** case 'E': case 'e': switch (count) { case 1: case 2: ! if (cur == 'c' || cur == 'e') { ! appendInternal(new WeekBasedFieldPrinterParser(cur, count)); } else if (cur == 'E') { appendText(field, TextStyle.SHORT); } else { if (count == 1) { appendValue(field); --- 1863,1874 ---- case 'E': case 'e': switch (count) { case 1: case 2: ! if (cur == 'e') { ! appendValue(new WeekBasedFieldPrinterParser(cur, count, count, 2)); } else if (cur == 'E') { appendText(field, TextStyle.SHORT); } else { if (count == 1) { appendValue(field);
*** 4769,4793 **** * The specific formatter and parameters is not selected until the * the field is to be printed or parsed. * 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 { private char chr; private int count; /** * Constructor. * * @param chr the pattern format letter that added this PrinterParser. * @param count the repeat count of the format letter */ ! WeekBasedFieldPrinterParser(char chr, int count) { this.chr = chr; this.count = count; } @Override public boolean format(DateTimePrintContext context, StringBuilder buf) { return printerParser(context.getLocale()).format(context, buf); } --- 4776,4831 ---- * The specific formatter and parameters is not selected until the * the field is to be printed or parsed. * 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 extends NumberPrinterParser { private char chr; private int count; /** * Constructor. * * @param chr the pattern format letter that added this PrinterParser. * @param count the repeat count of the format letter */ ! 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); }
*** 4808,4821 **** TemporalField field = null; switch (chr) { case 'Y': field = weekDef.weekBasedYear(); if (count == 2) { ! return new ReducedPrinterParser(field, 2, 2, 0, ReducedPrinterParser.BASE_DATE, 0); } else { return new NumberPrinterParser(field, count, 19, ! (count < 4) ? SignStyle.NORMAL : SignStyle.EXCEEDS_PAD, -1); } case 'e': case 'c': field = weekDef.dayOfWeek(); break; --- 4846,4861 ---- TemporalField field = null; switch (chr) { case 'Y': field = weekDef.weekBasedYear(); if (count == 2) { ! 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, ! this.subsequentWidth); } case 'e': case 'c': field = weekDef.dayOfWeek(); break;
*** 4826,4836 **** field = weekDef.weekOfMonth(); break; default: throw new IllegalStateException("unreachable"); } ! return new NumberPrinterParser(field, (count == 2 ? 2 : 1), 2, SignStyle.NOT_NEGATIVE); } @Override public String toString() { StringBuilder sb = new StringBuilder(30); --- 4866,4876 ---- field = weekDef.weekOfMonth(); break; default: throw new IllegalStateException("unreachable"); } ! return new NumberPrinterParser(field, minWidth, maxWidth, SignStyle.NOT_NEGATIVE, this.subsequentWidth); } @Override public String toString() { StringBuilder sb = new StringBuilder(30);
< prev index next >