Search in sources :

Example 66 with SimpleDateFormat

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

the class DateFormatTest method TestTimeZoneDisplayName.

@Test
public void TestTimeZoneDisplayName() {
    Calendar cal = new GregorianCalendar();
    SimpleDateFormat testfmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    testfmt.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));
    for (int i = 0; i < fallbackTests.length; ++i) {
        String[] info = fallbackTests[i];
        logln(info[0] + ";" + info[1] + ";" + info[2] + ";" + info[3]);
        long time = 0;
        try {
            Date testd = testfmt.parse(info[2]);
            time = testd.getTime();
        } catch (ParseException pe) {
            errln("Failed to parse test date data");
            continue;
        }
        ULocale l = new ULocale(info[0]);
        TimeZone tz = TimeZone.getTimeZone(info[1]);
        SimpleDateFormat fmt = new SimpleDateFormat(info[3], l);
        cal.setTimeInMillis(time);
        cal.setTimeZone(tz);
        String result = fmt.format(cal);
        if (!result.equals(info[4])) {
            errln(info[0] + ";" + info[1] + ";" + info[2] + ";" + info[3] + " expected: '" + info[4] + "' but got: '" + result + "'");
        }
    }
}
Also used : TimeZone(android.icu.util.TimeZone) 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) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 67 with SimpleDateFormat

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

the class DateFormatTest method TestParseMultiPatternMatch.

@Test
public void TestParseMultiPatternMatch() {
    class TestMultiPatternMatchItem {

        public boolean leniency;

        public String parseString;

        public String pattern;

        // null indicates expected error
        public String expectedResult;

        // Simple constructor
        public TestMultiPatternMatchItem(boolean len, String parString, String patt, String expResult) {
            leniency = len;
            pattern = patt;
            parseString = parString;
            expectedResult = expResult;
        }
    }
    ;
    final TestMultiPatternMatchItem[] items = { // leniency    parse String                  pattern                 expected result
    new TestMultiPatternMatchItem(true, "2013-Sep 13", "yyyy-MMM dd", "2013-Sep 13"), new TestMultiPatternMatchItem(true, "2013-September 14", "yyyy-MMM dd", "2013-Sep 14"), new TestMultiPatternMatchItem(false, "2013-September 15", "yyyy-MMM dd", null), new TestMultiPatternMatchItem(false, "2013-September 16", "yyyy-MMMM dd", "2013-September 16"), new TestMultiPatternMatchItem(true, "2013-Sep 17", "yyyy-LLL dd", "2013-Sep 17"), new TestMultiPatternMatchItem(true, "2013-September 18", "yyyy-LLL dd", "2013-Sep 18"), new TestMultiPatternMatchItem(false, "2013-September 19", "yyyy-LLL dd", null), new TestMultiPatternMatchItem(false, "2013-September 20", "yyyy-LLLL dd", "2013-September 20"), new TestMultiPatternMatchItem(true, "2013 Sat Sep 21", "yyyy EEE MMM dd", "2013 Sat Sep 21"), new TestMultiPatternMatchItem(true, "2013 Sunday Sep 22", "yyyy EEE MMM dd", "2013 Sun Sep 22"), new TestMultiPatternMatchItem(false, "2013 Monday Sep 23", "yyyy EEE MMM dd", null), new TestMultiPatternMatchItem(false, "2013 Tuesday Sep 24", "yyyy EEEE MMM dd", "2013 Tuesday Sep 24"), new TestMultiPatternMatchItem(true, "2013 Wed Sep 25", "yyyy eee MMM dd", "2013 Wed Sep 25"), new TestMultiPatternMatchItem(true, "2013 Thu Sep 26", "yyyy eee MMM dd", "2013 Thu Sep 26"), new TestMultiPatternMatchItem(false, "2013 Friday Sep 27", "yyyy eee MMM dd", null), new TestMultiPatternMatchItem(false, "2013 Saturday Sep 28", "yyyy eeee MMM dd", "2013 Saturday Sep 28"), new TestMultiPatternMatchItem(true, "2013 Sun Sep 29", "yyyy ccc MMM dd", "2013 Sun Sep 29"), new TestMultiPatternMatchItem(true, "2013 Monday Sep 30", "yyyy ccc MMM dd", "2013 Mon Sep 30"), new TestMultiPatternMatchItem(false, "2013 Sunday Oct 13", "yyyy ccc MMM dd", null), new TestMultiPatternMatchItem(false, "2013 Monday Oct 14", "yyyy cccc MMM dd", "2013 Monday Oct 14"), new TestMultiPatternMatchItem(true, "2013 Oct 15 Q4", "yyyy MMM dd QQQ", "2013 Oct 15 Q4"), new TestMultiPatternMatchItem(true, "2013 Oct 16 4th quarter", "yyyy MMM dd QQQ", "2013 Oct 16 Q4"), new TestMultiPatternMatchItem(false, "2013 Oct 17 4th quarter", "yyyy MMM dd QQQ", null), new TestMultiPatternMatchItem(false, "2013 Oct 18 Q4", "yyyy MMM dd QQQ", "2013 Oct 18 Q4"), new TestMultiPatternMatchItem(true, "2013 Oct 19 Q4", "yyyy MMM dd qqqq", "2013 Oct 19 4th quarter"), new TestMultiPatternMatchItem(true, "2013 Oct 20 4th quarter", "yyyy MMM dd qqqq", "2013 Oct 20 4th quarter"), new TestMultiPatternMatchItem(false, "2013 Oct 21 Q4", "yyyy MMM dd qqqq", null), new TestMultiPatternMatchItem(false, "2013 Oct 22 4th quarter", "yyyy MMM dd qqqq", "2013 Oct 22 4th quarter") };
    StringBuffer result = new StringBuffer();
    Date d = new Date();
    GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.US);
    SimpleDateFormat sdfmt = new SimpleDateFormat();
    ParsePosition p = new ParsePosition(0);
    for (TestMultiPatternMatchItem item : items) {
        cal.clear();
        sdfmt.setCalendar(cal);
        sdfmt.applyPattern(item.pattern);
        sdfmt.setLenient(item.leniency);
        sdfmt.setBooleanAttribute(BooleanAttribute.PARSE_MULTIPLE_PATTERNS_FOR_MATCH, item.leniency);
        result.setLength(0);
        p.setIndex(0);
        p.setErrorIndex(-1);
        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;
        }
        cal.setTime(d);
        result = sdfmt.format(cal, result, new FieldPosition(0));
        if (!result.toString().equalsIgnoreCase(item.expectedResult)) {
            errln("error: unexpected format result. expected - " + item.expectedResult + "  but result was - " + result);
        } else {
            logln("formatted results match! - " + result.toString());
        }
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 68 with SimpleDateFormat

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

