Search in sources :

Example 81 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project android_frameworks_base by DirtyUnicorns.

the class DatePickerCalendarDelegate method onLocaleChanged.

@Override
protected void onLocaleChanged(Locale locale) {
    final TextView headerYear = mHeaderYear;
    if (headerYear == null) {
        // again later after everything has been set up.
        return;
    }
    // Update the date formatter.
    final String datePattern = DateFormat.getBestDateTimePattern(locale, "EMMMd");
    mMonthDayFormat = new SimpleDateFormat(datePattern, locale);
    mMonthDayFormat.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE);
    mYearFormat = new SimpleDateFormat("y", locale);
    // Clear out the lazily-initialized accessibility event formatter.
    mAccessibilityEventFormat = null;
    // Update the header text.
    onCurrentDateChanged(false);
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat)

Example 82 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project android_frameworks_base by DirtyUnicorns.

the class DatePickerCalendarDelegate method onPopulateAccessibilityEvent.

@Override
public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
    if (mAccessibilityEventFormat == null) {
        final String pattern = DateFormat.getBestDateTimePattern(mCurrentLocale, "EMMMMdy");
        mAccessibilityEventFormat = new SimpleDateFormat(pattern);
    }
    final CharSequence text = mAccessibilityEventFormat.format(mCurrentDate.getTime());
    event.getText().add(text);
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat)

Example 83 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project j2objc by google.

the class GlobalizationPreferences method getDisplayName.

/**
 * Get the display name for an ID: language, script, territory, currency, timezone...
 * Uses the language priority list to do so.
 *
 * @param id language code, script code, ...
 * @param type specifies the type of the ID: ID_LANGUAGE, etc.
 * @return the display name
 * @hide draft / provisional / internal are hidden on Android
 */
public String getDisplayName(String id, int type) {
    String result = id;
    for (ULocale locale : getLocales()) {
        if (!isAvailableLocale(locale, TYPE_GENERIC)) {
            continue;
        }
        switch(type) {
            case ID_LOCALE:
                result = ULocale.getDisplayName(id, locale);
                break;
            case ID_LANGUAGE:
                result = ULocale.getDisplayLanguage(id, locale);
                break;
            case ID_SCRIPT:
                result = ULocale.getDisplayScript("und-" + id, locale);
                break;
            case ID_TERRITORY:
                result = ULocale.getDisplayCountry("und-" + id, locale);
                break;
            case ID_VARIANT:
                // TODO fix variant parsing
                result = ULocale.getDisplayVariant("und-QQ-" + id, locale);
                break;
            case ID_KEYWORD:
                result = ULocale.getDisplayKeyword(id, locale);
                break;
            case ID_KEYWORD_VALUE:
                String[] parts = new String[2];
                Utility.split(id, '=', parts);
                result = ULocale.getDisplayKeywordValue("und@" + id, parts[0], locale);
                // TODO fix to tell when successful
                if (result.equals(parts[1])) {
                    continue;
                }
                break;
            case ID_CURRENCY_SYMBOL:
            case ID_CURRENCY:
                Currency temp = new Currency(id);
                result = temp.getName(locale, type == ID_CURRENCY ? Currency.LONG_NAME : Currency.SYMBOL_NAME, new boolean[1]);
                // to create a currency
                break;
            case ID_TIMEZONE:
                SimpleDateFormat dtf = new SimpleDateFormat("vvvv", locale);
                dtf.setTimeZone(TimeZone.getFrozenTimeZone(id));
                result = dtf.format(new Date());
                // TODO, have method that doesn't require us to create a timezone
                // fix other hacks
                // hack for couldn't match
                boolean isBadStr = false;
                // Matcher badTimeZone = Pattern.compile("[A-Z]{2}|.*\\s\\([A-Z]{2}\\)").matcher("");
                // badtzstr = badTimeZone.reset(result).matches();
                String teststr = result;
                int sidx = result.indexOf('(');
                int eidx = result.indexOf(')');
                if (sidx != -1 && eidx != -1 && (eidx - sidx) == 3) {
                    teststr = result.substring(sidx + 1, eidx);
                }
                if (teststr.length() == 2) {
                    isBadStr = true;
                    for (int i = 0; i < 2; i++) {
                        char c = teststr.charAt(i);
                        if (c < 'A' || 'Z' < c) {
                            isBadStr = false;
                            break;
                        }
                    }
                }
                if (isBadStr) {
                    continue;
                }
                break;
            default:
                throw new IllegalArgumentException("Unknown type: " + type);
        }
        // This will not work at all for lots of stuff
        if (!id.equals(result)) {
            return result;
        }
    }
    return result;
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date)

