Search in sources :

Example 51 with SimpleDateFormat

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

the class DateFormatTest method TestFlexibleDayPeriod.

public void TestFlexibleDayPeriod() {
    // 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 BBB | 00:00:00", "12:00:00 midnight", sdf.format(k000000));
    assertEquals("hh:mm:ss BBB | 00:00:00", "12:00:00 at night", sdf.format(k000000));
    assertEquals("hh:mm:ss BBB | 00:00:30", "12:00:30 at night", sdf.format(k000030));
    assertEquals("hh:mm:ss BBB | 00:30:00", "12:30:00 at night", sdf.format(k003000));
    assertEquals("hh:mm:ss BBB | 06:00:00", "06:00:00 in the morning", sdf.format(k060000));
    assertEquals("hh:mm:ss BBB | 12:00:00", "12:00:00 noon", sdf.format(k120000));
    assertEquals("hh:mm:ss BBB | 18:00:00", "06:00:00 in the evening", 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 at night", 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 at night", sdf.format(k000030));
    assertEquals("hh:mm BBB | 00:30:00", "12:30 at night", 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 at night", sdf.format(k000000));
    // assertEquals("hh BBB | 00:00:30", "12 midnight", sdf.format(k000030));
    assertEquals("hh BBB | 00:00:30", "12 at night", sdf.format(k000030));
    // assertEquals("hh BBB | 00:30:00", "12 midnight", sdf.format(k003000));
    assertEquals("hh BBB | 00:30:00", "12 at night", 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 at night", sdf.format(k000000));
    assertEquals("hh:mm:ss BBBB | 00:00:30", "12:00:30 at night", sdf.format(k000030));
    assertEquals("hh:mm:ss BBBB | 00:30:00", "12:30:00 at night", sdf.format(k003000));
    assertEquals("hh:mm:ss BBBB | 06:00:00", "06:00:00 in the morning", 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 in the evening", 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 at night", 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 at night", sdf.format(k000030));
    assertEquals("hh:mm BBBB | 00:30:00", "12:30 at night", 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 at night", sdf.format(k000000));
    // assertEquals("hh BBBB | 00:00:30", "12 midnight", sdf.format(k000030));
    assertEquals("hh BBBB | 00:00:30", "12 at night", sdf.format(k000030));
    // assertEquals("hh BBBB | 00:30:00", "12 midnight", sdf.format(k003000));
    assertEquals("hh BBBB | 00:30:00", "12 at night", 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 at night", sdf.format(k000000));
    assertEquals("hh:mm:ss BBBBB | 00:00:30", "12:00:30 at night", sdf.format(k000030));
    assertEquals("hh:mm:ss BBBBB | 00:30:00", "12:30:00 at night", sdf.format(k003000));
    assertEquals("hh:mm:ss BBBBB | 06:00:00", "06:00:00 in the morning", 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 in the evening", 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 at night", 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 at night", sdf.format(k000030));
    assertEquals("hh:mm BBBBB | 00:30:00", "12:30 at night", 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 at night", sdf.format(k000000));
    // assertEquals("hh BBBBB | 00:00:30", "12 mi", sdf.format(k000030));
    assertEquals("hh BBBBB | 00:00:30", "12 at night", sdf.format(k000030));
    // assertEquals("hh BBBBB | 00:30:00", "12 mi", sdf.format(k003000));
    assertEquals("hh BBBBB | 00:30:00", "12 at night", sdf.format(k003000));
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat)

Example 52 with SimpleDateFormat

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

the class DateFormatTest method TestDayPeriodParsing.

public void TestDayPeriodParsing() throws ParseException {
    // Some times on 2015-11-13 (UTC+0).
    Date k000000 = new Date(1447372800000L);
    Date k003700 = new Date(1447375020000L);
    Date k010000 = new Date(1447376400000L);
    Date k013000 = new Date(1447378200000L);
    Date k030000 = new Date(1447383600000L);
    Date k090000 = new Date(1447405200000L);
    Date k120000 = new Date(1447416000000L);
    Date k130000 = new Date(1447419600000L);
    Date k133700 = new Date(1447421820000L);
    Date k150000 = new Date(1447426800000L);
    Date k190000 = new Date(1447441200000L);
    Date k193000 = new Date(1447443000000L);
    Date k200000 = new Date(1447444800000L);
    Date k210000 = new Date(1447448400000L);
    SimpleDateFormat sdf = new SimpleDateFormat("");
    sdf.setTimeZone(TimeZone.GMT_ZONE);
    // 'B' -- flexible day periods
    // A day period on its own parses to the center of that period.
    sdf.applyPattern("yyyy-MM-dd B");
    assertEquals("yyyy-MM-dd B | 2015-11-13 midnight", k000000, sdf.parse("2015-11-13 midnight"));
    assertEquals("yyyy-MM-dd B | 2015-11-13 noon", k120000, sdf.parse("2015-11-13 noon"));
    assertEquals("yyyy-MM-dd B | 2015-11-13 in the afternoon", k150000, sdf.parse("2015-11-13 in the afternoon"));
    assertEquals("yyyy-MM-dd B | 2015-11-13 in the evening", k193000, sdf.parse("2015-11-13 in the evening"));
    assertEquals("yyyy-MM-dd B | 2015-11-13 at night", k013000, sdf.parse("2015-11-13 at night"));
    // If time and day period are consistent with each other then time is parsed accordingly.
    sdf.applyPattern("yyyy-MM-dd hh:mm B");
    assertEquals("yyyy-MM-dd hh:mm B | 2015-11-13 12:00 midnight", k000000, sdf.parse("2015-11-13 12:00 midnight"));
    assertEquals("yyyy-MM-dd hh:mm B | 2015-11-13 12:00 noon", k120000, sdf.parse("2015-11-13 12:00 noon"));
    assertEquals("yyyy-MM-dd hh:mm B | 2015-11-13 01:00 at night", k010000, sdf.parse("2015-11-13 01:00 at night"));
    assertEquals("yyyy-MM-dd hh:mm B | 2015-11-13 01:00 in the afternoon", k130000, sdf.parse("2015-11-13 01:00 in the afternoon"));
    assertEquals("yyyy-MM-dd hh:mm B | 2015-11-13 09:00 in the morning", k090000, sdf.parse("2015-11-13 09:00 in the morning"));
    assertEquals("yyyy-MM-dd hh:mm B | 2015-11-13 09:00 at night", k210000, sdf.parse("2015-11-13 09:00 at night"));
    // If the hour is 13 thru 23 then day period has no effect on time (since time is assumed
    // to be in 24-hour format).
    // TODO: failing!
    sdf.applyPattern("yyyy-MM-dd HH:mm B");
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 13:37 midnight", k133700, sdf.parse("2015-11-13 13:37 midnight"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 13:37 noon", k133700, sdf.parse("2015-11-13 13:37 noon"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 13:37 at night", k133700, sdf.parse("2015-11-13 13:37 at night"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 13:37 in the afternoon", k133700, sdf.parse("2015-11-13 13:37 in the afternoon"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 13:37 in the morning", k133700, sdf.parse("2015-11-13 13:37 in the morning"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 13:37 at night", k133700, sdf.parse("2015-11-13 13:37 at night"));
    // Hour 0 is synonymous with hour 12 when parsed with 'h'.
    // This unfortunately means we have to tolerate "0 noon" as it's synonymous with "12 noon".
    sdf.applyPattern("yyyy-MM-dd hh:mm B");
    assertEquals("yyyy-MM-dd hh:mm B | 2015-11-13 00:00 midnight", k000000, sdf.parse("2015-11-13 00:00 midnight"));
    assertEquals("yyyy-MM-dd hh:mm B | 2015-11-13 00:00 noon", k120000, sdf.parse("2015-11-13 00:00 noon"));
    // But when parsed with 'H', 0 indicates a 24-hour time, therefore we disregard the day period.
    sdf.applyPattern("yyyy-MM-dd HH:mm B");
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 00:37 midnight", k003700, sdf.parse("2015-11-13 00:37 midnight"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 00:37 noon", k003700, sdf.parse("2015-11-13 00:37 noon"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 00:37 at night", k003700, sdf.parse("2015-11-13 00:37 at night"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 00:37 in the afternoon", k003700, sdf.parse("2015-11-13 00:37 in the afternoon"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 00:37 in the morning", k003700, sdf.parse("2015-11-13 00:37 in the morning"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 00:37 at night", k003700, sdf.parse("2015-11-13 00:37 at night"));
    // Even when parsed with 'H', hours 1 thru 12 are considered 12-hour time and takes
    // day period into account in parsing.
    sdf.applyPattern("yyyy-MM-dd HH:mm B");
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 12:00 midnight", k000000, sdf.parse("2015-11-13 12:00 midnight"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 12:00 noon", k120000, sdf.parse("2015-11-13 12:00 noon"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 01:00 at night", k010000, sdf.parse("2015-11-13 01:00 at night"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 01:00 in the afternoon", k130000, sdf.parse("2015-11-13 01:00 in the afternoon"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 09:00 in the morning", k090000, sdf.parse("2015-11-13 09:00 in the morning"));
    assertEquals("yyyy-MM-dd HH:mm B | 2015-11-13 09:00 at night", k210000, sdf.parse("2015-11-13 09:00 at night"));
    // If a 12-hour time and the day period don't agree with each other, time is parsed as close
    // to the given day period as possible.
    sdf.applyPattern("yyyy-MM-dd hh:mm B");
    // AFTERNOON1 is [12, 18), but "7 in the afternoon" parses to 19:00.
    assertEquals("yyyy-MM-dd hh:mm B | 2015-11-13 07:00 in the afternoon", k190000, sdf.parse("2015-11-13 07:00 in the afternoon"));
    // NIGHT1 is [21, 6), but "8 at night" parses to 20:00.
    assertEquals("yyyy-MM-dd hh:mm B | 2015-11-13 08:00 at night", k200000, sdf.parse("2015-11-13 08:00 at night"));
    // 'b' -- fixed day periods (AM, PM, midnight, noon)
    // On their own, "midnight" parses to 00:00 and "noon" parses to 12:00.
    // AM and PM are handled by the 'a' parser (which doesn't handle this case well).
    sdf.applyPattern("yyyy-MM-dd b");
    assertEquals("yyyy-MM-dd b | 2015-11-13 midnight", k000000, sdf.parse("2015-11-13 midnight"));
    assertEquals("yyyy-MM-dd b | 2015-11-13 noon", k120000, sdf.parse("2015-11-13 noon"));
    // For 12-hour times, AM and PM should be parsed as if with pattern character 'a'.
    sdf.applyPattern("yyyy-MM-dd hh:mm b");
    assertEquals("yyyy-MM-dd hh:mm b | 2015-11-13 01:00 AM", k010000, sdf.parse("2015-11-13 01:00 AM"));
    assertEquals("yyyy-MM-dd hh:mm b | 2015-11-13 01:00 PM", k130000, sdf.parse("2015-11-13 01:00 PM"));
    // 12 midnight parses to 00:00, and 12 noon parses to 12:00.
    assertEquals("yyyy-MM-dd hh:mm b | 2015-11-13 12:00 midnight", k000000, sdf.parse("2015-11-13 12:00 midnight"));
    assertEquals("yyyy-MM-dd hh:mm b | 2015-11-13 12:00 noon", k120000, sdf.parse("2015-11-13 12:00 noon"));
    // Hours 13-23 indicate 24-hour time so we disregard "midnight" or "noon".
    // Again, AM and PM are handled by the 'a' parser which doesn't handle this case well.
    sdf.applyPattern("yyyy-MM-dd HH:mm b");
    assertEquals("yyyy-MM-dd HH:mm b | 2015-11-13 13:37 midnight", k133700, sdf.parse("2015-11-13 13:37 midnight"));
    assertEquals("yyyy-MM-dd HH:mm b | 2015-11-13 13:37 noon", k133700, sdf.parse("2015-11-13 13:37 noon"));
    // Hour 0 is synonymous with hour 12 when parsed with 'h'.
    // Again, this means we have to tolerate "0 noon" as it's synonymous with "12 noon".
    sdf.applyPattern("yyyy-MM-dd hh:mm b");
    assertEquals("yyyy-MM-dd hh:mm b | 2015-11-13 00:00 midnight", k000000, sdf.parse("2015-11-13 00:00 midnight"));
    assertEquals("yyyy-MM-dd hh:mm b | 2015-11-13 00:00 noon", k120000, sdf.parse("2015-11-13 00:00 noon"));
    // With 'H' though 0 indicates a 24-hour time, therefore we disregard the day period.
    sdf.applyPattern("yyyy-MM-dd HH:mm b");
    assertEquals("yyyy-MM-dd HH:mm b | 2015-11-13 00:37 midnight", k003700, sdf.parse("2015-11-13 00:37 midnight"));
    assertEquals("yyyy-MM-dd HH:mm b | 2015-11-13 00:37 noon", k003700, sdf.parse("2015-11-13 00:37 noon"));
    // If "midnight" or "noon" is parsed with a 12-hour time other than 12:00, choose
    // the version that's closer to the period given.
    sdf.applyPattern("yyyy-MM-dd hh:mm b");
    assertEquals("yyyy-MM-dd hh:mm b | 2015-11-13 03:00 midnight", k030000, sdf.parse("2015-11-13 03:00 midnight"));
    assertEquals("yyyy-MM-dd hh:mm b | 2015-11-13 03:00 noon", k150000, sdf.parse("2015-11-13 03:00 noon"));
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date)

Example 53 with SimpleDateFormat

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

the class DateFormatTest method TestGreekMay.

@Test
public void TestGreekMay() {
    Date date = new Date(-9896080848000L);
    SimpleDateFormat fmt = new SimpleDateFormat("EEEE, dd MMMM yyyy h:mm:ss a", new Locale("el", "", ""));
    String str = fmt.format(date);
    ParsePosition pos = new ParsePosition(0);
    Date d2 = fmt.parse(str, pos);
    if (!date.equals(d2)) {
        errln("FAIL: unable to parse strings where case-folding changes length");
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 54 with SimpleDateFormat

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

the class DateFormatTest method TestTwoDigitYearDSTParse.

/**
 * Test the parsing of 2-digit years.
 */
@Test
public void TestTwoDigitYearDSTParse() {
    SimpleDateFormat fullFmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss.SSS zzz yyyy G");
    SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yy h:mm:ss 'o''clock' a z", Locale.ENGLISH);
    String s = "03-Apr-04 2:20:47 o'clock AM PST";
    /*
         * SimpleDateFormat(pattern, locale) Construct a SimpleDateDateFormat using
         * the given pattern, the locale and using the TimeZone.getDefault();
         * So it need to add the timezone offset on hour field.
         * ps. the Method Calendar.getTime() used by SimpleDateFormat.parse() always
         * return Date value with TimeZone.getDefault() [Richard/GCL]
         */
    TimeZone defaultTZ = TimeZone.getDefault();
    TimeZone PST = TimeZone.getTimeZone("PST");
    int defaultOffset = defaultTZ.getRawOffset();
    int PSTOffset = PST.getRawOffset();
    int hour = 2 + (defaultOffset - PSTOffset) / (60 * 60 * 1000);
    // hour is the expected hour of day, in units of seconds
    hour = ((hour < 0) ? hour + 24 : hour) * 60 * 60;
    try {
        Date d = fmt.parse(s);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        // DSTOffset
        hour += defaultTZ.inDaylightTime(d) ? 1 : 0;
        logln(s + " P> " + ((DateFormat) fullFmt).format(d));
        // hr is the actual hour of day, in units of seconds
        // adjust for DST
        int hr = cal.get(Calendar.HOUR_OF_DAY) * 60 * 60 - cal.get(Calendar.DST_OFFSET) / 1000;
        if (hr != hour)
            errln("FAIL: Hour (-DST) = " + hr / (60 * 60.0) + "; expected " + hour / (60 * 60.0));
    } catch (ParseException e) {
        errln("Parse Error:" + e.getMessage());
    }
}
Also used : TimeZone(android.icu.util.TimeZone) 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) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 55 with SimpleDateFormat

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

the class DateFormatTest method TestRunTogetherPattern985.

/**
 * Verify the behavior of patterns in which digits for different fields run together
 * without intervening separators.
 */
@Test
public void TestRunTogetherPattern985() {
    String format = "yyyyMMddHHmmssSSS";
    String now, then;
    // UBool flag;
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    Date date1 = new Date();
    now = ((DateFormat) formatter).format(date1);
    logln(now);
    ParsePosition pos = new ParsePosition(0);
    Date date2 = formatter.parse(now, pos);
    if (date2 == null)
        then = "Parse stopped at " + pos.getIndex();
    else
        then = ((DateFormat) formatter).format(date2);
    logln(then);
    if (date2 == null || !date2.equals(date1))
        errln("FAIL");
}
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) ParsePosition(java.text.ParsePosition) 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