Search in sources :

Example 1 with DateNumberFormat

use of android.icu.impl.DateNumberFormat in project j2objc by google.

the class SimpleDateFormat method initialize.

/*
     * Initialized fields
     */
private void initialize() {
    if (locale == null) {
        locale = ULocale.getDefault(Category.FORMAT);
    }
    if (formatData == null) {
        formatData = new DateFormatSymbols(locale);
    }
    if (calendar == null) {
        calendar = Calendar.getInstance(locale);
    }
    if (numberFormat == null) {
        NumberingSystem ns = NumberingSystem.getInstance(locale);
        if (ns.isAlgorithmic()) {
            numberFormat = NumberFormat.getInstance(locale);
        } else {
            String digitString = ns.getDescription();
            String nsName = ns.getName();
            // Use a NumberFormat optimized for date formatting
            numberFormat = new DateNumberFormat(locale, digitString, nsName);
        }
    }
    // Note: deferring calendar calculation until when we really need it.
    // Instead, we just record time of construction for backward compatibility.
    defaultCenturyBase = System.currentTimeMillis();
    setLocale(calendar.getLocale(ULocale.VALID_LOCALE), calendar.getLocale(ULocale.ACTUAL_LOCALE));
    initLocalZeroPaddingNumberFormat();
    if (override != null) {
        initNumberFormatters(locale);
    }
    parsePattern();
}
Also used : AttributedString(java.text.AttributedString) DateNumberFormat(android.icu.impl.DateNumberFormat)

Example 2 with DateNumberFormat

use of android.icu.impl.DateNumberFormat in project j2objc by google.

the class SimpleDateFormat method parseInt.

/**
 * Parse an integer using numberFormat up to maxDigits.
 */
private Number parseInt(String text, int maxDigits, ParsePosition pos, boolean allowNegative, NumberFormat fmt) {
    Number number;
    int oldPos = pos.getIndex();
    if (allowNegative) {
        number = fmt.parse(text, pos);
    } else {
        // Invalidate negative numbers
        if (fmt instanceof DecimalFormat) {
            String oldPrefix = ((DecimalFormat) fmt).getNegativePrefix();
            ((DecimalFormat) fmt).setNegativePrefix(SUPPRESS_NEGATIVE_PREFIX);
            number = fmt.parse(text, pos);
            ((DecimalFormat) fmt).setNegativePrefix(oldPrefix);
        } else {
            boolean dateNumberFormat = (fmt instanceof DateNumberFormat);
            if (dateNumberFormat) {
                ((DateNumberFormat) fmt).setParsePositiveOnly(true);
            }
            number = fmt.parse(text, pos);
            if (dateNumberFormat) {
                ((DateNumberFormat) fmt).setParsePositiveOnly(false);
            }
        }
    }
    if (maxDigits > 0) {
        // adjust the result to fit into
        // the maxDigits and move the position back
        int nDigits = pos.getIndex() - oldPos;
        if (nDigits > maxDigits) {
            double val = number.doubleValue();
            nDigits -= maxDigits;
            while (nDigits > 0) {
                val /= 10;
                nDigits--;
            }
            pos.setIndex(oldPos + maxDigits);
            number = Integer.valueOf((int) val);
        }
    }
    return number;
}
Also used : AttributedString(java.text.AttributedString) DateNumberFormat(android.icu.impl.DateNumberFormat)

Example 3 with DateNumberFormat

use of android.icu.impl.DateNumberFormat in project j2objc by google.

the class SimpleDateFormat method initializeTimeZoneFormat.

/**
 * Private method lazily instantiate the TimeZoneFormat field
 * @param bForceUpdate when true, check if tzFormat is synchronized with
 * the current numberFormat and update its digits if necessary. When false,
 * this check is skipped.
 */
private synchronized void initializeTimeZoneFormat(boolean bForceUpdate) {
    if (bForceUpdate || tzFormat == null) {
        tzFormat = TimeZoneFormat.getInstance(locale);
        String digits = null;
        if (numberFormat instanceof DecimalFormat) {
            DecimalFormatSymbols decsym = ((DecimalFormat) numberFormat).getDecimalFormatSymbols();
            digits = new String(decsym.getDigits());
        } else if (numberFormat instanceof DateNumberFormat) {
            digits = new String(((DateNumberFormat) numberFormat).getDigits());
        }
        if (digits != null) {
            if (!tzFormat.getGMTOffsetDigits().equals(digits)) {
                if (tzFormat.isFrozen()) {
                    tzFormat = tzFormat.cloneAsThawed();
                }
                tzFormat.setGMTOffsetDigits(digits);
            }
        }
    }
}
Also used : AttributedString(java.text.AttributedString) DateNumberFormat(android.icu.impl.DateNumberFormat)

Aggregations

DateNumberFormat (android.icu.impl.DateNumberFormat)3 AttributedString (java.text.AttributedString)3