Search in sources :

Example 46 with DateFormat

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

the class GlobalizationPreferences method guessDateFormat.

/**
 * This function can be overridden by subclasses to use different heuristics.
 * <b>It MUST return a 'safe' value,
 * one whose modification will not affect this object.</b>
 *
 * @param dateStyle
 * @param timeStyle
 * @hide draft / provisional / internal are hidden on Android
 */
protected DateFormat guessDateFormat(int dateStyle, int timeStyle) {
    DateFormat result;
    ULocale dfLocale = getAvailableLocale(TYPE_DATEFORMAT);
    if (dfLocale == null) {
        dfLocale = ULocale.ROOT;
    }
    if (timeStyle == DF_NONE) {
        result = DateFormat.getDateInstance(getCalendar(), dateStyle, dfLocale);
    } else if (dateStyle == DF_NONE) {
        result = DateFormat.getTimeInstance(getCalendar(), timeStyle, dfLocale);
    } else {
        result = DateFormat.getDateTimeInstance(getCalendar(), dateStyle, timeStyle, dfLocale);
    }
    return result;
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat)

Example 47 with DateFormat

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

the class NumberFormatRegressionTest method TestJ691.

/**
 * DateFormat should call setIntegerParseOnly(TRUE) on adopted
 * NumberFormat objects.
 */
@Test
public void TestJ691() {
    Locale loc = new Locale("fr", "CH");
    // set up the input date string & expected output
    String udt = "11.10.2000";
    String exp = "11.10.00";
    // create a Calendar for this locale
    Calendar cal = Calendar.getInstance(loc);
    // create a NumberFormat for this locale
    NumberFormat nf = NumberFormat.getInstance(loc);
    // *** Here's the key: We don't want to have to do THIS:
    // nf.setParseIntegerOnly(true);
    // create the DateFormat
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, loc);
    df.setCalendar(cal);
    df.setNumberFormat(nf);
    // set parsing to lenient & parse
    Date ulocdat = new Date();
    df.setLenient(true);
    try {
        ulocdat = df.parse(udt);
    } catch (java.text.ParseException pe) {
        errln(pe.getMessage());
    }
    // format back to a string
    String outString = df.format(ulocdat);
    if (!outString.equals(exp)) {
        errln("FAIL: " + udt + " => " + outString);
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) Calendar(android.icu.util.Calendar) DateFormat(android.icu.text.DateFormat) ParseException(java.text.ParseException) Date(java.util.Date) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 48 with DateFormat

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

the class LocaleAliasTest method TestDateFormat.

@Test
public void TestDateFormat() {
    ULocale defLoc = ULocale.getDefault();
    ULocale.setDefault(_DEFAULT_LOCALE);
    for (int i = 0; i < _LOCALE_NUMBER; i++) {
        ULocale oldLoc = _LOCALES[i][0];
        ULocale newLoc = _LOCALES[i][1];
        if (availableMap.get(_LOCALES[i][1]) == null) {
            logln(_LOCALES[i][1] + " is not available. Skipping!");
            continue;
        }
        DateFormat df1 = DateFormat.getDateInstance(DateFormat.FULL, oldLoc);
        DateFormat df2 = DateFormat.getDateInstance(DateFormat.FULL, newLoc);
        // Test function "getLocale"
        ULocale l1 = df1.getLocale(ULocale.VALID_LOCALE);
        ULocale l2 = df2.getLocale(ULocale.VALID_LOCALE);
        if (!newLoc.equals(l1)) {
            errln("DateFormatTest: newLoc!=l1: newLoc= " + newLoc + " l1= " + l1);
        }
        if (!l1.equals(l2)) {
            errln("DateFormatTest: l1!=l2: l1= " + l1 + " l2= " + l2);
        }
        if (!df1.equals(df2)) {
            errln("DateFormatTest: df1!=df2: newLoc= " + newLoc + " oldLoc= " + oldLoc);
        }
        TestFmwk.logln("DateFormat(getLocale) old:" + l1 + "   new:" + l2);
    // Test function "format"
    // Date d = new Date();
    // String d1 = df1.format(d);
    // String d2 = df2.format(d);
    // if (!d1.equals(d2)) {
    // pass = false;
    // }
    // this.logln("DateFormat(format) old:"+d1+"   new:"+d2);
    }
    ULocale.setDefault(defLoc);
}
Also used : ULocale(android.icu.util.ULocale) DateFormat(android.icu.text.DateFormat) Test(org.junit.Test)

Example 49 with DateFormat

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

the class TestMessageFormat method TestDateFormatHashCode.

public void TestDateFormatHashCode() {
    DateFormat testDF = DateFormat.getDateInstance(DateFormat.DEFAULT, ULocale.GERMAN);
    NumberFormat testNF = testDF.getNumberFormat();
    int expectedResult = testNF.getMaximumIntegerDigits() * 37 + testNF.getMaximumFractionDigits();
    int actualHashResult = testDF.hashCode();
    assertEquals("DateFormat hashCode", expectedResult, actualHashResult);
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) NumberFormat(android.icu.text.NumberFormat)

Example 50 with DateFormat

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

the class DateView method updateClock.

protected void updateClock() {
    if (mDateFormat == null) {
        final Locale l = Locale.getDefault();
        DateFormat format = DateFormat.getInstanceForSkeleton(mDatePattern, l);
        format.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE);
        mDateFormat = format;
    }
    mCurrentTime.setTime(System.currentTimeMillis());
    final String text = mDateFormat.format(mCurrentTime);
    if (!text.equals(mLastText)) {
        setText(text);
        mLastText = text;
    }
}
Also used : Locale(java.util.Locale) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat)

Aggregations

DateFormat (android.icu.text.DateFormat)97 SimpleDateFormat (android.icu.text.SimpleDateFormat)80 Test (org.junit.Test)78 Date (java.util.Date)67 GregorianCalendar (android.icu.util.GregorianCalendar)44 ChineseDateFormat (android.icu.text.ChineseDateFormat)37 Calendar (android.icu.util.Calendar)37 ULocale (android.icu.util.ULocale)33 JapaneseCalendar (android.icu.util.JapaneseCalendar)27 IslamicCalendar (android.icu.util.IslamicCalendar)23 ChineseCalendar (android.icu.util.ChineseCalendar)22 ParseException (java.text.ParseException)21 BuddhistCalendar (android.icu.util.BuddhistCalendar)19 TimeZone (android.icu.util.TimeZone)19 HebrewCalendar (android.icu.util.HebrewCalendar)17 Locale (java.util.Locale)17 ParsePosition (java.text.ParsePosition)15 FieldPosition (java.text.FieldPosition)13 IOException (java.io.IOException)10 SimpleTimeZone (android.icu.util.SimpleTimeZone)7