the class DateFormatTest method TestAmPmMidnightNoon.

@Test
public void TestAmPmMidnightNoon() {
    // Some times on 2015-11-13.
    long k000000 = 1447372800000L;
    long k000030 = 1447372830000L;
    long k003000 = 1447374600000L;
    long k060000 = 1447394400000L;
    long k120000 = 1447416000000L;
    long k180000 = 1447437600000L;
    // Short.
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss bbb");
    sdf.setTimeZone(TimeZone.GMT_ZONE);
    // Note: "midnight" can be ambiguous as to whether it refers to beginning of day or end of day.
    // For ICU 57 output of "midnight" is temporarily suppressed.
    // assertEquals("hh:mm:ss bbbb | 00:00:00", "12:00:00 midnight", sdf.format(k000000));
    assertEquals("hh:mm:ss bbbb | 00:00:00", "12:00:00 AM", sdf.format(k000000));
    assertEquals("hh:mm:ss bbbb | 00:00:30", "12:00:30 AM", sdf.format(k000030));
    assertEquals("hh:mm:ss bbbb | 00:30:00", "12:30:00 AM", sdf.format(k003000));
    assertEquals("hh:mm:ss bbbb | 06:00:00", "06:00:00 AM", sdf.format(k060000));
    assertEquals("hh:mm:ss bbbb | 12:00:00", "12:00:00 noon", sdf.format(k120000));
    assertEquals("hh:mm:ss bbbb | 18:00:00", "06:00:00 PM", sdf.format(k180000));
    sdf.applyPattern("hh:mm bbb");
    // assertEquals("hh:mm bbb | 00:00:00", "12:00 midnight", sdf.format(k000000));
    assertEquals("hh:mm bbb | 00:00:00", "12:00 AM", sdf.format(k000000));
    // assertEquals("hh:mm bbb | 00:00:30", "12:00 midnight", sdf.format(k000030));
    assertEquals("hh:mm bbb | 00:00:30", "12:00 AM", sdf.format(k000030));
    assertEquals("hh:mm bbb | 00:30:00", "12:30 AM", sdf.format(k003000));
    sdf.applyPattern("hh bbb");
    // assertEquals("hh bbb | 00:00:00", "12 midnight", sdf.format(k000000));
    assertEquals("hh bbb | 00:00:00", "12 AM", sdf.format(k000000));
    // assertEquals("hh bbb | 00:00:30", "12 midnight", sdf.format(k000030));
    assertEquals("hh bbb | 00:00:30", "12 AM", sdf.format(k000030));
    // assertEquals("hh bbb | 00:30:00", "12 midnight", sdf.format(k003000));
    assertEquals("hh bbb | 00:30:00", "12 AM", sdf.format(k003000));
    // Wide.
    sdf.applyPattern("hh:mm:ss bbbb");
    // assertEquals("hh:mm:ss bbbb | 00:00:00", "12:00:00 midnight", sdf.format(k000000));
    assertEquals("hh:mm:ss bbbb | 00:00:00", "12:00:00 AM", sdf.format(k000000));
    assertEquals("hh:mm:ss bbbb | 00:00:30", "12:00:30 AM", sdf.format(k000030));
    assertEquals("hh:mm:ss bbbb | 00:30:00", "12:30:00 AM", sdf.format(k003000));
    assertEquals("hh:mm:ss bbbb | 06:00:00", "06:00:00 AM", sdf.format(k060000));
    assertEquals("hh:mm:ss bbbb | 12:00:00", "12:00:00 noon", sdf.format(k120000));
    assertEquals("hh:mm:ss bbbb | 18:00:00", "06:00:00 PM", sdf.format(k180000));
    sdf.applyPattern("hh:mm bbbb");
    // assertEquals("hh:mm bbbb | 00:00:00", "12:00 midnight", sdf.format(k000000));
    assertEquals("hh:mm bbbb | 00:00:00", "12:00 AM", sdf.format(k000000));
    // assertEquals("hh:mm bbbb | 00:00:30", "12:00 midnight", sdf.format(k000030));
    assertEquals("hh:mm bbbb | 00:00:30", "12:00 AM", sdf.format(k000030));
    assertEquals("hh:mm bbbb | 00:30:00", "12:30 AM", sdf.format(k003000));
    sdf.applyPattern("hh bbbb");
    // assertEquals("hh bbbb | 00:00:00", "12 midnight", sdf.format(k000000));
    assertEquals("hh bbbb | 00:00:00", "12 AM", sdf.format(k000000));
    // assertEquals("hh bbbb | 00:00:30", "12 midnight", sdf.format(k000030));
    assertEquals("hh bbbb | 00:00:30", "12 AM", sdf.format(k000030));
    // assertEquals("hh bbbb | 00:30:00", "12 midnight", sdf.format(k003000));
    assertEquals("hh bbbb | 00:30:00", "12 AM", sdf.format(k003000));
    // Narrow.
    sdf.applyPattern("hh:mm:ss bbbbb");
    // assertEquals("hh:mm:ss bbbbb | 00:00:00", "12:00:00 mi", sdf.format(k000000));
    assertEquals("hh:mm:ss bbbbb | 00:00:00", "12:00:00 a", sdf.format(k000000));
    assertEquals("hh:mm:ss bbbbb | 00:00:30", "12:00:30 a", sdf.format(k000030));
    assertEquals("hh:mm:ss bbbbb | 00:30:00", "12:30:00 a", sdf.format(k003000));
    assertEquals("hh:mm:ss bbbbb | 06:00:00", "06:00:00 a", sdf.format(k060000));
    assertEquals("hh:mm:ss bbbbb | 12:00:00", "12:00:00 n", sdf.format(k120000));
    assertEquals("hh:mm:ss bbbbb | 18:00:00", "06:00:00 p", sdf.format(k180000));
    sdf.applyPattern("hh:mm bbbbb");
    // assertEquals("hh:mm bbbbb | 00:00:00", "12:00 mi", sdf.format(k000000));
    assertEquals("hh:mm bbbbb | 00:00:00", "12:00 a", sdf.format(k000000));
    // assertEquals("hh:mm bbbbb | 00:00:30", "12:00 mi", sdf.format(k000030));
    assertEquals("hh:mm bbbbb | 00:00:30", "12:00 a", sdf.format(k000030));
    assertEquals("hh:mm bbbbb | 00:30:00", "12:30 a", sdf.format(k003000));
    sdf.applyPattern("hh bbbbb");
    // assertEquals("hh bbbbb | 00:00:00", "12 mi", sdf.format(k000000));
    assertEquals("hh bbbbb | 00:00:00", "12 a", sdf.format(k000000));
    // assertEquals("hh bbbbb | 00:00:30", "12 mi", sdf.format(k000030));
    assertEquals("hh bbbbb | 00:00:30", "12 a", sdf.format(k000030));
    // assertEquals("hh bbbbb | 00:30:00", "12 mi", sdf.format(k003000));
    assertEquals("hh bbbbb | 00:30:00", "12 a", sdf.format(k003000));
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 69 with SimpleDateFormat

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

the class DateFormatTest method TestPartialParse994.

/**
 * Verify that strings which contain incomplete specifications are parsed
 * correctly.  In some instances, this means not being parsed at all, and
 * returning an appropriate error.
 */
@Test
public void TestPartialParse994() {
    SimpleDateFormat f = new SimpleDateFormat();
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(1997, 1 - 1, 17, 10, 11, 42);
    Date date = null;
    tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/17 10:11:42", cal.getTime());
    tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/17 10:", date);
    tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/17 10", date);
    tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/17 ", date);
    tryPat994(f, "yy/MM/dd HH:mm:ss", "97/01/17", date);
}
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 70 with SimpleDateFormat

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

the class DateFormatTest method parse2DigitYear.

// internal test subroutine, used by TestTwoDigitYear
public void parse2DigitYear(DateFormat fmt, String str, Date expected) {
    try {
        Date d = fmt.parse(str);
        logln("Parsing \"" + str + "\" with " + ((SimpleDateFormat) fmt).toPattern() + "  => " + d);
        if (!d.equals(expected))
            errln("FAIL: Expected " + expected);
    } catch (ParseException e) {
        errln(e.getMessage());
    }
}
Also used : ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date)

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