Search in sources :

Example 61 with DateFormat

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

the class DateFormatTest method TestMonthPatterns.

@Test
public void TestMonthPatterns() {
    class ChineseCalTestDate {

        public int era;

        public int year;

        // here 1-based
        public int month;

        public int isLeapMonth;

        public int day;

        // Simple constructor
        public ChineseCalTestDate(int e, int y, int m, int il, int d) {
            era = e;
            year = y;
            month = m;
            isLeapMonth = il;
            day = d;
        }
    }
    ;
    final ChineseCalTestDate[] dates = { // (in chinese era 78) gregorian 2012-4-22
    new ChineseCalTestDate(78, 29, 4, 0, 2), // (in chinese era 78) gregorian 2012-5-22
    new ChineseCalTestDate(78, 29, 4, 1, 2), // (in chinese era 78) gregorian 2012-6-20
    new ChineseCalTestDate(78, 29, 5, 0, 2) };
    class MonthPatternItem {

        public String locale;

        public int style;

        public String[] dateString;

        // Simple constructor
        public MonthPatternItem(String loc, int styl, String dateStr0, String dateStr1, String dateStr2) {
            locale = loc;
            style = styl;
            dateString = new String[3];
            dateString[0] = dateStr0;
            dateString[1] = dateStr1;
            dateString[2] = dateStr2;
        }
    }
    ;
    final MonthPatternItem[] items = { new MonthPatternItem("root@calendar=chinese", DateFormat.LONG, "2012(ren-chen) M04 2", "2012(ren-chen) M04bis 2", "2012(ren-chen) M05 2"), new MonthPatternItem("root@calendar=chinese", DateFormat.SHORT, "2012-04-02", "2012-04bis-02", "2012-05-02"), new MonthPatternItem("root@calendar=chinese", -1, "29-4-2", "29-4bis-2", "29-5-2"), new MonthPatternItem("root@calendar=chinese", -2, "78x29-4-2", "78x29-4bis-2", "78x29-5-2"), new MonthPatternItem("root@calendar=chinese", -3, "ren-chen-4-2", "ren-chen-4bis-2", "ren-chen-5-2"), new MonthPatternItem("root@calendar=chinese", -4, "ren-chen M04 2", "ren-chen M04bis 2", "ren-chen M05 2"), new MonthPatternItem("en@calendar=gregorian", -3, "2012-4-22", "2012-5-22", "2012-6-20"), new MonthPatternItem("en@calendar=chinese", DateFormat.LONG, "Fourth Month 2, 2012(ren-chen)", "Fourth Monthbis 2, 2012(ren-chen)", "Fifth Month 2, 2012(ren-chen)"), new MonthPatternItem("en@calendar=chinese", DateFormat.SHORT, "4/2/2012", "4bis/2/2012", "5/2/2012"), new MonthPatternItem("zh@calendar=chinese", DateFormat.LONG, "2012\u58EC\u8FB0\u5E74\u56DB\u6708\u521D\u4E8C", "2012\u58EC\u8FB0\u5E74\u95F0\u56DB\u6708\u521D\u4E8C", "2012\u58EC\u8FB0\u5E74\u4E94\u6708\u521D\u4E8C"), new MonthPatternItem("zh@calendar=chinese", DateFormat.SHORT, "2012/4/2", "2012/\u95F04/2", "2012/5/2"), new MonthPatternItem("zh@calendar=chinese", -3, "\u58EC\u8FB0-4-2", "\u58EC\u8FB0-\u95F04-2", "\u58EC\u8FB0-5-2"), new MonthPatternItem("zh@calendar=chinese", -4, "\u58EC\u8FB0 \u56DB\u6708 2", "\u58EC\u8FB0 \u95F0\u56DB\u6708 2", "\u58EC\u8FB0 \u4E94\u6708 2"), new MonthPatternItem("zh_Hant@calendar=chinese", DateFormat.LONG, "2012\u58EC\u8FB0\u5E74\u56DB\u6708\u521D\u4E8C", "2012\u58EC\u8FB0\u5E74\u958F\u56DB\u6708\u521D\u4E8C", "2012\u58EC\u8FB0\u5E74\u4E94\u6708\u521D\u4E8C"), new MonthPatternItem("zh_Hant@calendar=chinese", DateFormat.SHORT, "2012/4/2", "2012/\u958F4/2", "2012/5/2"), new MonthPatternItem("fr@calendar=chinese", DateFormat.LONG, "2 s\u00ECyu\u00E8 ren-chen", "2 s\u00ECyu\u00E8bis ren-chen", "2 w\u01D4yu\u00E8 ren-chen"), new MonthPatternItem("fr@calendar=chinese", DateFormat.SHORT, "2/4/29", "2/4bis/29", "2/5/29"), new MonthPatternItem("en@calendar=dangi", DateFormat.LONG, "Third Monthbis 2, 2012(ren-chen)", "Fourth Month 2, 2012(ren-chen)", "Fifth Month 1, 2012(ren-chen)"), new MonthPatternItem("en@calendar=dangi", DateFormat.SHORT, "3bis/2/2012", "4/2/2012", "5/1/2012"), new MonthPatternItem("en@calendar=dangi", -2, "78x29-3bis-2", "78x29-4-2", "78x29-5-1"), new MonthPatternItem("ko@calendar=dangi", DateFormat.LONG, "\uC784\uC9C4\uB144 \uC7243\uC6D4 2\uC77C", "\uC784\uC9C4\uB144 4\uC6D4 2\uC77C", "\uC784\uC9C4\uB144 5\uC6D4 1\uC77C"), new MonthPatternItem("ko@calendar=dangi", DateFormat.SHORT, "29. \uC7243. 2.", "29. 4. 2.", "29. 5. 1.") };
    // style: -1        -2            -3       -4
    // previously G and l for chinese cal only handled by ChineseDateFormat
    final String[] customPatterns = { "y-Ml-d", "G'x'y-Ml-d", "U-M-d", "U MMM d" };
    Calendar rootChineseCalendar = Calendar.getInstance(new ULocale("root@calendar=chinese"));
    for (MonthPatternItem item : items) {
        ULocale locale = new ULocale(item.locale);
        DateFormat dfmt = (item.style >= 0) ? DateFormat.getDateInstance(item.style, locale) : new SimpleDateFormat(customPatterns[-item.style - 1], locale);
        int idate = 0;
        for (ChineseCalTestDate date : dates) {
            rootChineseCalendar.clear();
            rootChineseCalendar.set(Calendar.ERA, date.era);
            rootChineseCalendar.set(date.year, date.month - 1, date.day);
            rootChineseCalendar.set(Calendar.IS_LEAP_MONTH, date.isLeapMonth);
            StringBuffer result = new StringBuffer();
            FieldPosition fpos = new FieldPosition(0);
            dfmt.format(rootChineseCalendar, result, fpos);
            if (result.toString().compareTo(item.dateString[idate]) != 0) {
                errln("FAIL: Chinese calendar format for locale " + item.locale + ", style " + item.style + ", expected \"" + item.dateString[idate] + "\", got \"" + result + "\"");
            } else {
                // formatted OK, try parse
                ParsePosition ppos = new ParsePosition(0);
                // ensure we are really parsing the fields we should be
                rootChineseCalendar.set(Calendar.YEAR, 1);
                rootChineseCalendar.set(Calendar.MONTH, 0);
                rootChineseCalendar.set(Calendar.IS_LEAP_MONTH, 0);
                rootChineseCalendar.set(Calendar.DATE, 1);
                // 
                dfmt.parse(result.toString(), rootChineseCalendar, ppos);
                int era = rootChineseCalendar.get(Calendar.ERA);
                int year = rootChineseCalendar.get(Calendar.YEAR);
                int month = rootChineseCalendar.get(Calendar.MONTH) + 1;
                int isLeapMonth = rootChineseCalendar.get(Calendar.IS_LEAP_MONTH);
                int day = rootChineseCalendar.get(Calendar.DATE);
                if (ppos.getIndex() < result.length() || year != date.year || month != date.month || isLeapMonth != date.isLeapMonth || day != date.day) {
                    errln("FAIL: Chinese calendar parse for locale " + item.locale + ", style " + item.style + ", string \"" + result + "\", expected " + date.year + "-" + date.month + "(" + date.isLeapMonth + ")-" + date.day + ", got pos " + ppos.getIndex() + " era(" + era + ")-" + year + "-" + month + "(" + isLeapMonth + ")-" + day);
                }
            }
            idate++;
        }
    }
}
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) FieldPosition(java.text.FieldPosition) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 62 with DateFormat

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

