Search in sources :

Example 86 with DateFormat

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

the class DateFormatTest method TestDateFormatCalendar.

/**
 * Test DateFormat(Calendar) API
 */
@Test
public void TestDateFormatCalendar() {
    DateFormat date = null, time = null, full = null;
    Calendar cal = null;
    ParsePosition pos = new ParsePosition(0);
    String str;
    Date when;
    /* Create a formatter for date fields. */
    date = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
    if (date == null) {
        errln("FAIL: getDateInstance failed");
        return;
    }
    /* Create a formatter for time fields. */
    time = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US);
    if (time == null) {
        errln("FAIL: getTimeInstance failed");
        return;
    }
    /* Create a full format for output */
    full = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);
    if (full == null) {
        errln("FAIL: getInstance failed");
        return;
    }
    /* Create a calendar */
    cal = Calendar.getInstance(Locale.US);
    if (cal == null) {
        errln("FAIL: Calendar.getInstance failed");
        return;
    }
    /* Parse the date */
    cal.clear();
    str = "4/5/2001";
    pos.setIndex(0);
    date.parse(str, cal, pos);
    if (pos.getIndex() != str.length()) {
        errln("FAIL: DateFormat.parse(4/5/2001) failed at " + pos.getIndex());
        return;
    }
    /* Parse the time */
    str = "5:45 PM";
    pos.setIndex(0);
    time.parse(str, cal, pos);
    if (pos.getIndex() != str.length()) {
        errln("FAIL: DateFormat.parse(17:45) failed at " + pos.getIndex());
        return;
    }
    /* Check result */
    when = cal.getTime();
    str = full.format(when);
    // Thursday, April 5, 2001 5:45:00 PM PDT 986517900000
    if (when.getTime() == 986517900000.0) {
        logln("Ok: Parsed result: " + str);
    } else {
        errln("FAIL: Parsed result: " + str + ", exp 4/5/2001 5:45 PM");
    }
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) 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) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 87 with DateFormat

use of android.icu.text.DateFormat 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 88 with DateFormat

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

the class DateFormatTest method TestDateFormatZone146.

/**
 * Test the formatting of time zones.
 */
@Test
public void TestDateFormatZone146() {
    TimeZone saveDefault = TimeZone.getDefault();
    // try {
    TimeZone thedefault = TimeZone.getTimeZone("GMT");
    TimeZone.setDefault(thedefault);
    // java.util.Locale.setDefault(new java.util.Locale("ar", "", ""));
    // check to be sure... its GMT all right
    TimeZone testdefault = TimeZone.getDefault();
    String testtimezone = testdefault.getID();
    if (testtimezone.equals("GMT"))
        logln("Test timezone = " + testtimezone);
    else
        errln("Test timezone should be GMT, not " + testtimezone);
    // now try to use the default GMT time zone
    GregorianCalendar greenwichcalendar = new GregorianCalendar(1997, 3, 4, 23, 0);
    // *****************************greenwichcalendar.setTimeZone(TimeZone.getDefault());
    // greenwichcalendar.set(1997, 3, 4, 23, 0);
    // try anything to set hour to 23:00 !!!
    greenwichcalendar.set(Calendar.HOUR_OF_DAY, 23);
    // get time
    Date greenwichdate = greenwichcalendar.getTime();
    // format every way
    String[] DATA = { "simple format:  ", "04/04/97 23:00 GMT", "MM/dd/yy HH:mm zzz", "full format:    ", "Friday, April 4, 1997 11:00:00 o'clock PM GMT", "EEEE, MMMM d, yyyy h:mm:ss 'o''clock' a zzz", "long format:    ", "April 4, 1997 11:00:00 PM GMT", "MMMM d, yyyy h:mm:ss a z", "default format: ", "04-Apr-97 11:00:00 PM", "dd-MMM-yy h:mm:ss a", "short format:   ", "4/4/97 11:00 PM", "M/d/yy h:mm a" };
    int DATA_length = DATA.length;
    for (int i = 0; i < DATA_length; i += 3) {
        DateFormat fmt = new SimpleDateFormat(DATA[i + 2], Locale.ENGLISH);
        fmt.setCalendar(greenwichcalendar);
        String result = fmt.format(greenwichdate);
        logln(DATA[i] + result);
        if (!result.equals(DATA[i + 1]))
            errln("FAIL: Expected " + DATA[i + 1] + ", got " + result);
    }
    // }
    // finally {
    TimeZone.setDefault(saveDefault);
// }
}
Also used : TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 89 with DateFormat

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

