Search in sources :

Example 46 with SimpleDateFormat

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

the class DateFormatTest method TestDayOfYearPattern195.

/**
 * Test the day of year pattern.
 */
@Test
public void TestDayOfYearPattern195() {
    Calendar cal = Calendar.getInstance();
    Date today = cal.getTime();
    int year, month, day;
    year = cal.get(Calendar.YEAR);
    month = cal.get(Calendar.MONTH);
    day = cal.get(Calendar.DAY_OF_MONTH);
    cal.clear();
    cal.set(year, month, day);
    Date expected = cal.getTime();
    logln("Test Date: " + today);
    SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance();
    tryPattern(sdf, today, null, expected);
    tryPattern(sdf, today, "G yyyy DDD", expected);
}
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) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 47 with SimpleDateFormat

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

the class DateFormatTest method TestNonGregoFmtParse.

@Test
public void TestNonGregoFmtParse() {
    class CalAndFmtTestItem {

        public int era;

        public int year;

        public int month;

        public int day;

        public int hour;

        public int minute;

        public String formattedDate;

        // Simple constructor
        public CalAndFmtTestItem(int er, int yr, int mo, int da, int hr, int mi, String fd) {
            era = er;
            year = yr;
            month = mo;
            day = da;
            hour = hr;
            minute = mi;
            formattedDate = fd;
        }
    }
    ;
    // test items for he@calendar=hebrew, long date format
    final CalAndFmtTestItem[] cafti_he_hebrew_long = { // era    yr  mo  da  hr  mi  formattedDate
    new CalAndFmtTestItem(0, 4999, 12, 29, 12, 0, "\u05DB\u05F4\u05D8 \u05D1\u05D0\u05DC\u05D5\u05DC \u05D3\u05F3\u05EA\u05EA\u05E7\u05E6\u05F4\u05D8"), new CalAndFmtTestItem(0, 5100, 0, 1, 12, 0, "\u05D0\u05F3 \u05D1\u05EA\u05E9\u05E8\u05D9 \u05E7\u05F3"), new CalAndFmtTestItem(0, 5774, 5, 1, 12, 0, "\u05D0\u05F3 \u05D1\u05D0\u05D3\u05E8 \u05D0\u05F3 \u05EA\u05E9\u05E2\u05F4\u05D3"), new CalAndFmtTestItem(0, 5999, 12, 29, 12, 0, "\u05DB\u05F4\u05D8 \u05D1\u05D0\u05DC\u05D5\u05DC \u05EA\u05EA\u05E7\u05E6\u05F4\u05D8"), new CalAndFmtTestItem(0, 6100, 0, 1, 12, 0, "\u05D0\u05F3 \u05D1\u05EA\u05E9\u05E8\u05D9 \u05D5\u05F3\u05E7\u05F3") };
    final CalAndFmtTestItem[] cafti_zh_chinese_custU = { // era    yr  mo  da  hr  mi  formattedDate
    new CalAndFmtTestItem(78, 31, 0, 1, 12, 0, "2014\u7532\u5348\u5E74\u6B63\u67081"), new CalAndFmtTestItem(77, 31, 0, 1, 12, 0, "1954\u7532\u5348\u5E74\u6B63\u67081") };
    final CalAndFmtTestItem[] cafti_zh_chinese_custNoU = { // era    yr  mo  da  hr  mi  formattedDate
    new CalAndFmtTestItem(78, 31, 0, 1, 12, 0, "2014\u5E74\u6B63\u67081"), new CalAndFmtTestItem(77, 31, 0, 1, 12, 0, "1954\u5E74\u6B63\u67081") };
    final CalAndFmtTestItem[] cafti_ja_japanese_custGy = { // era    yr  mo  da  hr  mi  formattedDate
    new CalAndFmtTestItem(235, 26, 2, 5, 12, 0, "2014(\u5E73\u621026)\u5E743\u67085\u65E5"), new CalAndFmtTestItem(234, 60, 2, 5, 12, 0, "1985(\u662D\u548C60)\u5E743\u67085\u65E5") };
    final CalAndFmtTestItem[] cafti_ja_japanese_custNoGy = { // era    yr  mo  da  hr  mi  formattedDate
    new CalAndFmtTestItem(235, 26, 2, 5, 12, 0, "2014\u5E743\u67085\u65E5"), new CalAndFmtTestItem(234, 60, 2, 5, 12, 0, "1985\u5E743\u67085\u65E5") };
    final CalAndFmtTestItem[] cafti_en_islamic_cust = { // era    yr  mo  da  hr  mi  formattedDate
    new CalAndFmtTestItem(0, 1384, 0, 1, 12, 0, "1 Muh. 1384 AH, 1964"), new CalAndFmtTestItem(0, 1436, 0, 1, 12, 0, "1 Muh. 1436 AH, 2014"), new CalAndFmtTestItem(0, 1487, 0, 1, 12, 0, "1 Muh. 1487 AH, 2064") };
    class TestNonGregoItem {

        public String locale;

        public int style;

        // ignored unless style == DateFormat.NONE
        public String pattern;

        public CalAndFmtTestItem[] caftItems;

        // Simple constructor
        public TestNonGregoItem(String loc, int styl, String pat, CalAndFmtTestItem[] items) {
            locale = loc;
            style = styl;
            pattern = pat;
            caftItems = items;
        }
    }
    ;
    final TestNonGregoItem[] items = { new TestNonGregoItem("he@calendar=hebrew", DateFormat.LONG, "", cafti_he_hebrew_long), new TestNonGregoItem("zh@calendar=chinese", DateFormat.NONE, "rU\u5E74MMMd", cafti_zh_chinese_custU), new TestNonGregoItem("zh@calendar=chinese", DateFormat.NONE, "r\u5E74MMMd", cafti_zh_chinese_custNoU), new TestNonGregoItem("ja@calendar=japanese", DateFormat.NONE, "r(Gy)\u5E74M\u6708d\u65E5", cafti_ja_japanese_custGy), new TestNonGregoItem("ja@calendar=japanese", DateFormat.NONE, "r\u5E74M\u6708d\u65E5", cafti_ja_japanese_custNoGy), new TestNonGregoItem("en@calendar=islamic", DateFormat.NONE, "d MMM y G, r", cafti_en_islamic_cust) };
    for (TestNonGregoItem item : items) {
        ULocale locale = new ULocale(item.locale);
        DateFormat dfmt = null;
        if (item.style != DateFormat.NONE) {
            dfmt = DateFormat.getDateInstance(item.style, locale);
        } else {
            dfmt = new SimpleDateFormat(item.pattern, locale);
        }
        Calendar cal = dfmt.getCalendar();
        for (CalAndFmtTestItem caftItem : item.caftItems) {
            cal.clear();
            cal.set(Calendar.ERA, caftItem.era);
            cal.set(caftItem.year, caftItem.month, caftItem.day, caftItem.hour, caftItem.minute, 0);
            StringBuffer result = new StringBuffer();
            FieldPosition fpos = new FieldPosition(0);
            dfmt.format(cal, result, fpos);
            if (result.toString().compareTo(caftItem.formattedDate) != 0) {
                errln("FAIL: date format for locale " + item.locale + ", style " + item.style + ", expected \"" + caftItem.formattedDate + "\", got \"" + result + "\"");
            } else {
                // formatted OK, try parse
                ParsePosition ppos = new ParsePosition(0);
                dfmt.parse(result.toString(), cal, ppos);
                int era = cal.get(Calendar.ERA);
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DATE);
                if (ppos.getIndex() < result.length() || era != caftItem.era || year != caftItem.year || month != caftItem.month || day != caftItem.day) {
                    errln("FAIL: date parse for locale " + item.locale + ", style " + item.style + ", string \"" + result + "\", expected " + caftItem.era + ":" + caftItem.year + "-" + caftItem.month + "-" + caftItem.day + ", got pos " + ppos.getIndex() + " " + year + "-" + month + "-" + day);
                }
            }
        }
    }
}
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 48 with SimpleDateFormat

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