the class DateFormatTest method TestTwoDigitYear.

/**
 * Test the parsing of two-digit years.
 */
@Test
public void TestTwoDigitYear() {
    DateFormat fmt = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(130 + 1900, Calendar.JUNE, 5);
    parse2DigitYear(fmt, "6/5/30", cal.getTime());
    cal.clear();
    cal.set(50 + 1900, Calendar.JUNE, 4);
    parse2DigitYear(fmt, "6/4/50", cal.getTime());
}
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) Test(org.junit.Test)

Example 63 with DateFormat

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

the class DateFormatTest method Test10632.

// A regression test case for ticket#10632.
// Make sure RELATIVE style works for getInstance overloads taking
// Calendar instance.
@Test
public void Test10632() {
    Date[] testDates = new Date[3];
    Calendar cal = Calendar.getInstance();
    // today
    testDates[0] = cal.getTime();
    // tomorrow
    cal.add(Calendar.DATE, 1);
    testDates[1] = cal.getTime();
    // yesterday
    cal.add(Calendar.DATE, -2);
    testDates[2] = cal.getTime();
    // Relative styles for testing
    int[] dateStylesList = { DateFormat.RELATIVE_FULL, DateFormat.RELATIVE_LONG, DateFormat.RELATIVE_MEDIUM, DateFormat.RELATIVE_SHORT };
    Calendar fmtCal = DateFormat.getInstance().getCalendar();
    for (int i = 0; i < dateStylesList.length; i++) {
        DateFormat fmt0 = DateFormat.getDateTimeInstance(dateStylesList[i], DateFormat.DEFAULT);
        DateFormat fmt1 = DateFormat.getDateTimeInstance(fmtCal, dateStylesList[i], DateFormat.DEFAULT);
        for (int j = 0; j < testDates.length; j++) {
            String s0 = fmt0.format(testDates[j]);
            String s1 = fmt1.format(testDates[j]);
            if (!s0.equals(s1)) {
                errln("FAIL: Different results returned by two equivalent relative formatters: s0=" + s0 + ", s1=" + s1);
            }
        }
    }
}
Also used : 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) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 64 with DateFormat

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

