Search in sources :

Example 6 with GregorianCalendar

use of android.icu.util.GregorianCalendar in project j2objc by google.

the class DateFormatTest method TestErrorChecking.

@Test
public void TestErrorChecking() {
    try {
        DateFormat.getDateTimeInstance(-1, -1, Locale.US);
        errln("Expected exception for getDateTimeInstance(-1, -1, Locale)");
    } catch (IllegalArgumentException e) {
        logln("one ok");
    } catch (Exception e) {
        warnln("Expected IllegalArgumentException, got: " + e);
    }
    try {
        DateFormat df = new SimpleDateFormat("aaNNccc");
        df.format(new Date());
        errln("Expected exception for format with bad pattern");
    } catch (IllegalArgumentException ex) {
        logln("two ok");
    } catch (Exception e) {
        warnln("Expected IllegalArgumentException, got: " + e);
    }
    {
        // opposite of text
        SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yy");
        fmt.set2DigitYearStart(getDate(2003, Calendar.DECEMBER, 25));
        String text = "12/25/03";
        Calendar xcal = new GregorianCalendar();
        xcal.setLenient(false);
        ParsePosition pp = new ParsePosition(0);
        // should get parse error on second field, not lenient
        fmt.parse(text, xcal, pp);
        if (pp.getErrorIndex() == -1) {
            errln("Expected parse error");
        } else {
            logln("three ok");
        }
    }
}
Also used : 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) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParseException(java.text.ParseException) IOException(java.io.IOException) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 7 with GregorianCalendar

use of android.icu.util.GregorianCalendar in project j2objc by google.

the class DateFormatTest method TestRelativeDateFormat.

// based on TestRelativeDateFormat() in icu/trunk/source/test/cintltst/cdattst.c
@Test
public void TestRelativeDateFormat() {
    ULocale loc = ULocale.US;
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar cal = new GregorianCalendar(tz, loc);
    Date now = new Date();
    cal.setTime(now);
    cal.set(Calendar.HOUR_OF_DAY, 18);
    cal.set(Calendar.MINUTE, 49);
    cal.set(Calendar.SECOND, 0);
    Date today = cal.getTime();
    // minutes string to search for in formatted result
    String minutesStr = "49";
    int[] dateStylesList = { DateFormat.RELATIVE_FULL, DateFormat.RELATIVE_LONG, DateFormat.RELATIVE_MEDIUM, DateFormat.RELATIVE_SHORT };
    for (int i = 0; i < dateStylesList.length; i++) {
        int dateStyle = dateStylesList[i];
        DateFormat fmtRelDateTime = DateFormat.getDateTimeInstance(dateStyle, DateFormat.SHORT, loc);
        DateFormat fmtRelDate = DateFormat.getDateInstance(dateStyle, loc);
        DateFormat fmtTime = DateFormat.getTimeInstance(DateFormat.SHORT, loc);
        for (int dayOffset = -2; dayOffset <= 2; dayOffset++) {
            StringBuffer dateTimeStr = new StringBuffer(64);
            StringBuffer dateStr = new StringBuffer(64);
            StringBuffer timeStr = new StringBuffer(64);
            FieldPosition fp = new FieldPosition(DateFormat.MINUTE_FIELD);
            cal.setTime(today);
            cal.add(Calendar.DATE, dayOffset);
            fmtRelDateTime.format(cal, dateTimeStr, fp);
            fmtRelDate.format(cal, dateStr, new FieldPosition(0));
            fmtTime.format(cal, timeStr, new FieldPosition(0));
            logln(dayOffset + ", " + dateStyle + ", " + dateTimeStr);
            logln(dayOffset + ", " + dateStyle + ", " + dateStr);
            logln(dayOffset + ", " + dateStyle + ", " + timeStr);
            // check that dateStr is in dateTimeStr
            if (dateTimeStr.toString().indexOf(dateStr.toString()) < 0) {
                errln("relative date string not found in datetime format with timeStyle SHORT, dateStyle " + dateStyle + " for dayOffset " + dayOffset);
                errln("datetime format is " + dateTimeStr.toString() + ", date string is " + dateStr.toString());
            }
            // check that timeStr is in dateTimeStr
            if (dateTimeStr.toString().indexOf(timeStr.toString()) < 0) {
                errln("short time string not found in datetime format with timeStyle SHORT, dateStyle " + dateStyle + " for dayOffset " + dayOffset);
                errln("datetime format is " + dateTimeStr.toString() + ", time string is " + timeStr.toString());
            }
            // check index of minutesStr
            int minutesStrIndex = dateTimeStr.toString().indexOf(minutesStr);
            if (fp.getBeginIndex() != minutesStrIndex) {
                errln("FieldPosition beginIndex " + fp.getBeginIndex() + " instead of " + minutesStrIndex + " for datetime format with timeStyle SHORT, dateStyle " + dateStyle + " for dayOffset " + dayOffset);
                errln("datetime format is " + dateTimeStr.toString());
            }
        }
    }
}
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) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) FieldPosition(java.text.FieldPosition) Date(java.util.Date) Test(org.junit.Test)

