Search in sources :

Example 76 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class CalendarRegressionTest method TestDateFormatFactoryJ26.

/**
 * DateFormat class mistakes date style and time style as follows: -
 * DateFormat.getDateTimeInstance takes date style as time style, and time
 * style as date style - If a Calendar is passed to
 * DateFormat.getDateInstance, it returns time instance - If a Calendar is
 * passed to DateFormat.getTimeInstance, it returns date instance
 */
@Test
public void TestDateFormatFactoryJ26() {
    TimeZone zone = TimeZone.getDefault();
    try {
        Locale loc = Locale.US;
        TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
        java.util.Calendar tempcal = java.util.Calendar.getInstance();
        tempcal.set(2001, Calendar.APRIL, 5, 17, 43, 53);
        Date date = tempcal.getTime();
        Calendar cal = Calendar.getInstance(loc);
        Object[] DATA = { DateFormat.getDateInstance(DateFormat.SHORT, loc), "DateFormat.getDateInstance(DateFormat.SHORT, loc)", "4/5/01", DateFormat.getTimeInstance(DateFormat.SHORT, loc), "DateFormat.getTimeInstance(DateFormat.SHORT, loc)", "5:43 PM", DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc), "DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc)", "Thursday, April 5, 2001 at 5:43 PM", DateFormat.getDateInstance(cal, DateFormat.SHORT, loc), "DateFormat.getDateInstance(cal, DateFormat.SHORT, loc)", "4/5/01", DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc), "DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc)", "5:43 PM", DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc), "DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc)", "Thursday, April 5, 2001 at 5:43 PM", cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc), "cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc)", "4/5/01, 5:43:53 PM Pacific Daylight Time", cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc), "cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc)", "Thursday, April 5, 2001 at 5:43 PM" };
        for (int i = 0; i < DATA.length; i += 3) {
            DateFormat df = (DateFormat) DATA[i];
            String desc = (String) DATA[i + 1];
            String exp = (String) DATA[i + 2];
            String got = df.format(date);
            if (got.equals(exp)) {
                logln("Ok: " + desc + " => " + got);
            } else {
                errln("FAIL: " + desc + " => " + got + ", expected " + exp);
            }
        }
    } finally {
        TimeZone.setDefault(zone);
    }
}
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) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 77 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class CalendarRegressionTest method TestT9452.

/**
 * Test case for ticket 9452
 * Calendar addition fall onto the missing date - 2011-12-30 in Samoa
 */