the class DateFormatTest method TestCzechMonths459.

/**
 * Verify the handling of Czech June and July, which have the unique attribute that
 * one is a proper prefix substring of the other.
 */
@Test
public void TestCzechMonths459() {
    DateFormat fmt = DateFormat.getDateInstance(DateFormat.FULL, new Locale("cs", "", ""));
    logln("Pattern " + ((SimpleDateFormat) fmt).toPattern());
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(1997, Calendar.JUNE, 15);
    Date june = cal.getTime();
    cal.clear();
    cal.set(1997, Calendar.JULY, 15);
    Date july = cal.getTime();
    String juneStr = fmt.format(june);
    String julyStr = fmt.format(july);
    try {
        logln("format(June 15 1997) = " + juneStr);
        Date d = fmt.parse(juneStr);
        String s = fmt.format(d);
        int month, yr, day;
        cal.setTime(d);
        yr = cal.get(Calendar.YEAR);
        month = cal.get(Calendar.MONTH);
        day = cal.get(Calendar.DAY_OF_MONTH);
        logln("  . parse . " + s + " (month = " + month + ")");
        if (month != Calendar.JUNE)
            errln("FAIL: Month should be June");
        if (yr != 1997)
            errln("FAIL: Year should be 1997");
        if (day != 15)
            errln("FAIL: day should be 15");
        logln("format(July 15 1997) = " + julyStr);
        d = fmt.parse(julyStr);
        s = fmt.format(d);
        cal.setTime(d);
        yr = cal.get(Calendar.YEAR) - 1900;
        month = cal.get(Calendar.MONTH);
        day = cal.get(Calendar.DAY_OF_WEEK);
        logln("  . parse . " + s + " (month = " + month + ")");
        if (month != Calendar.JULY)
            errln("FAIL: Month should be July");
    } catch (ParseException e) {
        errln(e.getMessage());
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) 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) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 65 with DateFormat

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

