Search in sources :

Example 56 with SimpleDateFormat

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

the class DateFormatTest method TestGenericTime.

@Test
public void TestGenericTime() {
    // any zone pattern should parse any zone
    Locale en = new Locale("en", "", "");
    String[] ZDATA = { "yyyy MM dd HH:mm zzz", // round trip
    "y/M/d H:mm zzzz", "F", "2004 01 01 01:00 PST", "2004/1/1 1:00 Pacific Standard Time", "y/M/d H:mm zzz", "F", "2004 01 01 01:00 PST", "2004/1/1 1:00 PST", "y/M/d H:mm vvvv", "F", "2004 01 01 01:00 PST", "2004/1/1 1:00 Pacific Time", "y/M/d H:mm v", "F", "2004 01 01 01:00 PST", "2004/1/1 1:00 PT", // non-generic timezone string influences dst offset even if wrong for date/time
    "y/M/d H:mm zzz", "pf", "2004/1/1 1:00 PDT", "2004 01 01 01:00 PDT", "2004/1/1 0:00 PST", "y/M/d H:mm vvvv", "pf", "2004/1/1 1:00 PDT", "2004 01 01 01:00 PDT", "2004/1/1 0:00 Pacific Time", "y/M/d H:mm zzz", "pf", "2004/7/1 1:00 PST", "2004 07 01 02:00 PDT", "2004/7/1 2:00 PDT", "y/M/d H:mm vvvv", "pf", "2004/7/1 1:00 PST", "2004 07 01 02:00 PDT", "2004/7/1 2:00 Pacific Time", // generic timezone generates dst offset appropriate for local time
    "y/M/d H:mm zzz", "pf", "2004/1/1 1:00 PT", "2004 01 01 01:00 PST", "2004/1/1 1:00 PST", "y/M/d H:mm vvvv", "pf", "2004/1/1 1:00 PT", "2004 01 01 01:00 PST", "2004/1/1 1:00 Pacific Time", "y/M/d H:mm zzz", "pf", "2004/7/1 1:00 PT", "2004 07 01 01:00 PDT", "2004/7/1 1:00 PDT", "y/M/d H:mm vvvv", "pf", "2004/7/1 1:00 PT", "2004 07 01 01:00 PDT", "2004/7/1 1:00 Pacific Time", // time to parse does not really exist, PT interpreted as earlier time
    "y/M/d H:mm zzz", "pf", "2005/4/3 2:30 PT", "2005 04 03 03:30 PDT", "2005/4/3 3:30 PDT", "y/M/d H:mm zzz", "pf", "2005/4/3 2:30 PST", "2005 04 03 03:30 PDT", "2005/4/3 3:30 PDT", "y/M/d H:mm zzz", "pf", "2005/4/3 2:30 PDT", "2005 04 03 01:30 PST", "2005/4/3 1:30 PST", "y/M/d H:mm v", "pf", "2005/4/3 2:30 PT", "2005 04 03 03:30 PDT", "2005/4/3 3:30 PT", "y/M/d H:mm v", "pf", "2005/4/3 2:30 PST", "2005 04 03 03:30 PDT", "2005/4/3 3:30 PT", "y/M/d H:mm v", "pf", "2005/4/3 2:30 PDT", "2005 04 03 01:30 PST", "2005/4/3 1:30 PT", "y/M/d H:mm", "pf", "2005/4/3 2:30", "2005 04 03 03:30 PDT", "2005/4/3 3:30", // time to parse is ambiguous, PT interpreted as later time
    "y/M/d H:mm zzz", "pf", "2005/10/30 1:30 PT", "2005 10 30 01:30 PST", "2005/10/30 1:30 PST", "y/M/d H:mm v", "pf", "2005/10/30 1:30 PT", "2005 10 30  01:30 PST", "2005/10/30 1:30 PT", "y/M/d H:mm", "pf", "2005/10/30 1:30 PT", "2005 10 30 01:30 PST", "2005/10/30 1:30", "y/M/d H:mm zzz", "pf", "2004/10/31 1:30 PT", "2004 10 31 01:30 PST", "2004/10/31 1:30 PST", "y/M/d H:mm zzz", "pf", "2004/10/31 1:30 PST", "2004 10 31 01:30 PST", "2004/10/31 1:30 PST", "y/M/d H:mm zzz", "pf", "2004/10/31 1:30 PDT", "2004 10 31 01:30 PDT", "2004/10/31 1:30 PDT", "y/M/d H:mm v", "pf", "2004/10/31 1:30 PT", "2004 10 31 01:30 PST", "2004/10/31 1:30 PT", "y/M/d H:mm v", "pf", "2004/10/31 1:30 PST", "2004 10 31 01:30 PST", "2004/10/31 1:30 PT", "y/M/d H:mm v", "pf", "2004/10/31 1:30 PDT", "2004 10 31 01:30 PDT", "2004/10/31 1:30 PT", "y/M/d H:mm", "pf", "2004/10/31 1:30", "2004 10 31 01:30 PST", "2004/10/31 1:30" // Below is actually an invalid test case.  See the note in #5910.  Disable the case for now.
    // TODO: Revisit after 3.8
    // "y/M/d H:mm vvvv", "pf", "2004/10/31 1:30 Argentina Time", "2004 10 30 21:30 PDT", "2004/10/31 1:30 Argentina Time",
    };
    expect(ZDATA, en, true);
    logln("cross format/parse tests");
    final String basepat = "yy/MM/dd H:mm ";
    final SimpleDateFormat[] formats = { new SimpleDateFormat(basepat + "v", en), new SimpleDateFormat(basepat + "vvvv", en), new SimpleDateFormat(basepat + "zzz", en), new SimpleDateFormat(basepat + "zzzz", en) };
    final SimpleDateFormat univ = new SimpleDateFormat("yyyy MM dd HH:mm zzz", en);
    // To allow cross pattern parsing, we need to set ParseOption.ALL_STYLES
    TimeZoneFormat tzfmt = univ.getTimeZoneFormat().cloneAsThawed();
    tzfmt.setDefaultParseOptions(EnumSet.of(ParseOption.ALL_STYLES));
    tzfmt.freeze();
    univ.setTimeZoneFormat(tzfmt);
    for (SimpleDateFormat sdf : formats) {
        sdf.setTimeZoneFormat(tzfmt);
    }
    final String[] times = { "2004 01 02 03:04 PST", "2004 07 08 09:10 PDT" };
    for (int i = 0; i < times.length; ++i) {
        try {
            Date d = univ.parse(times[i]);
            logln("time: " + d);
            for (int j = 0; j < formats.length; ++j) {
                String test = formats[j].format(d);
                logln("test: '" + test + "'");
                for (int k = 0; k < formats.length; ++k) {
                    try {
                        Date t = formats[k].parse(test);
                        if (!d.equals(t)) {
                            errln("format " + k + " incorrectly parsed output of format " + j + " (" + test + "), returned " + t + " instead of " + d);
                        } else {
                            logln("format " + k + " parsed ok");
                        }
                    } catch (ParseException e) {
                        errln("format " + k + " could not parse output of format " + j + " (" + test + ")");
                    }
                }
            }
        } catch (ParseException e) {
            errln("univ could not parse: " + times[i]);
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) TimeZoneFormat(android.icu.text.TimeZoneFormat) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 57 with SimpleDateFormat

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

the class DateFormatTest method tryPat994.

// internal test subroutine, used by TestPartialParse994
public void tryPat994(SimpleDateFormat format, String pat, String str, Date expected) {
    Date Null = null;
    logln("Pattern \"" + pat + "\"   String \"" + str + "\"");
    try {
        format.applyPattern(pat);
        Date date = format.parse(str);
        String f = ((DateFormat) format).format(date);
        logln(" parse(" + str + ") -> " + date);
        logln(" format -> " + f);
        if (expected.equals(Null) || !date.equals(expected))
            // " + expected);
            errln("FAIL: Expected null");
        if (!f.equals(str))
            errln("FAIL: Expected " + str);
    } catch (ParseException e) {
        logln("ParseException: " + e.getMessage());
        if (!(expected == Null))
            errln("FAIL: Expected " + expected);
    } catch (Exception e) {
        errln("*** Exception:");
        e.printStackTrace();
    }
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParseException(java.text.ParseException) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 58 with SimpleDateFormat

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

the class DateFormatTest method TestMinuteSecondFieldsInOddPlaces.

public void TestMinuteSecondFieldsInOddPlaces() {
    // Some times on 2015-11-13 (UTC+0).
    long k000000 = 1447372800000L;
    long k000030 = 1447372830000L;
    long k003000 = 1447374600000L;
    long k060030 = 1447394430000L;
    long k063000 = 1447396200000L;
    // Apply pattern through constructor to make sure parsePattern() is called during initialization.
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm 'ss' bbbb");
    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.
    // Seconds field is not present.
    // assertEquals("hh:mm 'ss' bbbb | 00:00:30", "12:00 ss midnight", sdf.format(k000030));
    assertEquals("hh:mm 'ss' bbbb | 00:00:30", "12:00 ss AM", sdf.format(k000030));
    assertEquals("hh:mm 'ss' bbbb | 06:00:30", "06:00 ss AM", sdf.format(k060030));
    sdf.applyPattern("hh:mm 'ss' BBBB");
    // assertEquals("hh:mm 'ss' BBBB | 00:00:30", "12:00 ss midnight", sdf.format(k000030));
    assertEquals("hh:mm 'ss' BBBB | 00:00:30", "12:00 ss at night", sdf.format(k000030));
    assertEquals("hh:mm 'ss' BBBB | 06:00:30", "06:00 ss in the morning", sdf.format(k060030));
    // Minutes field is not present.
    sdf.applyPattern("hh 'mm ss' bbbb");
    // assertEquals("hh 'mm ss' bbbb | 00:30:00", "12 mm ss midnight", sdf.format(k003000));
    assertEquals("hh 'mm ss' bbbb | 00:30:00", "12 mm ss AM", sdf.format(k003000));
    assertEquals("hh 'mm ss' bbbb | 06:30:00", "06 mm ss AM", sdf.format(k063000));
    sdf.applyPattern("hh 'mm ss' BBBB");
    // assertEquals("hh 'mm ss' BBBB | 00:30:00", "12 mm ss midnight", sdf.format(k003000));
    assertEquals("hh 'mm ss' BBBB | 00:30:00", "12 mm ss at night", sdf.format(k003000));
    assertEquals("hh 'mm ss' BBBB | 06:30:00", "06 mm ss in the morning", sdf.format(k063000));
    // Minutes and seconds fields appear after day periods.
    sdf.applyPattern("bbbb hh:mm:ss");
    // assertEquals("bbbb hh:mm:ss | 00:00:00", "midnight 12:00:00", sdf.format(k000000));
    assertEquals("bbbb hh:mm:ss | 00:00:00", "AM 12:00:00", sdf.format(k000000));
    assertEquals("bbbb hh:mm:ss | 00:00:30", "AM 12:00:30", sdf.format(k000030));
    assertEquals("bbbb hh:mm:ss | 00:30:00", "AM 12:30:00", sdf.format(k003000));
    sdf.applyPattern("BBBB hh:mm:ss");
    // assertEquals("BBBB hh:mm:ss | 00:00:00", "midnight 12:00:00", sdf.format(k000000));
    assertEquals("BBBB hh:mm:ss | 00:00:00", "at night 12:00:00", sdf.format(k000000));
    assertEquals("BBBB hh:mm:ss | 00:00:30", "at night 12:00:30", sdf.format(k000030));
    assertEquals("BBBB hh:mm:ss | 00:30:00", "at night 12:30:00", sdf.format(k003000));
    // Confirm applyPattern() reparses the pattern string.
    sdf.applyPattern("BBBB hh");
    // assertEquals("BBBB hh | 00:00:30", "midnight 12", sdf.format(k000030));
    assertEquals("BBBB hh | 00:00:30", "at night 12", sdf.format(k000030));
    sdf.applyPattern("BBBB hh:mm:'ss'");
    // assertEquals("BBBB hh:mm:'ss' | 00:00:30", "midnight 12:00:ss", sdf.format(k000030));
    assertEquals("BBBB hh:mm:'ss' | 00:00:30", "at night 12:00:ss", sdf.format(k000030));
    sdf.applyPattern("BBBB hh:mm:ss");
    assertEquals("BBBB hh:mm:ss | 00:00:30", "at night 12:00:30", sdf.format(k000030));
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat)

Example 59 with SimpleDateFormat

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

the class DateFormatTest method TestLetterDPattern212.

/**
 * Test the handling of 'D' in patterns.
 */
@Test
public void TestLetterDPattern212() {
    String dateString = "1995-040.05:01:29";
    String bigD = "yyyy-DDD.hh:mm:ss";
    String littleD = "yyyy-ddd.hh:mm:ss";
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(1995, 0, 1, 5, 1, 29);
    Date expLittleD = cal.getTime();
    Date expBigD = new Date((long) (expLittleD.getTime() + 39 * 24 * 3600000.0));
    // Expect the same, with default lenient parsing
    expLittleD = expBigD;
    logln("dateString= " + dateString);
    SimpleDateFormat formatter = new SimpleDateFormat(bigD);
    ParsePosition pos = new ParsePosition(0);
    Date myDate = formatter.parse(dateString, pos);
    logln("Using " + bigD + " . " + myDate);
    if (!myDate.equals(expBigD))
        errln("FAIL: Expected " + expBigD);
    formatter = new SimpleDateFormat(littleD);
    pos = new ParsePosition(0);
    myDate = formatter.parse(dateString, pos);
    logln("Using " + littleD + " . " + myDate);
    if (!myDate.equals(expLittleD))
        errln("FAIL: Expected " + expLittleD);
}
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) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 60 with SimpleDateFormat

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

the class DateFormatTest method TestSimpleDateFormatConstructor_String_String_ULocale.

/*
     * Tests the constructor public SimpleDateFormat(String pattern, String override, ULocale loc)
     */
@Test
public void TestSimpleDateFormatConstructor_String_String_ULocale() {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("", "", null);
        sdf = (SimpleDateFormat) sdf.clone();
    } catch (Exception e) {
        errln("SimpleDateFormat(String pattern, String override, ULocale loc) " + "was not suppose to return an exception when constructing a new " + "SimpleDateFormat object.");
    }
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat) 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