@Test
public void TestT9452() {
    TimeZone samoaTZ = TimeZone.getTimeZone("Pacific/Apia");
    GregorianCalendar cal = new GregorianCalendar(samoaTZ);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
    sdf.setTimeZone(samoaTZ);
    // Set date to 2011-12-29 00:00
    cal.clear();
    cal.set(2011, Calendar.DECEMBER, 29, 0, 0, 0);
    Date d = cal.getTime();
    String dstr = sdf.format(d);
    logln("Initial date: " + dstr);
    // Add 1 day
    cal.add(Calendar.DATE, 1);
    d = cal.getTime();
    dstr = sdf.format(d);
    logln("+1 day: " + dstr);
    assertEquals("Add 1 day", "2011-12-31T00:00:00+14:00", dstr);
    // Subtract 1 day
    cal.add(Calendar.DATE, -1);
    d = cal.getTime();
    dstr = sdf.format(d);
    logln("-1 day: " + dstr);
    assertEquals("Subtract 1 day", "2011-12-29T00:00:00-10:00", dstr);
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 78 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class DateFormatTest method TestCoverage.

@Test
public void TestCoverage() {
    Date now = new Date();
    Calendar cal = new GregorianCalendar();
    DateFormat f = DateFormat.getTimeInstance();
    logln("time: " + f.format(now));
    // sigh, everyone overrides this
    int hash = f.hashCode();
    f = DateFormat.getInstance(cal);
    if (hash == f.hashCode()) {
        errln("FAIL: hashCode equal for inequal objects");
    }
    logln("time again: " + f.format(now));
    f = DateFormat.getTimeInstance(cal, DateFormat.FULL);
    logln("time yet again: " + f.format(now));
    f = DateFormat.getDateInstance();
    logln("time yet again: " + f.format(now));
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "de_DE");
    DateFormatSymbols sym = new DateFormatSymbols(rb, Locale.GERMANY);
    DateFormatSymbols sym2 = (DateFormatSymbols) sym.clone();
    if (sym.hashCode() != sym2.hashCode()) {
        errln("fail, date format symbols hashcode not equal");
    }
    if (!sym.equals(sym2)) {
        errln("fail, date format symbols not equal");
    }
    Locale foo = new Locale("fu", "FU", "BAR");
    rb = null;
    sym = new DateFormatSymbols(GregorianCalendar.class, foo);
    sym.equals(null);
    sym = new ChineseDateFormatSymbols();
    sym = new ChineseDateFormatSymbols(new Locale("en_US"));
    try {
        sym = new ChineseDateFormatSymbols(null, new Locale("en_US"));
        errln("ChineseDateFormatSymbols(Calender, Locale) was suppose to return a null " + "pointer exception for a null paramater.");
    } catch (Exception e) {
    }
    sym = new ChineseDateFormatSymbols(new ChineseCalendar(), new Locale("en_US"));
    try {
        sym = new ChineseDateFormatSymbols(null, new ULocale("en_US"));
        errln("ChineseDateFormatSymbols(Calender, ULocale) was suppose to return a null " + "pointer exception for a null paramater.");
    } catch (Exception e) {
    }
    sym = new ChineseDateFormatSymbols(new ChineseCalendar(), foo);
    // cover new ChineseDateFormatSymbols(Calendar, ULocale)
    ChineseCalendar ccal = new ChineseCalendar();
    // gclsh1 add
    sym = new ChineseDateFormatSymbols(ccal, ULocale.CHINA);
    StringBuffer buf = new StringBuffer();
    FieldPosition pos = new FieldPosition(0);
    f.format((Object) cal, buf, pos);
    f.format((Object) now, buf, pos);
    f.format((Object) new Long(now.getTime()), buf, pos);
    try {
        f.format((Object) "Howdy", buf, pos);
    } catch (Exception e) {
    }
    NumberFormat nf = f.getNumberFormat();
    f.setNumberFormat(nf);
    boolean lenient = f.isLenient();
    f.setLenient(lenient);
    ULocale uloc = f.getLocale(ULocale.ACTUAL_LOCALE);
    DateFormat sdfmt = new SimpleDateFormat();
    if (f.hashCode() != f.hashCode()) {
        errln("hashCode is not stable");
    }
    if (!f.equals(f)) {
        errln("f != f");
    }
    if (f.equals(null)) {
        errln("f should not equal null");
    }
    if (f.equals(sdfmt)) {
        errln("A time instance shouldn't equal a default date format");
    }
    Date d;
    {
        ChineseDateFormat fmt = new ChineseDateFormat("yymm", Locale.US);
        try {
            // fewer symbols than required 2
            fmt.parse("2");
            errln("whoops");
        } catch (ParseException e) {
            logln("ok");
        }
        try {
            // should succeed with obeycount
            fmt.parse("2255");
            logln("ok");
        } catch (ParseException e) {
            errln("whoops");
        }
        try {
            // not a number, should fail
            fmt.parse("ni hao");
            errln("whoops ni hao");
        } catch (ParseException e) {
            logln("ok ni hao");
        }
    }
    {
        Calendar xcal = new GregorianCalendar();
        xcal.set(Calendar.HOUR_OF_DAY, 0);
        DateFormat fmt = new SimpleDateFormat("k");
        StringBuffer xbuf = new StringBuffer();
        FieldPosition fpos = new FieldPosition(Calendar.HOUR_OF_DAY);
        fmt.format(xcal, xbuf, fpos);
        try {
            fmt.parse(xbuf.toString());
            logln("ok");
            xbuf.setLength(0);
            xcal.set(Calendar.HOUR_OF_DAY, 25);
            fmt.format(xcal, xbuf, fpos);
            Date d2 = fmt.parse(xbuf.toString());
            logln("ok again - d2=" + d2);
        } catch (ParseException e) {
            errln("whoops");
        }
    }
    {
        // cover gmt+hh:mm
        DateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
        try {
            d = fmt.parse("07/10/53 GMT+10:00");
            logln("ok : d = " + d);
        } catch (ParseException e) {
            errln("Parse of 07/10/53 GMT+10:00 for pattern MM/dd/yy z");
        }
        // cover invalid separator after GMT
        {
            ParsePosition pp = new ParsePosition(0);
            String text = "07/10/53 GMT=10:00";
            d = fmt.parse(text, pp);
            if (pp.getIndex() != 12) {
                errln("Parse of 07/10/53 GMT=10:00 for pattern MM/dd/yy z");
            }
            logln("Parsing of the text stopped at pos: " + pp.getIndex() + " as expected and length is " + text.length());
        }
        // cover bad text after GMT+.
        try {
            fmt.parse("07/10/53 GMT+blecch");
            logln("ok GMT+blecch");
        } catch (ParseException e) {
            errln("whoops GMT+blecch");
        }
        // cover bad text after GMT+hh:.
        try {
            fmt.parse("07/10/53 GMT+07:blecch");
            logln("ok GMT+xx:blecch");
        } catch (ParseException e) {
            errln("whoops GMT+xx:blecch");
        }
        // cover no ':' GMT+#, # < 24 (hh)
        try {
            d = fmt.parse("07/10/53 GMT+07");
            logln("ok GMT+07");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 GMT+07 for pattern MM/dd/yy z");
        }
        // cover no ':' GMT+#, # > 24 (hhmm)
        try {
            d = fmt.parse("07/10/53 GMT+0730");
            logln("ok");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 GMT+0730 for pattern MM/dd/yy z");
        }
        // cover GMT+#, # with second field
        try {
            d = fmt.parse("07/10/53 GMT+07:30:15");
            logln("ok GMT+07:30:15");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 GMT+07:30:15 for pattern MM/dd/yy z");
        }
        // cover no ':' GMT+#, # with second field, no leading zero
        try {
            d = fmt.parse("07/10/53 GMT+73015");
            logln("ok GMT+73015");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 GMT+73015 for pattern MM/dd/yy z");
        }
        // cover no ':' GMT+#, # with 1 digit second field
        try {
            d = fmt.parse("07/10/53 GMT+07300");
            logln("ok GMT+07300");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 GMT+07300 for pattern MM/dd/yy z");
        }
        // cover raw digits with no leading sign (bad RFC822)
        try {
            d = fmt.parse("07/10/53 07");
            errln("Parse of 07/10/53 07 for pattern MM/dd/yy z passed!");
        } catch (ParseException e) {
            logln("ok");
        }
        // cover raw digits (RFC822)
        try {
            d = fmt.parse("07/10/53 +07");
            logln("ok");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 +07 for pattern MM/dd/yy z failed");
        }
        // cover raw digits (RFC822)
        try {
            d = fmt.parse("07/10/53 -0730");
            logln("ok");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 -00730 for pattern MM/dd/yy z failed");
        }
        // cover raw digits (RFC822) in DST
        try {
            fmt.setTimeZone(TimeZone.getTimeZone("PDT"));
            d = fmt.parse("07/10/53 -0730");
            logln("ok");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 -0730 for pattern MM/dd/yy z failed");
        }
    }
    // TODO: revisit toLocalizedPattern
    if (false) {
        SimpleDateFormat fmt = new SimpleDateFormat("aabbcc");
        try {
            String pat = fmt.toLocalizedPattern();
            errln("whoops, shouldn't have been able to localize aabbcc");
        } catch (IllegalArgumentException e) {
            logln("aabbcc localize ok");
        }
    }
    {
        SimpleDateFormat fmt = new SimpleDateFormat("'aabbcc");
        try {
            fmt.toLocalizedPattern();
            errln("whoops, localize unclosed quote");
        } catch (IllegalArgumentException e) {
            logln("localize unclosed quote ok");
        }
    }
    {
        SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
        // bogus time zone
        String text = "08/15/58 DBDY";
        try {
            fmt.parse(text);
            errln("recognized bogus time zone DBDY");
        } catch (ParseException e) {
            logln("time zone ex ok");
        }
    }
    {
        // force fallback to default timezone when fmt timezone
        // is not named
        SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
        // force fallback to default time zone, still fails
        // not in equivalency group
        fmt.setTimeZone(TimeZone.getTimeZone("GMT+0147"));
        String text = "08/15/58 DBDY";
        try {
            fmt.parse(text);
            errln("Parse of 07/10/53 DBDY for pattern MM/dd/yy z passed");
        } catch (ParseException e) {
            logln("time zone ex2 ok");
        }
        // force success on fallback
        text = "08/15/58 " + TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT);
        try {
            fmt.parse(text);
            logln("found default tz");
        } catch (ParseException e) {
            errln("whoops, got parse exception");
        }
    }
    {
        // force fallback to symbols list of timezones when neither
        // fmt and default timezone is named
        SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
        TimeZone oldtz = TimeZone.getDefault();
        // nonstandard tz
        TimeZone newtz = TimeZone.getTimeZone("GMT+0137");
        fmt.setTimeZone(newtz);
        // todo: fix security issue
        TimeZone.setDefault(newtz);
        // fallback to symbol list, but fail
        // try to parse the bogus time zone
        String text = "08/15/58 DBDY";
        try {
            fmt.parse(text);
            errln("Parse of 07/10/53 DBDY for pattern MM/dd/yy z passed");
        } catch (ParseException e) {
            logln("time zone ex3 ok");
        } catch (Exception e) {
            // hmmm... this shouldn't happen.  don't want to exit this
            // fn with timezone improperly set, so just in case
            TimeZone.setDefault(oldtz);
            throw new IllegalStateException(e.getMessage());
        }
    }
    {
        // cover getAvailableULocales
        final ULocale[] locales = DateFormat.getAvailableULocales();
        long count = locales.length;
        if (count == 0) {
            errln(" got a empty list for getAvailableULocales");
        } else {
            logln("" + count + " available ulocales");
        }
    }
    {
        // cover DateFormatSymbols.getDateFormatBundle
        cal = new GregorianCalendar();
        Locale loc = Locale.getDefault();
        DateFormatSymbols mysym = new DateFormatSymbols(cal, loc);
        if (mysym == null)
            errln("FAIL: constructs DateFormatSymbols with calendar and locale failed");
        uloc = ULocale.getDefault();
        // These APIs are obsolete and return null
        ResourceBundle resb = DateFormatSymbols.getDateFormatBundle(cal, loc);
        ResourceBundle resb2 = DateFormatSymbols.getDateFormatBundle(cal, uloc);
        ResourceBundle resb3 = DateFormatSymbols.getDateFormatBundle(cal.getClass(), loc);
        ResourceBundle resb4 = DateFormatSymbols.getDateFormatBundle(cal.getClass(), uloc);
        if (resb != null) {
            logln("resb is not null");
        }
        if (resb2 != null) {
            logln("resb2 is not null");
        }
        if (resb3 != null) {
            logln("resb3 is not null");
        }
        if (resb4 != null) {
            logln("resb4 is not null");
        }
    }
    {
        // cover DateFormatSymbols.getInstance
        DateFormatSymbols datsym1 = DateFormatSymbols.getInstance();
        DateFormatSymbols datsym2 = new DateFormatSymbols();
        if (!datsym1.equals(datsym2)) {
            errln("FAIL: DateFormatSymbols returned by getInstance()" + "does not match new DateFormatSymbols().");
        }
        datsym1 = DateFormatSymbols.getInstance(Locale.JAPAN);
        datsym2 = DateFormatSymbols.getInstance(ULocale.JAPAN);
        if (!datsym1.equals(datsym2)) {
            errln("FAIL: DateFormatSymbols returned by getInstance(Locale.JAPAN)" + "does not match the one returned by getInstance(ULocale.JAPAN).");
        }
    }
    {
        // cover DateFormatSymbols.getAvailableLocales/getAvailableULocales
        Locale[] allLocales = DateFormatSymbols.getAvailableLocales();
        if (allLocales.length == 0) {
            errln("FAIL: Got a empty list for DateFormatSymbols.getAvailableLocales");
        } else {
            logln("PASS: " + allLocales.length + " available locales returned by DateFormatSymbols.getAvailableLocales");
        }
        ULocale[] allULocales = DateFormatSymbols.getAvailableULocales();
        if (allULocales.length == 0) {
            errln("FAIL: Got a empty list for DateFormatSymbols.getAvailableLocales");
        } else {
            logln("PASS: " + allULocales.length + " available locales returned by DateFormatSymbols.getAvailableULocales");
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) ChineseDateFormatSymbols(android.icu.text.ChineseDateFormatSymbols) ParsePosition(java.text.ParsePosition) ChineseCalendar(android.icu.util.ChineseCalendar) ULocale(android.icu.util.ULocale) BuddhistCalendar(android.icu.util.BuddhistCalendar) HebrewCalendar(android.icu.util.HebrewCalendar) IslamicCalendar(android.icu.util.IslamicCalendar) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) ICUResourceBundle(android.icu.impl.ICUResourceBundle) FieldPosition(java.text.FieldPosition) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException) TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormatSymbols(android.icu.text.DateFormatSymbols) ChineseDateFormatSymbols(android.icu.text.ChineseDateFormatSymbols) ICUResourceBundle(android.icu.impl.ICUResourceBundle) UResourceBundle(android.icu.util.UResourceBundle) ResourceBundle(java.util.ResourceBundle) ParseException(java.text.ParseException) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 79 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class DateFormatTest method TestFieldPosition.

