/** * Formats the date. * * <p>If the FieldPosition {@code field} is not null, and the field specified by this * FieldPosition is formatted, set the begin and end index of the formatted field in the * FieldPosition. * * <p>If the list {@code fields} is not null, find fields of this date, set FieldPositions with * these fields, and add them to the fields vector. * * @param date Date to Format * @param buffer StringBuffer to store the resulting formatted String * @param field FieldPosition to set begin and end index of the field specified, if it is part of * the format for this date * @param fields list used to store the FieldPositions for each field in this date * @return the formatted Date * @throws IllegalArgumentException if the object cannot be formatted by this Format. */ private StringBuffer formatImpl( Date date, StringBuffer buffer, FieldPosition field, List<FieldPosition> fields) { boolean quote = false; int next, last = -1, count = 0; calendar.setTime(date); if (field != null) { field.clear(); } final int patternLength = pattern.length(); for (int i = 0; i < patternLength; i++) { next = (pattern.charAt(i)); if (next == '\'') { if (count > 0) { append(buffer, field, fields, (char) last, count); count = 0; } if (last == next) { buffer.append('\''); last = -1; } else { last = next; } quote = !quote; continue; } if (!quote && (last == next || (next >= 'a' && next <= 'z') || (next >= 'A' && next <= 'Z'))) { if (last == next) { count++; } else { if (count > 0) { append(buffer, field, fields, (char) last, count); } last = next; count = 1; } } else { if (count > 0) { append(buffer, field, fields, (char) last, count); count = 0; } last = -1; buffer.append((char) next); } } if (count > 0) { append(buffer, field, fields, (char) last, count); } return buffer; }