the class DateFormatTest method _testIt917.

// internal test subroutine, used by TestRunTogetherPattern917
private void _testIt917(SimpleDateFormat fmt, String str, Date expected) {
    logln("pattern=" + fmt.toPattern() + "   string=" + str);
    Date o = new Date();
    o = (Date) ((DateFormat) fmt).parseObject(str, new ParsePosition(0));
    logln("Parsed object: " + o);
    if (o == null || !o.equals(expected))
        errln("FAIL: Expected " + expected);
    String formatted = o == null ? "null" : ((DateFormat) fmt).format(o);
    logln("Formatted string: " + formatted);
    if (!formatted.equals(str))
        errln("FAIL: Expected " + str);
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 49 with SimpleDateFormat

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

the class DateFormatTest method TestCalendarType.

/*
     * Test for checking SimpleDateFormat/DateFormatSymbols creation
     * honor the calendar keyword in the given locale.  See ticket#6100
     */
@Test
public void TestCalendarType() {
    final String testPattern = "GGGG y MMMM d EEEE";
    final ULocale[] testLocales = { new ULocale("de"), new ULocale("fr_FR@calendar=gregorian"), new ULocale("en@calendar=islamic"), new ULocale("ja_JP@calendar=japanese"), new ULocale("zh_Hans_CN@calendar=bogus"), new ULocale("ko_KR@calendar=dangi") };
    SimpleDateFormat[] formatters = new SimpleDateFormat[5];
    for (int i = 0; i < testLocales.length; i++) {
        // Create a locale with no keywords
        StringBuffer locStrBuf = new StringBuffer();
        if (testLocales[i].getLanguage().length() > 0) {
            locStrBuf.append(testLocales[i].getLanguage());
        }
        if (testLocales[i].getScript().length() > 0) {
            locStrBuf.append('_');
            locStrBuf.append(testLocales[i].getScript());
        }
        if (testLocales[i].getCountry().length() > 0) {
            locStrBuf.append('_');
            locStrBuf.append(testLocales[i].getCountry());
        }
        ULocale locNoKeywords = new ULocale(locStrBuf.toString());
        Calendar cal = Calendar.getInstance(testLocales[i]);
        // Calendar getDateFormat method
        DateFormat df = cal.getDateTimeFormat(DateFormat.MEDIUM, DateFormat.MEDIUM, locNoKeywords);
        if (df instanceof SimpleDateFormat) {
            formatters[0] = (SimpleDateFormat) df;
            formatters[0].applyPattern(testPattern);
        } else {
            formatters[0] = null;
        }
        // DateFormat constructor with locale
        df = DateFormat.getDateInstance(DateFormat.MEDIUM, testLocales[i]);
        if (df instanceof SimpleDateFormat) {
            formatters[1] = (SimpleDateFormat) df;
            formatters[1].applyPattern(testPattern);
        } else {
            formatters[1] = null;
        }
        // DateFormat constructor with Calendar
        df = DateFormat.getDateInstance(cal, DateFormat.MEDIUM, locNoKeywords);
        if (df instanceof SimpleDateFormat) {
            formatters[2] = (SimpleDateFormat) df;
            formatters[2].applyPattern(testPattern);
        } else {
            formatters[2] = null;
        }
        // SimpleDateFormat constructor
        formatters[3] = new SimpleDateFormat(testPattern, testLocales[i]);
        // SimpleDateFormat with DateFormatSymbols
        DateFormatSymbols dfs = new DateFormatSymbols(testLocales[i]);
        formatters[4] = new SimpleDateFormat(testPattern, dfs, testLocales[i]);
        // All SimpleDateFormat instances should produce the exact
        // same result.
        String expected = null;
        Date d = new Date();
        for (int j = 0; j < formatters.length; j++) {
            if (formatters[j] != null) {
                String tmp = formatters[j].format(d);
                if (expected == null) {
                    expected = tmp;
                } else if (!expected.equals(tmp)) {
                    errln("FAIL: formatter[" + j + "] returned \"" + tmp + "\" in locale " + testLocales[i] + " - expected: " + expected);
                }
            }
        }
    }
}
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) 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) Date(java.util.Date) Test(org.junit.Test)

