private AttributedCharacterIterator formatToCharacterIteratorImpl(Date date) { StringBuffer buffer = new StringBuffer(); ArrayList<FieldPosition> fields = new ArrayList<FieldPosition>(); // format the date, and find fields formatImpl(date, buffer, null, fields); // create and AttributedString with the formatted buffer AttributedString as = new AttributedString(buffer.toString()); // add DateFormat field attributes to the AttributedString for (FieldPosition pos : fields) { Format.Field attribute = pos.getFieldAttribute(); as.addAttribute(attribute, attribute, pos.getBeginIndex(), pos.getEndIndex()); } // return the CharacterIterator from AttributedString return as.getIterator(); }
private void append( StringBuffer buffer, FieldPosition position, List<FieldPosition> fields, char format, int count) { int field = -1; int index = PATTERN_CHARS.indexOf(format); if (index == -1) { throw new IllegalArgumentException("Unknown pattern character '" + format + "'"); } int beginPosition = buffer.length(); Field dateFormatField = null; switch (index) { case ERA_FIELD: dateFormatField = Field.ERA; buffer.append(formatData.eras[calendar.get(Calendar.ERA)]); break; case YEAR_FIELD: dateFormatField = Field.YEAR; int year = calendar.get(Calendar.YEAR); /* * For 'y' and 'yyy', we're consistent with Unicode and previous releases * of Android. But this means we're inconsistent with the RI. * http://unicode.org/reports/tr35/ */ if (count == 2) { appendNumber(buffer, 2, year % 100); } else { appendNumber(buffer, count, year); } break; case STAND_ALONE_MONTH_FIELD: // 'L' dateFormatField = Field.MONTH; appendMonth(buffer, count, true); break; case MONTH_FIELD: // 'M' dateFormatField = Field.MONTH; appendMonth(buffer, count, false); break; case DATE_FIELD: dateFormatField = Field.DAY_OF_MONTH; field = Calendar.DATE; break; case HOUR_OF_DAY1_FIELD: // 'k' dateFormatField = Field.HOUR_OF_DAY1; int hour = calendar.get(Calendar.HOUR_OF_DAY); appendNumber(buffer, count, hour == 0 ? 24 : hour); break; case HOUR_OF_DAY0_FIELD: // 'H' dateFormatField = Field.HOUR_OF_DAY0; field = Calendar.HOUR_OF_DAY; break; case MINUTE_FIELD: dateFormatField = Field.MINUTE; field = Calendar.MINUTE; break; case SECOND_FIELD: dateFormatField = Field.SECOND; field = Calendar.SECOND; break; case MILLISECOND_FIELD: dateFormatField = Field.MILLISECOND; int value = calendar.get(Calendar.MILLISECOND); appendNumber(buffer, count, value); break; case STAND_ALONE_DAY_OF_WEEK_FIELD: dateFormatField = Field.DAY_OF_WEEK; appendDayOfWeek(buffer, count, true); break; case DAY_OF_WEEK_FIELD: dateFormatField = Field.DAY_OF_WEEK; appendDayOfWeek(buffer, count, false); break; case DAY_OF_YEAR_FIELD: dateFormatField = Field.DAY_OF_YEAR; field = Calendar.DAY_OF_YEAR; break; case DAY_OF_WEEK_IN_MONTH_FIELD: dateFormatField = Field.DAY_OF_WEEK_IN_MONTH; field = Calendar.DAY_OF_WEEK_IN_MONTH; break; case WEEK_OF_YEAR_FIELD: dateFormatField = Field.WEEK_OF_YEAR; field = Calendar.WEEK_OF_YEAR; break; case WEEK_OF_MONTH_FIELD: dateFormatField = Field.WEEK_OF_MONTH; field = Calendar.WEEK_OF_MONTH; break; case AM_PM_FIELD: dateFormatField = Field.AM_PM; buffer.append(formatData.ampms[calendar.get(Calendar.AM_PM)]); break; case HOUR1_FIELD: // 'h' dateFormatField = Field.HOUR1; hour = calendar.get(Calendar.HOUR); appendNumber(buffer, count, hour == 0 ? 12 : hour); break; case HOUR0_FIELD: // 'K' dateFormatField = Field.HOUR0; field = Calendar.HOUR; break; case TIMEZONE_FIELD: // 'z' dateFormatField = Field.TIME_ZONE; appendTimeZone(buffer, count, true); break; case RFC_822_TIMEZONE_FIELD: // 'Z' dateFormatField = Field.TIME_ZONE; appendNumericTimeZone(buffer, count, false); break; } if (field != -1) { appendNumber(buffer, count, calendar.get(field)); } if (fields != null) { position = new FieldPosition(dateFormatField); position.setBeginIndex(beginPosition); position.setEndIndex(buffer.length()); fields.add(position); } else { // Set to the first occurrence if ((position.getFieldAttribute() == dateFormatField || (position.getFieldAttribute() == null && position.getField() == index)) && position.getEndIndex() == 0) { position.setBeginIndex(beginPosition); position.setEndIndex(buffer.length()); } } }