the class DateFormatTest method TestParseLeniencyAPIs.

@Test
public void TestParseLeniencyAPIs() {
    DateFormat fmt = DateFormat.getInstance();
    assertTrue("isLenient default", fmt.isLenient());
    assertTrue("isCalendarLenient default", fmt.isCalendarLenient());
    assertTrue("ALLOW_WHITESPACE default", fmt.getBooleanAttribute(BooleanAttribute.PARSE_ALLOW_WHITESPACE));
    assertTrue("ALLOW_NUMERIC default", fmt.getBooleanAttribute(BooleanAttribute.PARSE_ALLOW_NUMERIC));
    assertTrue("PARTIAL_MATCH default", fmt.getBooleanAttribute(BooleanAttribute.PARSE_PARTIAL_LITERAL_MATCH));
    assertTrue("MULTIPLE_PATTERNS default", fmt.getBooleanAttribute(BooleanAttribute.PARSE_MULTIPLE_PATTERNS_FOR_MATCH));
    // Set calendar to strict
    fmt.setCalendarLenient(false);
    assertFalse("isLeninent after setCalendarLenient(FALSE)", fmt.isLenient());
    assertFalse("isCalendarLenient after setCalendarLenient(FALSE)", fmt.isCalendarLenient());
    assertTrue("ALLOW_WHITESPACE after setCalendarLenient(FALSE)", fmt.getBooleanAttribute(BooleanAttribute.PARSE_ALLOW_WHITESPACE));
    assertTrue("ALLOW_NUMERIC  after setCalendarLenient(FALSE)", fmt.getBooleanAttribute(BooleanAttribute.PARSE_ALLOW_NUMERIC));
    // Set to strict
    fmt.setLenient(false);
    assertFalse("isLeninent after setLenient(FALSE)", fmt.isLenient());
    assertFalse("isCalendarLenient after setLenient(FALSE)", fmt.isCalendarLenient());
    assertFalse("ALLOW_WHITESPACE after setLenient(FALSE)", fmt.getBooleanAttribute(BooleanAttribute.PARSE_ALLOW_WHITESPACE));
    assertFalse("ALLOW_NUMERIC  after setLenient(FALSE)", fmt.getBooleanAttribute(BooleanAttribute.PARSE_ALLOW_NUMERIC));
    // These two boolean attributes are NOT affected according to the API specification
    assertTrue("PARTIAL_MATCH after setLenient(FALSE)", fmt.getBooleanAttribute(BooleanAttribute.PARSE_PARTIAL_LITERAL_MATCH));
    assertTrue("MULTIPLE_PATTERNS after setLenient(FALSE)", fmt.getBooleanAttribute(BooleanAttribute.PARSE_MULTIPLE_PATTERNS_FOR_MATCH));
    // Allow white space leniency
    fmt.setBooleanAttribute(BooleanAttribute.PARSE_ALLOW_WHITESPACE, true);
    assertFalse("isLeninent after ALLOW_WHITESPACE/TRUE", fmt.isLenient());
    assertFalse("isCalendarLenient after ALLOW_WHITESPACE/TRUE", fmt.isCalendarLenient());
    assertTrue("ALLOW_WHITESPACE after ALLOW_WHITESPACE/TRUE", fmt.getBooleanAttribute(BooleanAttribute.PARSE_ALLOW_WHITESPACE));
    assertFalse("ALLOW_NUMERIC  after ALLOW_WHITESPACE/TRUE", fmt.getBooleanAttribute(BooleanAttribute.PARSE_ALLOW_NUMERIC));
    // Set to lenient
    fmt.setLenient(true);
    assertTrue("isLenient after setLenient(TRUE)", fmt.isLenient());
    assertTrue("isCalendarLenient after setLenient(TRUE)", fmt.isCalendarLenient());
    assertTrue("ALLOW_WHITESPACE after setLenient(TRUE)", fmt.getBooleanAttribute(BooleanAttribute.PARSE_ALLOW_WHITESPACE));
    assertTrue("ALLOW_NUMERIC after setLenient(TRUE)", fmt.getBooleanAttribute(BooleanAttribute.PARSE_ALLOW_NUMERIC));
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) 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