Example 50 with SimpleDateFormat

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

the class DateFormatTest method TestISOEra.

/*
 * Test case for ISO Era processing (ticket#7357)
 */
@Test
public void TestISOEra() {
    String[] data = { // input, output
    "BC 4004-10-23T07:00:00Z", "BC 4004-10-23T07:00:00Z", "AD 4004-10-23T07:00:00Z", "AD 4004-10-23T07:00:00Z", "-4004-10-23T07:00:00Z", "BC 4005-10-23T07:00:00Z", "4004-10-23T07:00:00Z", "AD 4004-10-23T07:00:00Z" };
    int numData = 8;
    // create formatter
    SimpleDateFormat fmt1 = new SimpleDateFormat("GGG yyyy-MM-dd'T'HH:mm:ss'Z");
    for (int i = 0; i < numData; i += 2) {
        // create input string
        String in = data[i];
        // parse string to date
        Date dt1;
        try {
            dt1 = fmt1.parse(in);
        } catch (Exception e) {
            errln("DateFormat.parse is not suppose to return an exception.");
            break;
        }
        // format date back to string
        String out;
        out = fmt1.format(dt1);
        // check that roundtrip worked as expected
        String expected = data[i + 1];
        if (!out.equals(expected)) {
            errln((String) "FAIL: " + in + " -> " + out + " expected -> " + expected);
        }
    }
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

SimpleDateFormat (android.icu.text.SimpleDateFormat)153 Test (org.junit.Test)113 Date (java.util.Date)103 GregorianCalendar (android.icu.util.GregorianCalendar)59 Calendar (android.icu.util.Calendar)53 ParseException (java.text.ParseException)42 DateFormat (android.icu.text.DateFormat)41 JapaneseCalendar (android.icu.util.JapaneseCalendar)41 ULocale (android.icu.util.ULocale)40 IslamicCalendar (android.icu.util.IslamicCalendar)37 ParsePosition (java.text.ParsePosition)36 FieldPosition (java.text.FieldPosition)29 ChineseCalendar (android.icu.util.ChineseCalendar)27 TimeZone (android.icu.util.TimeZone)27 BuddhistCalendar (android.icu.util.BuddhistCalendar)25 ChineseDateFormat (android.icu.text.ChineseDateFormat)21 HebrewCalendar (android.icu.util.HebrewCalendar)21 IOException (java.io.IOException)19 SimpleTimeZone (android.icu.util.SimpleTimeZone)16 Locale (java.util.Locale)14