/**
 * Verify that returned field position indices are correct.
 */
@Test
public void TestFieldPosition() {
    int i, j, exp;
    StringBuffer buf = new StringBuffer();
    // Verify data
    if (VersionInfo.ICU_VERSION.compareTo(VersionInfo.getInstance(3, 7)) >= 0) {
        DateFormatSymbols rootSyms = new DateFormatSymbols(new Locale("", "", ""));
        assertEquals("patternChars", PATTERN_CHARS, rootSyms.getLocalPatternChars());
    }
    assertTrue("DATEFORMAT_FIELD_NAMES", DATEFORMAT_FIELD_NAMES.length == DateFormat.FIELD_COUNT);
    if (DateFormat.FIELD_COUNT != PATTERN_CHARS.length() + 1) {
        // +1 for missing TIME_SEPARATOR pattern char
        errln("Did not get the correct value for DateFormat.FIELD_COUNT. Expected:  " + PATTERN_CHARS.length() + 1);
    }
    // Create test formatters
    final int COUNT = 4;
    DateFormat[] dateFormats = new DateFormat[COUNT];
    dateFormats[0] = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);
    dateFormats[1] = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.FRANCE);
    // Make the pattern "G y M d..."
    buf.append(PATTERN_CHARS);
    for (j = buf.length() - 1; j >= 0; --j) buf.insert(j, ' ');
    dateFormats[2] = new SimpleDateFormat(buf.toString(), Locale.US);
    // Make the pattern "GGGG yyyy MMMM dddd..."
    for (j = buf.length() - 1; j >= 0; j -= 2) {
        for (i = 0; i < 3; ++i) {
            buf.insert(j, buf.charAt(j));
        }
    }
    dateFormats[3] = new SimpleDateFormat(buf.toString(), Locale.US);
    Date aug13 = new Date((long) 871508052513.0);
    // Expected output field values for above DateFormats on aug13
    // Fields are given in order of DateFormat field number
    final String[] EXPECTED = { "", "1997", "August", "13", "", "", "34", "12", "", "Wednesday", "", "", "", "", "PM", "2", "", "Pacific Daylight Time", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "1997", "ao\u00FBt", "13", "", "14", "34", "12", "", "mercredi", "", "", "", "", "", "", "", "heure d\u2019\u00E9t\u00E9 du Pacifique", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "AD", "1997", "8", "13", "14", "14", "34", "12", "5", "Wed", "225", "2", "33", "3", "PM", "2", "2", "PDT", "1997", "4", "1997", "2450674", "52452513", "-0700", "PT", "4", "8", "3", "3", "uslax", "1997", "GMT-7", "-07", "-07", "1997", "PM", "in the afternoon", "", "Anno Domini", "1997", "August", "0013", "0014", "0014", "0034", "0012", "5130", "Wednesday", "0225", "0002", "0033", "0003", "PM", "0002", "0002", "Pacific Daylight Time", "1997", "Wednesday", "1997", "2450674", "52452513", "GMT-07:00", "Pacific Time", "Wednesday", "August", "3rd quarter", "3rd quarter", "Los Angeles Time", "1997", "GMT-07:00", "-0700", "-0700", "1997", "PM", "in the afternoon", "" };
    assertTrue("data size", EXPECTED.length == COUNT * DateFormat.FIELD_COUNT);
    final DateFormat.Field[] DTFMT_FIELDS = { DateFormat.Field.AM_PM, DateFormat.Field.DAY_OF_MONTH, DateFormat.Field.DAY_OF_WEEK, DateFormat.Field.DAY_OF_WEEK_IN_MONTH, DateFormat.Field.DAY_OF_YEAR, DateFormat.Field.DOW_LOCAL, DateFormat.Field.ERA, DateFormat.Field.EXTENDED_YEAR, DateFormat.Field.HOUR_OF_DAY0, DateFormat.Field.HOUR_OF_DAY1, DateFormat.Field.HOUR0, DateFormat.Field.HOUR1, DateFormat.Field.JULIAN_DAY, DateFormat.Field.MILLISECOND, DateFormat.Field.MILLISECONDS_IN_DAY, DateFormat.Field.MINUTE, DateFormat.Field.MONTH, DateFormat.Field.QUARTER, DateFormat.Field.SECOND, DateFormat.Field.TIME_ZONE, DateFormat.Field.WEEK_OF_MONTH, DateFormat.Field.WEEK_OF_YEAR, DateFormat.Field.YEAR, DateFormat.Field.YEAR_WOY };
    final String[][] EXPECTED_BY_FIELD = { { "PM", "13", "Wednesday", "", "", "", "", "", "", "", "", "2", "", "", "", "34", "August", "", "12", "Pacific Daylight Time", "", "", "1997", "" }, { "", "13", "mercredi", "", "", "", "", "", "14", "", "", "", "", "", "", "34", "ao\u00FBt", "", "12", "heure d\u2019\u00E9t\u00E9 du Pacifique", "", "", "1997", "" }, { "PM", "13", "Wed", "2", "225", "4", "AD", "1997", "14", "14", "2", "2", "2450674", "5", "52452513", "34", "8", "3", "12", "PDT", "3", "33", "1997", "1997" }, { "PM", "0013", "Wednesday", "0002", "0225", "Wednesday", "Anno Domini", "1997", "0014", "0014", "0002", "0002", "2450674", "5130", "52452513", "0034", "August", "3rd quarter", "0012", "Pacific Daylight Time", "0003", "0033", "1997", "1997" } };
    TimeZone PT = TimeZone.getTimeZone("America/Los_Angeles");
    for (j = 0, exp = 0; j < COUNT; ++j) {
        // String str;
        DateFormat df = dateFormats[j];
        df.setTimeZone(PT);
        logln(" Pattern = " + ((SimpleDateFormat) df).toPattern());
        try {
            logln("  Result = " + df.format(aug13));
        } catch (Exception e) {
            errln("FAIL: " + e);
            e.printStackTrace();
            continue;
        }
        FieldPosition pos;
        String field;
        for (i = 0; i < DateFormat.FIELD_COUNT; ++i, ++exp) {
            pos = new FieldPosition(i);
            buf.setLength(0);
            df.format(aug13, buf, pos);
            field = buf.substring(pos.getBeginIndex(), pos.getEndIndex());
            assertEquals("pattern#" + j + " field #" + i + " " + DATEFORMAT_FIELD_NAMES[i], EXPECTED[exp], field);
        }
        // FieldPostion initialized by DateFormat.Field trac#6089
        for (i = 0; i < DTFMT_FIELDS.length; i++) {
            // The format method only set position for the first occurrence of
            // the specified field.
            pos = new FieldPosition(DTFMT_FIELDS[i]);
            buf.setLength(0);
            df.format(aug13, buf, pos);
            field = buf.substring(pos.getBeginIndex(), pos.getEndIndex());
            assertEquals("pattern#" + j + " " + DTFMT_FIELDS[i].toString(), EXPECTED_BY_FIELD[j][i], field);
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) FieldPosition(java.text.FieldPosition) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException) Field(android.icu.text.ChineseDateFormat.Field) TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormatSymbols(android.icu.text.DateFormatSymbols) ChineseDateFormatSymbols(android.icu.text.ChineseDateFormatSymbols) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 80 with TimeZone

