Example #1
0
 /**
  * Append a representation of the time zone of 'calendar' to 'buffer'.
  *
  * @param count the number of z or Z characters in the format string; "zzz" would be 3, for
  *     example.
  * @param generalTimeZone true if we should use a display name ("PDT") if available; false implies
  *     that we should use RFC 822 format ("-0800") instead. This corresponds to 'z' versus 'Z' in
  *     the format string.
  */
 private void appendTimeZone(StringBuffer buffer, int count, boolean generalTimeZone) {
   if (generalTimeZone) {
     TimeZone tz = calendar.getTimeZone();
     boolean daylight = (calendar.get(Calendar.DST_OFFSET) != 0);
     int style = count < 4 ? TimeZone.SHORT : TimeZone.LONG;
     if (!formatData.customZoneStrings) {
       buffer.append(tz.getDisplayName(daylight, style, formatData.locale));
       return;
     }
     // We can't call TimeZone.getDisplayName() because it would not use
     // the custom DateFormatSymbols of this SimpleDateFormat.
     String custom =
         TimeZoneNames.getDisplayName(formatData.zoneStrings, tz.getID(), daylight, style);
     if (custom != null) {
       buffer.append(custom);
       return;
     }
   }
   // We didn't find what we were looking for, so default to a numeric time zone.
   appendNumericTimeZone(buffer, count, generalTimeZone);
 }
Example #2
0
  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());
      }
    }
  }