Search in sources :

Example 11 with HebrewCalendar

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

the class DateFormatTest method TestRoundtripWithCalendar.

/*
     * Test for format/parse method with calendar which is different
     * from what DateFormat instance internally use.  See ticket#6420.
     */
@Test
public void TestRoundtripWithCalendar() {
    TimeZone tz = TimeZone.getTimeZone("Europe/Paris");
    TimeZone gmt = TimeZone.getTimeZone("Etc/GMT");
    final Calendar[] calendars = { new GregorianCalendar(tz), new BuddhistCalendar(tz), new HebrewCalendar(tz), new IslamicCalendar(tz), new JapaneseCalendar(tz) };
    final String pattern = "GyMMMMdEEEEHHmmssVVVV";
    // FIXME The formatters commented out below are currently failing because of
    // the calendar calculation problem reported by #6691
    // The order of test formatters mus match the order of calendars above.
    final DateFormat[] formatters = { // calendar=gregorian
    DateFormat.getPatternInstance(pattern, new ULocale("en_US")), // calendar=buddhist
    DateFormat.getPatternInstance(pattern, new ULocale("th_TH")), DateFormat.getPatternInstance(pattern, new ULocale("he_IL@calendar=hebrew")) // DateFormat.getPatternInstance(pattern, new ULocale("ar_EG@calendar=islamic")),
    // DateFormat.getPatternInstance(pattern, new ULocale("ja_JP@calendar=japanese")),
    };
    Date d = new Date();
    StringBuffer buf = new StringBuffer();
    FieldPosition fpos = new FieldPosition(0);
    ParsePosition ppos = new ParsePosition(0);
    for (int i = 0; i < formatters.length; i++) {
        buf.setLength(0);
        fpos.setBeginIndex(0);
        fpos.setEndIndex(0);
        calendars[i].setTime(d);
        // Normal case output - the given calendar matches the calendar
        // used by the formatter
        formatters[i].format(calendars[i], buf, fpos);
        String refStr = buf.toString();
        for (int j = 0; j < calendars.length; j++) {
            if (j == i) {
                continue;
            }
            buf.setLength(0);
            fpos.setBeginIndex(0);
            fpos.setEndIndex(0);
            calendars[j].setTime(d);
            // Even the different calendar type is specified,
            // we should get the same result.
            formatters[i].format(calendars[j], buf, fpos);
            if (!refStr.equals(buf.toString())) {
                errln("FAIL: Different format result with a different calendar for the same time -" + "\n Reference calendar type=" + calendars[i].getType() + "\n Another calendar type=" + calendars[j].getType() + "\n Expected result=" + refStr + "\n Actual result=" + buf.toString());
            }
        }
        calendars[i].setTimeZone(gmt);
        calendars[i].clear();
        ppos.setErrorIndex(-1);
        ppos.setIndex(0);
        // Normal case parse result - the given calendar matches the calendar
        // used by the formatter
        formatters[i].parse(refStr, calendars[i], ppos);
        for (int j = 0; j < calendars.length; j++) {
            if (j == i) {
                continue;
            }
            calendars[j].setTimeZone(gmt);
            calendars[j].clear();
            ppos.setErrorIndex(-1);
            ppos.setIndex(0);
            // Even the different calendar type is specified,
            // we should get the same time and time zone.
            formatters[i].parse(refStr, calendars[j], ppos);
            if (calendars[i].getTimeInMillis() != calendars[j].getTimeInMillis() || !calendars[i].getTimeZone().equals(calendars[j].getTimeZone())) {
                errln("FAIL: Different parse result with a different calendar for the same string -" + "\n Reference calendar type=" + calendars[i].getType() + "\n Another calendar type=" + calendars[j].getType() + "\n Date string=" + refStr + "\n Expected time=" + calendars[i].getTimeInMillis() + "\n Expected time zone=" + calendars[i].getTimeZone().getID() + "\n Actual time=" + calendars[j].getTimeInMillis() + "\n Actual time zone=" + calendars[j].getTimeZone().getID());
            }
        }
    }
}
Also used : 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) FieldPosition(java.text.FieldPosition) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) HebrewCalendar(android.icu.util.HebrewCalendar) TimeZone(android.icu.util.TimeZone) BuddhistCalendar(android.icu.util.BuddhistCalendar) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) IslamicCalendar(android.icu.util.IslamicCalendar) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Aggregations

HebrewCalendar (android.icu.util.HebrewCalendar)11 Test (org.junit.Test)11 MissingResourceException (java.util.MissingResourceException)6 DateFormat (android.icu.text.DateFormat)2 Calendar (android.icu.util.Calendar)2 ChineseDateFormat (android.icu.text.ChineseDateFormat)1 SimpleDateFormat (android.icu.text.SimpleDateFormat)1 BuddhistCalendar (android.icu.util.BuddhistCalendar)1 ChineseCalendar (android.icu.util.ChineseCalendar)1 GregorianCalendar (android.icu.util.GregorianCalendar)1 IslamicCalendar (android.icu.util.IslamicCalendar)1 JapaneseCalendar (android.icu.util.JapaneseCalendar)1 TimeZone (android.icu.util.TimeZone)1 ULocale (android.icu.util.ULocale)1 FieldPosition (java.text.FieldPosition)1 ParsePosition (java.text.ParsePosition)1 Date (java.util.Date)1