use of android.icu.util.TimeZone in project j2objc by google.

the class DateFormatTest method TestFormattingLocaleTimeSeparator.

@Test
public void TestFormattingLocaleTimeSeparator() {
    // Sun Sep 14 21:52:00 CET 1997
    Date date = new Date(874266720000L);
    TimeZone tz = TimeZone.getTimeZone("CET");
    DateFormat dfArab = DateFormat.getTimeInstance(DateFormat.SHORT, new ULocale("ar"));
    DateFormat dfLatn = DateFormat.getTimeInstance(DateFormat.SHORT, new ULocale("ar-u-nu-latn"));
    dfArab.setTimeZone(tz);
    dfLatn.setTimeZone(tz);
    String expectedArab = "\u0669:\u0665\u0662 \u0645";
    String expectedLatn = "9:52 \u0645";
    String actualArab = dfArab.format(date);
    String actualLatn = dfLatn.format(date);
    if (!actualArab.equals(expectedArab)) {
        errln("FAIL: Expected " + expectedArab + " Got " + actualArab);
    }
    if (!actualLatn.equals(expectedLatn)) {
        errln("FAIL: Expected " + expectedLatn + " Got " + actualLatn);
    }
}
Also used : TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Aggregations

TimeZone (android.icu.util.TimeZone)125 Test (org.junit.Test)98 SimpleTimeZone (android.icu.util.SimpleTimeZone)76 Date (java.util.Date)63 GregorianCalendar (android.icu.util.GregorianCalendar)49 Calendar (android.icu.util.Calendar)42 BasicTimeZone (android.icu.util.BasicTimeZone)41 SimpleDateFormat (android.icu.text.SimpleDateFormat)36 ULocale (android.icu.util.ULocale)31 RuleBasedTimeZone (android.icu.util.RuleBasedTimeZone)24 VTimeZone (android.icu.util.VTimeZone)23 DateFormat (android.icu.text.DateFormat)19 JavaTimeZone (android.icu.impl.JavaTimeZone)18 NativeTimeZone (com.google.j2objc.util.NativeTimeZone)18 JapaneseCalendar (android.icu.util.JapaneseCalendar)15 FieldPosition (java.text.FieldPosition)15 BuddhistCalendar (android.icu.util.BuddhistCalendar)13 ParsePosition (java.text.ParsePosition)13 ChineseCalendar (android.icu.util.ChineseCalendar)12 ParseException (java.text.ParseException)12