Search in sources :

Example 96 with SimpleDateFormat

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

the class DateFormatTest method TestFormalChineseDate.

@Test
public void TestFormalChineseDate() {
    String pattern = "y\u5e74M\u6708d\u65e5";
    String override = "y=hanidec;M=hans;d=hans";
    // create formatter
    SimpleDateFormat sdf = new SimpleDateFormat(pattern, override, ULocale.CHINA);
    Calendar cal = Calendar.getInstance(ULocale.ENGLISH);
    cal.clear(Calendar.MILLISECOND);
    cal.set(2009, 6, 28, 0, 0, 0);
    FieldPosition pos = new FieldPosition(0);
    StringBuffer result = new StringBuffer();
    sdf.format(cal, result, pos);
    String res1 = result.toString();
    String expected = "\u4e8c\u3007\u3007\u4e5d\u5e74\u4e03\u6708\u4e8c\u5341\u516b\u65e5";
    if (!res1.equals(expected)) {
        errln((String) "FAIL: -> " + result.toString() + " expected -> " + expected);
    }
    ParsePosition pp = new ParsePosition(0);
    Date parsedate = sdf.parse(expected, pp);
    long time1 = parsedate.getTime();
    long time2 = cal.getTimeInMillis();
    if (time1 != time2) {
        errln("FAIL: parsed -> " + parsedate.toString() + " expected -> " + cal.toString());
    }
}
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) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 97 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat 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 98 with SimpleDateFormat

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

the class DateFormatTest method TestInvalidPattern.

@Test
public void TestInvalidPattern() {
    Exception e = null;
    SimpleDateFormat f = null;
    String out = null;
    try {
        f = new SimpleDateFormat("Yesterday");
        out = f.format(new Date(0));
    } catch (IllegalArgumentException e1) {
        e = e1;
    }
    if (e != null) {
        logln("Ok: Received " + e.getMessage());
    } else {
        errln("FAIL: Expected exception, got " + f.toPattern() + "; " + out);
    }
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat) ParseException(java.text.ParseException) IOException(java.io.IOException) Date(java.util.Date) Test(org.junit.Test)

Example 99 with SimpleDateFormat

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

the class DateFormatTest method TestDateFormatLeniency.

@Test
public void TestDateFormatLeniency() {
    class TestDateFormatLeniencyItem {

        public ULocale locale;

        public boolean leniency;

        public String parseString;

        public String pattern;

        // null indicates expected error
        public String expectedResult;

        // Simple constructor
        public TestDateFormatLeniencyItem(ULocale loc, boolean len, String parString, String patt, String expResult) {
            locale = loc;
            leniency = len;
            pattern = patt;
            parseString = parString;
            expectedResult = expResult;
        }
    }
    ;
    final TestDateFormatLeniencyItem[] items = { // locale               leniency    parse String    pattern             expected result
    new TestDateFormatLeniencyItem(ULocale.ENGLISH, true, "2008-07 02", "yyyy-LLLL dd", "2008-July 02"), new TestDateFormatLeniencyItem(ULocale.ENGLISH, false, "2008-07 02", "yyyy-LLLL dd", null), new TestDateFormatLeniencyItem(ULocale.ENGLISH, true, "2008-Jan 02", "yyyy-LLL. dd", "2008-Jan. 02"), new TestDateFormatLeniencyItem(ULocale.ENGLISH, false, "2008-Jan 02", "yyyy-LLL. dd", null), new TestDateFormatLeniencyItem(ULocale.ENGLISH, true, "2008-Jan--02", "yyyy-MMM' -- 'dd", "2008-Jan -- 02"), new TestDateFormatLeniencyItem(ULocale.ENGLISH, false, "2008-Jan--02", "yyyy-MMM' -- 'dd", null) };
    for (TestDateFormatLeniencyItem item : items) {
        SimpleDateFormat sdfmt = new SimpleDateFormat(item.pattern, item.locale);
        sdfmt.setBooleanAttribute(BooleanAttribute.PARSE_ALLOW_WHITESPACE, item.leniency).setBooleanAttribute(BooleanAttribute.PARSE_ALLOW_NUMERIC, item.leniency).setBooleanAttribute(BooleanAttribute.PARSE_PARTIAL_LITERAL_MATCH, item.leniency);
        ParsePosition p = new ParsePosition(0);
        Date d = sdfmt.parse(item.parseString, p);
        if (item.expectedResult == null) {
            if (p.getErrorIndex() != -1)
                continue;
            else
                errln("error: unexpected parse success..." + item.parseString + " w/ lenient=" + item.leniency + " should have failed");
        }
        if (p.getErrorIndex() != -1) {
            errln("error: parse error for string " + item.parseString + " -- idx[" + p.getIndex() + "] errIdx[" + p.getErrorIndex() + "]");
            continue;
        }
        String result = sdfmt.format(d);
        if (!result.equalsIgnoreCase(item.expectedResult)) {
            errln("error: unexpected format result. expected - " + item.expectedResult + "  but result was - " + result);
        } else {
            logln("formatted results match! - " + result);
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 100 with SimpleDateFormat

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

the class DateFormatTest method TestDateFormatZone061.

/**
 * Test the formatting of time zones.
 */
@Test
public void TestDateFormatZone061() {
    Date date;
    DateFormat formatter;
    date = new Date(859248000000L);
    logln("Date 1997/3/25 00:00 GMT: " + date);
    formatter = new SimpleDateFormat("dd-MMM-yyyyy HH:mm", Locale.UK);
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    String temp = formatter.format(date);
    logln("Formatted in GMT to: " + temp);
    try {
        Date tempDate = formatter.parse(temp);
        logln("Parsed to: " + tempDate);
        if (!tempDate.equals(date))
            errln("FAIL: Expected " + date + " Got: " + tempDate);
    } catch (Throwable t) {
        System.out.println(t);
    }
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) 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