Example 8 with GregorianCalendar

use of android.icu.util.GregorianCalendar 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 9 with GregorianCalendar

use of android.icu.util.GregorianCalendar in project j2objc by google.

the class CalendarRegressionTest method Test4288792.

/**
 * WEEK_OF_YEAR computed incorrectly. A failure of this test can indicate a problem in several different places in
 * the
 */
@Test
public void Test4288792() throws Exception {
    TimeZone savedTZ = TimeZone.getDefault();
    TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
    GregorianCalendar cal = new GregorianCalendar();
    for (int i = 1900; i < 2100; i++) {
        for (int j1 = 1; j1 <= 7; j1++) {
            // Loop for MinimalDaysInFirstWeek: 1..7
            for (int j = Calendar.SUNDAY; j <= Calendar.SATURDAY; j++) {
                // Loop for FirstDayOfWeek: SUNDAY..SATURDAY
                cal.clear();
                cal.setMinimalDaysInFirstWeek(j1);
                cal.setFirstDayOfWeek(j);
                // Set the calendar to the first day of the last week
                // of the year. This may overlap some of the start of
                // the next year; that is, the last week of 1999 may
                // include some of January 2000. Use the add() method
                // to advance through the week. For each day, call
                // get(WEEK_OF_YEAR). The result should be the same
                // for the whole week. Note that a bug in
                // getActualMaximum() will break this test.
                // Set date to the mid year first before getActualMaximum(WEEK_OF_YEAR).
                // getActualMaximum(WEEK_OF_YEAR) is based on the current calendar's
                // year of week of year. After clear(), calendar is set to January 1st,
                // which may belongs to previous year of week of year.
                cal.set(i, Calendar.JULY, 1);
                int maxWeek = cal.getActualMaximum(Calendar.WEEK_OF_YEAR);
                cal.set(Calendar.WEEK_OF_YEAR, maxWeek);
                cal.set(Calendar.DAY_OF_WEEK, j);
                for (int k = 1; k < 7; k++) {
                    cal.add(Calendar.DATE, 1);
                    int WOY = cal.get(Calendar.WEEK_OF_YEAR);
                    if (WOY != maxWeek) {
                        errln(cal.getTime() + ",got=" + WOY + ",expected=" + maxWeek + ",min=" + j1 + ",first=" + j);
                    }
                }
                // Now advance the calendar one more day. This should
                // put it at the first day of week 1 of the next year.
                cal.add(Calendar.DATE, 1);
                int WOY = cal.get(Calendar.WEEK_OF_YEAR);
                if (WOY != 1) {
                    errln(cal.getTime() + ",got=" + WOY + ",expected=1,min=" + j1 + ",first" + j);
                }
            }
        }
    }
    TimeZone.setDefault(savedTZ);
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 10 with GregorianCalendar

use of android.icu.util.GregorianCalendar in project j2objc by google.

the class CalendarRegressionTest method Test4096539.

@Test
public void Test4096539() {
    int[] y = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    for (int x = 0; x < 12; x++) {
        GregorianCalendar gc = new GregorianCalendar(1997, x, y[x]);
        int m1, m2;
        log((m1 = gc.get(Calendar.MONTH) + 1) + "/" + gc.get(Calendar.DATE) + "/" + gc.get(Calendar.YEAR) + " + 1mo = ");
        gc.add(Calendar.MONTH, 1);
        logln((m2 = gc.get(Calendar.MONTH) + 1) + "/" + gc.get(Calendar.DATE) + "/" + gc.get(Calendar.YEAR));
        int m = (m1 % 12) + 1;
        if (m2 != m)
            errln("Fail: Want " + m + " Got " + m2);
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Aggregations

GregorianCalendar (android.icu.util.GregorianCalendar)111 Test (org.junit.Test)102 Date (java.util.Date)63 Calendar (android.icu.util.Calendar)40 SimpleDateFormat (android.icu.text.SimpleDateFormat)34 SimpleTimeZone (android.icu.util.SimpleTimeZone)28 TimeZone (android.icu.util.TimeZone)28 DateFormat (android.icu.text.DateFormat)24 JapaneseCalendar (android.icu.util.JapaneseCalendar)17 ULocale (android.icu.util.ULocale)16 ChineseCalendar (android.icu.util.ChineseCalendar)15 IslamicCalendar (android.icu.util.IslamicCalendar)14 BuddhistCalendar (android.icu.util.BuddhistCalendar)12 ChineseDateFormat (android.icu.text.ChineseDateFormat)8 IOException (java.io.IOException)8 ParsePosition (java.text.ParsePosition)8 CalendarAstronomer (android.icu.impl.CalendarAstronomer)7 HebrewCalendar (android.icu.util.HebrewCalendar)7 FieldPosition (java.text.FieldPosition)7 ParseException (java.text.ParseException)6