Example 84 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project j2objc by google.

the class CalendarRegressionTest method Test4197699.

/**
 * Week of year is wrong at the start and end of the year.
 */
@Test
public void Test4197699() {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.setMinimalDaysInFirstWeek(4);
    DateFormat fmt = new SimpleDateFormat("E dd MMM yyyy  'DOY='D 'WOY='w");
    fmt.setCalendar(cal);
    int[] DATA = { 2000, Calendar.JANUARY, 1, 52, 2001, Calendar.DECEMBER, 31, 1 };
    for (int i = 0; i < DATA.length; ) {
        cal.set(DATA[i++], DATA[i++], DATA[i++]);
        int expWOY = DATA[i++];
        int actWOY = cal.get(Calendar.WEEK_OF_YEAR);
        if (expWOY == actWOY) {
            logln("Ok: " + fmt.format(cal.getTime()));
        } else {
            errln("FAIL: " + fmt.format(cal.getTime()) + ", expected WOY=" + expWOY);
            cal.add(Calendar.DATE, -8);
            for (int j = 0; j < 14; ++j) {
                cal.add(Calendar.DATE, 1);
                logln(fmt.format(cal.getTime()));
            }
        }
    }
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 85 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project j2objc by google.

the class CalendarRegressionTest method Test4086724.

@Test
public void Test4086724() {
    SimpleDateFormat date;
    TimeZone saveZone = TimeZone.getDefault();
    Locale saveLocale = Locale.getDefault();
    try {
        Locale.setDefault(Locale.UK);
        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
        date = new SimpleDateFormat("dd MMM yyy (zzzz) 'is in week' ww");
        Calendar cal = Calendar.getInstance();
        cal.set(1997, Calendar.SEPTEMBER, 30);
        Date now = cal.getTime();
        logln(date.format(now));
        cal.set(1997, Calendar.JANUARY, 1);
        now = cal.getTime();
        logln(date.format(now));
        cal.set(1997, Calendar.JANUARY, 8);
        now = cal.getTime();
        logln(date.format(now));
        cal.set(1996, Calendar.DECEMBER, 31);
        now = cal.getTime();
        logln(date.format(now));
    } finally {
        Locale.setDefault(saveLocale);
        TimeZone.setDefault(saveZone);
    }
    logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Aggregations

SimpleDateFormat (android.icu.text.SimpleDateFormat)153 Test (org.junit.Test)113 Date (java.util.Date)103 GregorianCalendar (android.icu.util.GregorianCalendar)59 Calendar (android.icu.util.Calendar)53 ParseException (java.text.ParseException)42 DateFormat (android.icu.text.DateFormat)41 JapaneseCalendar (android.icu.util.JapaneseCalendar)41 ULocale (android.icu.util.ULocale)40 IslamicCalendar (android.icu.util.IslamicCalendar)37 ParsePosition (java.text.ParsePosition)36 FieldPosition (java.text.FieldPosition)29 ChineseCalendar (android.icu.util.ChineseCalendar)27 TimeZone (android.icu.util.TimeZone)27 BuddhistCalendar (android.icu.util.BuddhistCalendar)25 ChineseDateFormat (android.icu.text.ChineseDateFormat)21 HebrewCalendar (android.icu.util.HebrewCalendar)21 IOException (java.io.IOException)19 SimpleTimeZone (android.icu.util.SimpleTimeZone)16 Locale (java.util.Locale)14