Search in sources :

Example 16 with IslamicCalendar

use of android.icu.util.IslamicCalendar 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)

Example 17 with IslamicCalendar

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

the class GlobalizationPreferencesTest method TestCalendar.

@Test
public void TestCalendar() {
    GlobalizationPreferences gp = new GlobalizationPreferences();
    // Set locale - pt_BR
    logln("Set locale - pt");
    gp.setLocale(new ULocale("pt"));
    Calendar cal = gp.getCalendar();
    String calType = cal.getType();
    if (!calType.equals("gregorian")) {
        errln("FAIL: Calendar type is " + calType + " Expected: gregorian");
    }
    // Set a list of locales
    logln("Set locales - en, en_JP, en_GB");
    ULocale[] locales = new ULocale[3];
    locales[0] = new ULocale("en");
    locales[1] = new ULocale("en_JP");
    locales[2] = new ULocale("en_GB");
    gp.setLocales(locales);
    cal = gp.getCalendar();
    ULocale calLocale = cal.getLocale(ULocale.VALID_LOCALE);
    if (!calLocale.equals(locales[2])) {
        errln("FAIL: Calendar locale is " + calLocale.toString() + " - Expected: en_GB");
    }
    // Set ecplicit calendar
    logln("Set Japanese calendar to this object");
    JapaneseCalendar jcal = new JapaneseCalendar();
    gp.setCalendar(jcal);
    cal = gp.getCalendar();
    calType = cal.getType();
    if (!calType.equals("japanese")) {
        errln("FAIL: Calendar type is " + calType + " Expected: japanese");
    }
    jcal.setFirstDayOfWeek(3);
    if (cal.getFirstDayOfWeek() == jcal.getFirstDayOfWeek()) {
        errln("FAIL: Calendar returned by getCalendar must be a safe copy");
    }
    cal.setFirstDayOfWeek(3);
    Calendar cal1 = gp.getCalendar();
    if (cal1.getFirstDayOfWeek() == cal.getFirstDayOfWeek()) {
        errln("FAIL: Calendar returned by getCalendar must be a safe copy");
    }
    // Freeze
    logln("Freeze this object");
    IslamicCalendar ical = new IslamicCalendar();
    boolean bFrozen = false;
    gp.freeze();
    try {
        gp.setCalendar(ical);
    } catch (UnsupportedOperationException uoe) {
        logln("setCalendar is blocked");
        bFrozen = true;
    }
    if (!bFrozen) {
        errln("FAIL: setCalendar must be blocked");
    }
    // Safe clone
    logln("cloneAsThawed");
    GlobalizationPreferences gp1 = (GlobalizationPreferences) gp.cloneAsThawed();
    cal = gp.getCalendar();
    calType = cal.getType();
    if (!calType.equals("japanese")) {
        errln("FAIL: Calendar type afte clone is " + calType + " Expected: japanese");
    }
    logln("Set islamic calendar");
    gp1.setCalendar(ical);
    cal = gp1.getCalendar();
    calType = cal.getType();
    if (!calType.equals("islamic-civil")) {
        // default constructed IslamicCalendar is islamic-civil
        errln("FAIL: Calendar type afte clone is " + calType + " Expected: islamic-civil");
    }
}
Also used : GlobalizationPreferences(android.icu.util.GlobalizationPreferences) ULocale(android.icu.util.ULocale) IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) BuddhistCalendar(android.icu.util.BuddhistCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) IslamicCalendar(android.icu.util.IslamicCalendar) Test(org.junit.Test)

Aggregations

IslamicCalendar (android.icu.util.IslamicCalendar)17 Test (org.junit.Test)14 GregorianCalendar (android.icu.util.GregorianCalendar)7 ULocale (android.icu.util.ULocale)7 SimpleDateFormat (android.icu.text.SimpleDateFormat)6 Calendar (android.icu.util.Calendar)5 JapaneseCalendar (android.icu.util.JapaneseCalendar)4 Date (java.util.Date)4 DateFormat (android.icu.text.DateFormat)3 BuddhistCalendar (android.icu.util.BuddhistCalendar)3 TimeZone (android.icu.util.TimeZone)3 IOException (java.io.IOException)3 GlobalizationPreferences (android.icu.util.GlobalizationPreferences)2 Locale (java.util.Locale)2 ChineseDateFormat (android.icu.text.ChineseDateFormat)1 ChineseCalendar (android.icu.util.ChineseCalendar)1 HebrewCalendar (android.icu.util.HebrewCalendar)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1