Search in sources :

Example 16 with LocaleData

use of libcore.icu.LocaleData in project android_frameworks_base by AOSPA.

the class DateFormat method format.

/**
     * Given a format string and a {@link java.util.Calendar} object, returns a CharSequence
     * containing the requested date.
     * @param inFormat the format string, as described in {@link android.text.format.DateFormat}
     * @param inDate the date to format
     * @return a {@link CharSequence} containing the requested text
     */
public static CharSequence format(CharSequence inFormat, Calendar inDate) {
    SpannableStringBuilder s = new SpannableStringBuilder(inFormat);
    int count;
    LocaleData localeData = LocaleData.get(Locale.getDefault());
    int len = inFormat.length();
    for (int i = 0; i < len; i += count) {
        count = 1;
        int c = s.charAt(i);
        if (c == QUOTE) {
            count = appendQuotedText(s, i, len);
            len = s.length();
            continue;
        }
        while ((i + count < len) && (s.charAt(i + count) == c)) {
            count++;
        }
        String replacement;
        switch(c) {
            case 'A':
            case 'a':
                replacement = localeData.amPm[inDate.get(Calendar.AM_PM) - Calendar.AM];
                break;
            case 'd':
                replacement = zeroPad(inDate.get(Calendar.DATE), count);
                break;
            case 'c':
            case 'E':
                replacement = getDayOfWeekString(localeData, inDate.get(Calendar.DAY_OF_WEEK), count, c);
                break;
            // hour in am/pm (0-11)
            case 'K':
            case // hour in am/pm (1-12)
            'h':
                {
                    int hour = inDate.get(Calendar.HOUR);
                    if (c == 'h' && hour == 0) {
                        hour = 12;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            // hour in day (0-23)
            case 'H':
            case // hour in day (1-24) [but see note below]
            'k':
                {
                    int hour = inDate.get(Calendar.HOUR_OF_DAY);
                    // times are abusing 'k'. http://b/8359981.
                    if (false && c == 'k' && hour == 0) {
                        hour = 24;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            case 'L':
            case 'M':
                replacement = getMonthString(localeData, inDate.get(Calendar.MONTH), count, c);
                break;
            case 'm':
                replacement = zeroPad(inDate.get(Calendar.MINUTE), count);
                break;
            case 's':
                replacement = zeroPad(inDate.get(Calendar.SECOND), count);
                break;
            case 'y':
                replacement = getYearString(inDate.get(Calendar.YEAR), count);
                break;
            case 'z':
                replacement = getTimeZoneString(inDate, count);
                break;
            default:
                replacement = null;
                break;
        }
        if (replacement != null) {
            s.replace(i, i + count, replacement);
            // CARE: count is used in the for loop above
            count = replacement.length();
            len = s.length();
        }
    }
    if (inFormat instanceof Spanned) {
        return new SpannedString(s);
    } else {
        return s.toString();
    }
}
Also used : LocaleData(libcore.icu.LocaleData) SpannedString(android.text.SpannedString) SpannedString(android.text.SpannedString) Spanned(android.text.Spanned) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 17 with LocaleData

use of libcore.icu.LocaleData in project robovm by robovm.

the class Date method toString.

/**
     * Returns a string representation of this {@code Date}.
     * The formatting is equivalent to using a {@code SimpleDateFormat} with
     * the format string "EEE MMM dd HH:mm:ss zzz yyyy", which looks something
     * like "Tue Jun 22 13:07:00 PDT 1999". The current default time zone and
     * locale are used. If you need control over the time zone or locale,
     * use {@code SimpleDateFormat} instead.
     */
@Override
public String toString() {
    // TODO: equivalent to the following one-liner, though that's slower on stingray
    // at 476us versus 69us...
    //   return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").format(d);
    LocaleData localeData = LocaleData.get(Locale.US);
    Calendar cal = new GregorianCalendar(milliseconds);
    TimeZone tz = cal.getTimeZone();
    StringBuilder result = new StringBuilder();
    result.append(localeData.shortWeekdayNames[cal.get(Calendar.DAY_OF_WEEK)]);
    result.append(' ');
    result.append(localeData.shortMonthNames[cal.get(Calendar.MONTH)]);
    result.append(' ');
    appendTwoDigits(result, cal.get(Calendar.DAY_OF_MONTH));
    result.append(' ');
    appendTwoDigits(result, cal.get(Calendar.HOUR_OF_DAY));
    result.append(':');
    appendTwoDigits(result, cal.get(Calendar.MINUTE));
    result.append(':');
    appendTwoDigits(result, cal.get(Calendar.SECOND));
    result.append(' ');
    result.append(tz.getDisplayName(tz.inDaylightTime(this), TimeZone.SHORT));
    result.append(' ');
    result.append(cal.get(Calendar.YEAR));
    return result.toString();
}
Also used : LocaleData(libcore.icu.LocaleData)

Example 18 with LocaleData

use of libcore.icu.LocaleData in project robovm by robovm.

the class SimpleDateFormat method parseDayOfWeek.

private int parseDayOfWeek(String string, int offset, boolean standAlone) {
    LocaleData ld = formatData.localeData;
    int index = parseText(string, offset, standAlone ? ld.longStandAloneWeekdayNames : formatData.weekdays, Calendar.DAY_OF_WEEK);
    if (index < 0) {
        index = parseText(string, offset, standAlone ? ld.shortStandAloneWeekdayNames : formatData.shortWeekdays, Calendar.DAY_OF_WEEK);
    }
    return index;
}
Also used : LocaleData(libcore.icu.LocaleData)

Example 19 with LocaleData

use of libcore.icu.LocaleData in project robovm by robovm.

the class SimpleDateFormat method parseMonth.

private int parseMonth(String string, int offset, int count, int absolute, boolean standAlone) {
    if (count <= 2) {
        return parseNumber(absolute, string, offset, Calendar.MONTH, -1);
    }
    LocaleData ld = formatData.localeData;
    int index = parseText(string, offset, standAlone ? ld.longStandAloneMonthNames : formatData.months, Calendar.MONTH);
    if (index < 0) {
        index = parseText(string, offset, standAlone ? ld.shortStandAloneMonthNames : formatData.shortMonths, Calendar.MONTH);
    }
    return index;
}
Also used : LocaleData(libcore.icu.LocaleData)

Example 20 with LocaleData

use of libcore.icu.LocaleData in project robovm by robovm.

the class SimpleDateFormat method appendDayOfWeek.

// See http://www.unicode.org/reports/tr35/#Date_Format_Patterns for the different counts.
private void appendDayOfWeek(StringBuffer buffer, int count, boolean standAlone) {
    String[] days;
    LocaleData ld = formatData.localeData;
    if (count == 4) {
        days = standAlone ? ld.longStandAloneWeekdayNames : formatData.weekdays;
    } else if (count == 5) {
        days = standAlone ? ld.tinyStandAloneWeekdayNames : formatData.localeData.tinyWeekdayNames;
    } else {
        days = standAlone ? ld.shortStandAloneWeekdayNames : formatData.shortWeekdays;
    }
    buffer.append(days[calendar.get(Calendar.DAY_OF_WEEK)]);
}
Also used : LocaleData(libcore.icu.LocaleData)

Aggregations

LocaleData (libcore.icu.LocaleData)78 SpannableStringBuilder (android.text.SpannableStringBuilder)12 Locale (java.util.Locale)11 Test (org.junit.Test)8 Context (android.content.Context)6 Spanned (android.text.Spanned)6 SpannedString (android.text.SpannedString)6 CharacterStyle (android.text.style.CharacterStyle)6 RelativeSizeSpan (android.text.style.RelativeSizeSpan)6 SimpleDateFormat (java.text.SimpleDateFormat)6 Config (org.robolectric.annotation.Config)6 Date (java.util.Date)3 Resources (android.content.res.Resources)1 Implementation (org.robolectric.annotation.Implementation)1