the class DateFormatTest method TestContext.

@Test
public void TestContext() {
    class TestContextItem {

        public String locale;

        public String pattern;

        public DisplayContext capitalizationContext;

        public String expectedFormat;

        // Simple constructor
        public TestContextItem(String loc, String pat, DisplayContext capCtxt, String expFmt) {
            locale = loc;
            pattern = pat;
            capitalizationContext = capCtxt;
            expectedFormat = expFmt;
        }
    }
    ;
    final TestContextItem[] items = { new TestContextItem("fr", "MMMM y", DisplayContext.CAPITALIZATION_NONE, "juillet 2008"), new TestContextItem("fr", "MMMM y", DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "juillet 2008"), new TestContextItem("fr", "MMMM y", DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "Juillet 2008"), new TestContextItem("fr", "MMMM y", DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU, "juillet 2008"), new TestContextItem("fr", "MMMM y", DisplayContext.CAPITALIZATION_FOR_STANDALONE, "Juillet 2008"), new TestContextItem("cs", "LLLL y", DisplayContext.CAPITALIZATION_NONE, "\u010Dervenec 2008"), new TestContextItem("cs", "LLLL y", DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "\u010Dervenec 2008"), new TestContextItem("cs", "LLLL y", DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "\u010Cervenec 2008"), new TestContextItem("cs", "LLLL y", DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU, "\u010Cervenec 2008"), new TestContextItem("cs", "LLLL y", DisplayContext.CAPITALIZATION_FOR_STANDALONE, "\u010Dervenec 2008") };
    class TestRelativeContextItem {

        public String locale;

        public DisplayContext capitalizationContext;

        public String expectedFormatToday;

        public String expectedFormatYesterday;

        // Simple constructor
        public TestRelativeContextItem(String loc, DisplayContext capCtxt, String expFmtToday, String expFmtYesterday) {
            locale = loc;
            capitalizationContext = capCtxt;
            expectedFormatToday = expFmtToday;
            expectedFormatYesterday = expFmtYesterday;
        }
    }
    ;
    final TestRelativeContextItem[] relItems = { new TestRelativeContextItem("en", DisplayContext.CAPITALIZATION_NONE, "today", "yesterday"), new TestRelativeContextItem("en", DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "today", "yesterday"), new TestRelativeContextItem("en", DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "Today", "Yesterday"), new TestRelativeContextItem("en", DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU, "Today", "Yesterday"), new TestRelativeContextItem("en", DisplayContext.CAPITALIZATION_FOR_STANDALONE, "Today", "Yesterday"), new TestRelativeContextItem("nb", DisplayContext.CAPITALIZATION_NONE, "i dag", "i g\u00E5r"), new TestRelativeContextItem("nb", DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE, "i dag", "i g\u00E5r"), new TestRelativeContextItem("nb", DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE, "I dag", "I g\u00E5r"), new TestRelativeContextItem("nb", DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU, "i dag", "i g\u00E5r"), new TestRelativeContextItem("nb", DisplayContext.CAPITALIZATION_FOR_STANDALONE, "I dag", "I g\u00E5r") };
    Calendar cal = new GregorianCalendar(2008, Calendar.JULY, 2);
    for (TestContextItem item : items) {
        ULocale locale = new ULocale(item.locale);
        SimpleDateFormat sdfmt = new SimpleDateFormat(item.pattern, locale);
        // now try context & standard format call
        sdfmt.setContext(item.capitalizationContext);
        SimpleDateFormat sdfmtClone = (SimpleDateFormat) sdfmt.clone();
        if (!sdfmtClone.equals(sdfmt)) {
            errln("FAIL: for locale " + item.locale + ", capitalizationContext " + item.capitalizationContext + ", sdfmt.clone() != sdfmt (for SimpleDateFormat)");
        }
        StringBuffer result2 = new StringBuffer();
        FieldPosition fpos2 = new FieldPosition(0);
        sdfmt.format(cal, result2, fpos2);
        if (result2.toString().compareTo(item.expectedFormat) != 0) {
            errln("FAIL: format for locale " + item.locale + ", capitalizationContext " + item.capitalizationContext + ", expected \"" + item.expectedFormat + "\", got \"" + result2 + "\"");
        }
        // now read back context, make sure it is what we set (testing with DateFormat subclass)
        DisplayContext capitalizationContext = sdfmt.getContext(DisplayContext.Type.CAPITALIZATION);
        if (capitalizationContext != item.capitalizationContext) {
            errln("FAIL: getContext for locale " + item.locale + ", capitalizationContext " + item.capitalizationContext + ", but got context " + capitalizationContext);
        }
    }
    for (TestRelativeContextItem relItem : relItems) {
        ULocale locale = new ULocale(relItem.locale);
        DateFormat dfmt = DateFormat.getDateInstance(DateFormat.RELATIVE_LONG, locale);
        Date today = new Date();
        // now try context & standard format call
        dfmt.setContext(relItem.capitalizationContext);
        // write to stream, then read a copy from stream & compare
        boolean serializeTestFail = false;
        ByteArrayOutputStream baos = null;
        DateFormat dfmtFromStream = null;
        try {
            baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(dfmt);
            oos.close();
        } catch (IOException i) {
            errln("FAIL: for locale " + relItem.locale + ", capitalizationContext " + relItem.capitalizationContext + ", serialization of RELATIVE_LONG DateFormat fails with IOException");
            serializeTestFail = true;
        }
        if (!serializeTestFail) {
            byte[] buf = baos.toByteArray();
            try {
                ByteArrayInputStream bais = new ByteArrayInputStream(buf);
                ObjectInputStream ois = new ObjectInputStream(bais);
                dfmtFromStream = (DateFormat) ois.readObject();
                ois.close();
            } catch (IOException i) {
                errln("FAIL: for locale " + relItem.locale + ", capitalizationContext " + relItem.capitalizationContext + ", deserialization of RELATIVE_LONG DateFormat fails with IOException");
                serializeTestFail = true;
            } catch (ClassNotFoundException c) {
                errln("FAIL: for locale " + relItem.locale + ", capitalizationContext " + relItem.capitalizationContext + ", deserialization of RELATIVE_LONG DateFormat fails with ClassNotFoundException");
                serializeTestFail = true;
            }
        }
        if (!serializeTestFail && dfmtFromStream == null) {
            errln("FAIL: for locale " + relItem.locale + ", capitalizationContext " + relItem.capitalizationContext + ", dfmtFromStream is null (for RELATIVE_LONG)");
            serializeTestFail = true;
        }
        if (!serializeTestFail && !dfmtFromStream.equals(dfmt)) {
            errln("FAIL: for locale " + relItem.locale + ", capitalizationContext " + relItem.capitalizationContext + ", dfmtFromStream != dfmt (for RELATIVE_LONG)");
            serializeTestFail = true;
        }
        cal.setTime(today);
        StringBuffer result2 = new StringBuffer();
        FieldPosition fpos2 = new FieldPosition(0);
        dfmt.format(cal, result2, fpos2);
        if (result2.toString().compareTo(relItem.expectedFormatToday) != 0) {
            errln("FAIL: format today for locale " + relItem.locale + ", capitalizationContext " + relItem.capitalizationContext + ", expected \"" + relItem.expectedFormatToday + "\", got \"" + result2 + "\"");
        }
        if (!serializeTestFail) {
            result2.setLength(0);
            dfmtFromStream.format(cal, result2, fpos2);
            if (result2.toString().compareTo(relItem.expectedFormatToday) != 0) {
                errln("FAIL: use dfmtFromStream to format today for locale " + relItem.locale + ", capitalizationContext " + relItem.capitalizationContext + ", expected \"" + relItem.expectedFormatToday + "\", got \"" + result2 + "\"");
            }
        }
        cal.add(Calendar.DATE, -1);
        result2.setLength(0);
        dfmt.format(cal, result2, fpos2);
        if (result2.toString().compareTo(relItem.expectedFormatYesterday) != 0) {
            errln("FAIL: format yesterday for locale " + relItem.locale + ", capitalizationContext " + relItem.capitalizationContext + ", expected \"" + relItem.expectedFormatYesterday + "\", got \"" + result2 + "\"");
        }
        // now read back context, make sure it is what we set (testing with DateFormat itself)
        DisplayContext capitalizationContext = dfmt.getContext(DisplayContext.Type.CAPITALIZATION);
        if (capitalizationContext != relItem.capitalizationContext) {
            errln("FAIL: getContext for locale " + relItem.locale + ", capitalizationContext " + relItem.capitalizationContext + ", but got context " + capitalizationContext);
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DisplayContext(android.icu.text.DisplayContext) 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) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) FieldPosition(java.text.FieldPosition) ObjectOutputStream(java.io.ObjectOutputStream) Date(java.util.Date) ByteArrayInputStream(java.io.ByteArrayInputStream) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 90 with DateFormat

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

the class DateFormatTest method TestPatterns.

/**
 * Verify that patterns have the correct values and could produce the
 * the DateFormat instances that contain the correct localized patterns.
 */
@Test
public void TestPatterns() {
    final String[][] EXPECTED = { { DateFormat.YEAR, "y", "en", "y" }, { DateFormat.QUARTER, "QQQQ", "en", "QQQQ" }, { DateFormat.ABBR_QUARTER, "QQQ", "en", "QQQ" }, { DateFormat.YEAR_QUARTER, "yQQQQ", "en", "QQQQ y" }, { DateFormat.YEAR_ABBR_QUARTER, "yQQQ", "en", "QQQ y" }, { DateFormat.MONTH, "MMMM", "en", "LLLL" }, { DateFormat.ABBR_MONTH, "MMM", "en", "LLL" }, { DateFormat.NUM_MONTH, "M", "en", "L" }, { DateFormat.YEAR_MONTH, "yMMMM", "en", "MMMM y" }, { DateFormat.YEAR_ABBR_MONTH, "yMMM", "en", "MMM y" }, { DateFormat.YEAR_NUM_MONTH, "yM", "en", "M/y" }, { DateFormat.DAY, "d", "en", "d" }, { DateFormat.YEAR_MONTH_DAY, "yMMMMd", "en", "MMMM d, y" }, { DateFormat.YEAR_ABBR_MONTH_DAY, "yMMMd", "en", "MMM d, y" }, { DateFormat.YEAR_NUM_MONTH_DAY, "yMd", "en", "M/d/y" }, { DateFormat.WEEKDAY, "EEEE", "en", "cccc" }, { DateFormat.ABBR_WEEKDAY, "E", "en", "ccc" }, { DateFormat.YEAR_MONTH_WEEKDAY_DAY, "yMMMMEEEEd", "en", "EEEE, MMMM d, y" }, { DateFormat.YEAR_ABBR_MONTH_WEEKDAY_DAY, "yMMMEd", "en", "EEE, MMM d, y" }, { DateFormat.YEAR_NUM_MONTH_WEEKDAY_DAY, "yMEd", "en", "EEE, M/d/y" }, { DateFormat.MONTH_DAY, "MMMMd", "en", "MMMM d" }, { DateFormat.ABBR_MONTH_DAY, "MMMd", "en", "MMM d" }, { DateFormat.NUM_MONTH_DAY, "Md", "en", "M/d" }, { DateFormat.MONTH_WEEKDAY_DAY, "MMMMEEEEd", "en", "EEEE, MMMM d" }, { DateFormat.ABBR_MONTH_WEEKDAY_DAY, "MMMEd", "en", "EEE, MMM d" }, { DateFormat.NUM_MONTH_WEEKDAY_DAY, "MEd", "en", "EEE, M/d" }, // (fixed expected result per ticket 6872<-6626)
    { DateFormat.HOUR, "j", "en", "h a" }, // (fixed expected result per ticket 6872<-6626
    { DateFormat.HOUR24, "H", "en", "HH" }, { DateFormat.MINUTE, "m", "en", "m" }, // (fixed expected result per ticket 6872<-7180)
    { DateFormat.HOUR_MINUTE, "jm", "en", "h:mm a" }, // (fixed expected result per ticket 6872<-6626)
    { DateFormat.HOUR24_MINUTE, "Hm", "en", "HH:mm" }, { DateFormat.SECOND, "s", "en", "s" }, // (fixed expected result per ticket 6872<-7180)
    { DateFormat.HOUR_MINUTE_SECOND, "jms", "en", "h:mm:ss a" }, // (fixed expected result per ticket 6872<-6626)
    { DateFormat.HOUR24_MINUTE_SECOND, "Hms", "en", "HH:mm:ss" }, // (fixed expected result per ticket 6872<-6626)
    { DateFormat.MINUTE_SECOND, "ms", "en", "mm:ss" }, { DateFormat.LOCATION_TZ, "VVVV", "en", "VVVV" }, { DateFormat.GENERIC_TZ, "vvvv", "en", "vvvv" }, { DateFormat.ABBR_GENERIC_TZ, "v", "en", "v" }, { DateFormat.SPECIFIC_TZ, "zzzz", "en", "zzzz" }, { DateFormat.ABBR_SPECIFIC_TZ, "z", "en", "z" }, { DateFormat.ABBR_UTC_TZ, "ZZZZ", "en", "ZZZZ" }, // marker for starting combinations
    {}, { DateFormat.YEAR_NUM_MONTH_DAY + DateFormat.ABBR_UTC_TZ, "yMdZZZZ", "en", "M/d/y, ZZZZ" }, { DateFormat.MONTH_DAY + DateFormat.LOCATION_TZ, "MMMMdVVVV", "en", "MMMM d, VVVV" } };
    // just for verbose log
    Date testDate = new Date(2012 - 1900, 6, 1, 14, 58, 59);
    List<String> expectedSkeletons = new ArrayList<String>(DateFormat.DATE_SKELETONS);
    expectedSkeletons.addAll(DateFormat.TIME_SKELETONS);
    expectedSkeletons.addAll(DateFormat.ZONE_SKELETONS);
    boolean combinations = false;
    List<String> testedSkeletons = new ArrayList<String>();
    for (int i = 0; i < EXPECTED.length; i++) {
        if (EXPECTED[i].length == 0) {
            combinations = true;
            continue;
        }
        boolean ok = true;
        // Verify that patterns have the correct values
        String actualPattern = EXPECTED[i][0];
        if (!combinations) {
            testedSkeletons.add(actualPattern);
        }
        String expectedPattern = EXPECTED[i][1];
        ULocale locale = new ULocale(EXPECTED[i][2], "", "");
        if (!actualPattern.equals(expectedPattern)) {
            errln("FAILURE! Expected pattern: " + expectedPattern + " but was: " + actualPattern);
            ok = false;
        }
        // Verify that DataFormat instances produced contain the correct
        // localized patterns
        DateFormat date1 = DateFormat.getPatternInstance(actualPattern, locale);
        DateFormat date2 = DateFormat.getPatternInstance(Calendar.getInstance(locale), actualPattern, locale);
        String expectedLocalPattern = EXPECTED[i][3];
        String actualLocalPattern1 = ((SimpleDateFormat) date1).toLocalizedPattern();
        String actualLocalPattern2 = ((SimpleDateFormat) date2).toLocalizedPattern();
        if (!actualLocalPattern1.equals(expectedLocalPattern)) {
            errln("FAILURE! Expected local pattern: " + expectedLocalPattern + " but was: " + actualLocalPattern1);
            ok = false;
        }
        if (!actualLocalPattern2.equals(expectedLocalPattern)) {
            errln("FAILURE! Expected local pattern: " + expectedLocalPattern + " but was: " + actualLocalPattern2);
            ok = false;
        }
        if (ok && isVerbose()) {
            logln(date1.format(testDate) + "\t\t" + Arrays.asList(EXPECTED[i]));
        }
    }
    assertEquals("All skeletons are tested (and in an iterable list)", new HashSet<String>(expectedSkeletons), new HashSet<String>(testedSkeletons));
    assertEquals("All skeletons are tested (and in an iterable list), and in the right order.", expectedSkeletons, testedSkeletons);
}
Also used : ULocale(android.icu.util.ULocale) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ArrayList(java.util.ArrayList) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

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