Example #1
0
 // See http://www.unicode.org/reports/tr35/#Date_Format_Patterns for the different counts.
 // @param generalTimeZone "GMT-08:00" rather than "-0800".
 private void appendNumericTimeZone(StringBuffer buffer, int count, boolean generalTimeZone) {
   int offset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
   char sign = '+';
   if (offset < 0) {
     sign = '-';
     offset = -offset;
   }
   if (generalTimeZone || count == 4) {
     buffer.append("GMT");
   }
   buffer.append(sign);
   appendNumber(buffer, 2, offset / 3600000);
   if (generalTimeZone || count >= 4) {
     buffer.append(':');
   }
   appendNumber(buffer, 2, (offset % 3600000) / 60000);
 }
Example #2
0
  // See http://www.unicode.org/reports/tr35/#Date_Format_Patterns for the different counts.
  private void appendMonth(StringBuffer buffer, int count, boolean standAlone) {
    int month = calendar.get(Calendar.MONTH);
    if (count <= 2) {
      appendNumber(buffer, count, month + 1);
      return;
    }

    String[] months;
    LocaleData ld = formatData.localeData;
    if (count == 4) {
      months = standAlone ? ld.longStandAloneMonthNames : formatData.months;
    } else if (count == 5) {
      months = standAlone ? ld.tinyStandAloneMonthNames : ld.tinyMonthNames;
    } else {
      months = standAlone ? ld.shortStandAloneMonthNames : formatData.shortMonths;
    }
    buffer.append(months[month]);
  }
Example #3
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());
      }
    }
  }