Search in sources :

Example 26 with GregorianCalendar

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

the class CalendarRegressionTest method Test4142933.

/**
 * Bug states that ArrayIndexOutOfBoundsException is thrown by
 * GregorianCalendar.roll() when IllegalArgumentException should be.
 */
@Test
public void Test4142933() {
    GregorianCalendar calendar = new GregorianCalendar();
    try {
        calendar.roll(-1, true);
        errln("Test failed, no exception trown");
    } catch (IllegalArgumentException e) {
        // OK: Do nothing
        // logln("Test passed");
        System.out.print("");
    } catch (Exception e) {
        errln("Test failed. Unexpected exception is thrown: " + e);
        e.printStackTrace();
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) Test(org.junit.Test)

Example 27 with GregorianCalendar

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

the class CalendarRegressionTest method TestT8057.

/*
     * Test case for add/roll with non-lenient calendar reported by ticket#8057.
     * Calendar#add may result internal fields out of valid range. ICU used to
     * trigger field range validation also for internal field changes triggered
     * by add/roll, then throws IllegalArgumentException. The field value range
     * validation should be done only for fields set by user code explicitly
     * in non-lenient mode.
     */
@Test
public void TestT8057() {
    // Set the calendar to the last day in a leap year
    GregorianCalendar cal = new GregorianCalendar();
    cal.setLenient(false);
    cal.clear();
    cal.set(2008, Calendar.DECEMBER, 31);
    // Force calculating then fields once.
    long t = cal.getTimeInMillis();
    // 2009-12-31 00:00 PST
    long expected = 1262246400000L;
    try {
        cal.add(Calendar.YEAR, 1);
        t = cal.getTimeInMillis();
        if (t != expected) {
            errln("FAIL: wrong date after add: expected=" + expected + " returned=" + t);
        }
    } catch (IllegalArgumentException e) {
        errln("FAIL: add method should not throw IllegalArgumentException");
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 28 with GregorianCalendar

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

the class CalendarRegressionTest method Test4125881.

/**
 * Check isLeapYear for BC years.
 */
@Test
public void Test4125881() {
    GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
    DateFormat fmt = new SimpleDateFormat("MMMM d, yyyy G");
    cal.clear();
    for (int y = -20; y <= 10; ++y) {
        cal.set(Calendar.ERA, y < 1 ? GregorianCalendar.BC : GregorianCalendar.AD);
        cal.set(Calendar.YEAR, y < 1 ? 1 - y : y);
        logln(y + " = " + fmt.format(cal.getTime()) + " " + cal.isLeapYear(y));
        if (cal.isLeapYear(y) != ((y + 40) % 4 == 0))
            errln("Leap years broken");
    }
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 29 with GregorianCalendar

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

the class CalendarRegressionTest method Test4141665.

/**
 * GregorianCalendar.equals() ignores cutover date
 */
@Test
public void Test4141665() {
    GregorianCalendar cal = new GregorianCalendar();
    GregorianCalendar cal2 = (GregorianCalendar) cal.clone();
    Date cut = cal.getGregorianChange();
    // 100 days
    Date cut2 = new Date(cut.getTime() + 100 * 24 * 60 * 60 * 1000L);
    // later
    if (!cal.equals(cal2)) {
        errln("Cloned GregorianCalendars not equal");
    }
    cal2.setGregorianChange(cut2);
    if (cal.equals(cal2)) {
        errln("GregorianCalendar.equals() ignores cutover");
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 30 with GregorianCalendar

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

the class CalendarRegressionTest method Test4103271.

@Test
public void Test4103271() {
    SimpleDateFormat sdf = new SimpleDateFormat();
    int numYears = 40, startYear = 1997, numDays = 15;
    String output, testDesc;
    GregorianCalendar testCal = (GregorianCalendar) Calendar.getInstance();
    testCal.clear();
    sdf.setCalendar(testCal);
    sdf.applyPattern("d MMM yyyy");
    boolean fail = false;
    for (int firstDay = 1; firstDay <= 2; firstDay++) {
        for (int minDays = 1; minDays <= 7; minDays++) {
            testCal.setMinimalDaysInFirstWeek(minDays);
            testCal.setFirstDayOfWeek(firstDay);
            testDesc = ("Test" + String.valueOf(firstDay) + String.valueOf(minDays));
            logln(testDesc + " => 1st day of week=" + String.valueOf(firstDay) + ", minimum days in first week=" + String.valueOf(minDays));
            for (int j = startYear; j <= startYear + numYears; j++) {
                testCal.set(j, 11, 25);
                for (int i = 0; i < numDays; i++) {
                    testCal.add(Calendar.DATE, 1);
                    String calWOY;
                    int actWOY = testCal.get(Calendar.WEEK_OF_YEAR);
                    if (actWOY < 1 || actWOY > 53) {
                        Date d = testCal.getTime();
                        calWOY = String.valueOf(actWOY);
                        output = testDesc + " - " + sdf.format(d) + "\t";
                        output = output + "\t" + calWOY;
                        logln(output);
                        fail = true;
                    }
                }
            }
        }
    }
    int[] DATA = { 3, 52, 52, 52, 52, 52, 52, 52, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 4, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 1, 1, 1, 1, 1, 1, 1 };
    testCal.setFirstDayOfWeek(Calendar.SUNDAY);
    for (int j = 0; j < DATA.length; j += 22) {
        logln("Minimal days in first week = " + DATA[j] + "  Week starts on Sunday");
        testCal.setMinimalDaysInFirstWeek(DATA[j]);
        testCal.set(1997, Calendar.DECEMBER, 21);
        for (int i = 0; i < 21; ++i) {
            int woy = testCal.get(Calendar.WEEK_OF_YEAR);
            log(testCal.getTime() + " " + woy);
            if (woy != DATA[j + 1 + i]) {
                log(" ERROR");
                fail = true;
            }
            // logln();
            // Now compute the time from the fields, and make sure we
            // get the same answer back. This is a round-trip test.
            Date save = testCal.getTime();
            testCal.clear();
            testCal.set(Calendar.YEAR, DATA[j + 1 + i] < 25 ? 1998 : 1997);
            testCal.set(Calendar.WEEK_OF_YEAR, DATA[j + 1 + i]);
            testCal.set(Calendar.DAY_OF_WEEK, (i % 7) + Calendar.SUNDAY);
            if (!testCal.getTime().equals(save)) {
                logln("  Parse failed: " + testCal.getTime());
                fail = true;
            }
            testCal.setTime(save);
            testCal.add(Calendar.DAY_OF_MONTH, 1);
        }
    }
    Date[] d = new Date[8];
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(1997, Calendar.DECEMBER, 28);
    d[0] = tempcal.getTime();
    tempcal.set(1998, Calendar.JANUARY, 10);
    d[1] = tempcal.getTime();
    tempcal.set(1998, Calendar.DECEMBER, 31);
    d[2] = tempcal.getTime();
    tempcal.set(1999, Calendar.JANUARY, 1);
    d[3] = tempcal.getTime();
    // Test field disambiguation with a few special hard-coded cases.
    // This shouldn't fail if the above cases aren't failing.
    Object[] DISAM = { new Integer(1998), new Integer(1), new Integer(Calendar.SUNDAY), d[0], new Integer(1998), new Integer(2), new Integer(Calendar.SATURDAY), d[1], new Integer(1998), new Integer(53), new Integer(Calendar.THURSDAY), d[2], new Integer(1998), new Integer(53), new Integer(Calendar.FRIDAY), d[3] };
    testCal.setMinimalDaysInFirstWeek(3);
    testCal.setFirstDayOfWeek(Calendar.SUNDAY);
    for (int i = 0; i < DISAM.length; i += 4) {
        int y = ((Integer) DISAM[i]).intValue();
        int woy = ((Integer) DISAM[i + 1]).intValue();
        int dow = ((Integer) DISAM[i + 2]).intValue();
        Date exp = (Date) DISAM[i + 3];
        testCal.clear();
        testCal.set(Calendar.YEAR, y);
        testCal.set(Calendar.WEEK_OF_YEAR, woy);
        testCal.set(Calendar.DAY_OF_WEEK, dow);
        log(y + "-W" + woy + "-DOW" + dow + " expect:" + exp + " got:" + testCal.getTime());
        if (!testCal.getTime().equals(exp)) {
            log("  FAIL");
            fail = true;
        }
    // logln();
    }
    // Now try adding and rolling
    Object ADD = new Object();
    Object ROLL = new Object();
    tempcal.set(1998, Calendar.DECEMBER, 25);
    d[0] = tempcal.getTime();
    tempcal.set(1999, Calendar.JANUARY, 1);
    d[1] = tempcal.getTime();
    tempcal.set(1997, Calendar.DECEMBER, 28);
    d[2] = tempcal.getTime();
    tempcal.set(1998, Calendar.JANUARY, 4);
    d[3] = tempcal.getTime();
    tempcal.set(1998, Calendar.DECEMBER, 27);
    d[4] = tempcal.getTime();
    tempcal.set(1997, Calendar.DECEMBER, 28);
    d[5] = tempcal.getTime();
    tempcal.set(1999, Calendar.JANUARY, 2);
    d[6] = tempcal.getTime();
    tempcal.set(1998, Calendar.JANUARY, 3);
    d[7] = tempcal.getTime();
    Object[] ADDROLL = { ADD, new Integer(1), d[0], d[1], ADD, new Integer(1), d[2], d[3], ROLL, new Integer(1), d[4], d[5], ROLL, new Integer(1), d[6], d[7] };
    testCal.setMinimalDaysInFirstWeek(3);
    testCal.setFirstDayOfWeek(Calendar.SUNDAY);
    for (int i = 0; i < ADDROLL.length; i += 4) {
        int amount = ((Integer) ADDROLL[i + 1]).intValue();
        Date before = (Date) ADDROLL[i + 2];
        Date after = (Date) ADDROLL[i + 3];
        testCal.setTime(before);
        if (ADDROLL[i] == ADD)
            testCal.add(Calendar.WEEK_OF_YEAR, amount);
        else
            testCal.roll(Calendar.WEEK_OF_YEAR, amount);
        log((ADDROLL[i] == ADD ? "add(WOY," : "roll(WOY,") + amount + ") " + before + " => " + testCal.getTime());
        if (!after.equals(testCal.getTime())) {
            logln("  exp:" + after + "  FAIL");
            fail = true;
        } else
            logln(" ok");
        testCal.setTime(after);
        if (ADDROLL[i] == ADD)
            testCal.add(Calendar.WEEK_OF_YEAR, -amount);
        else
            testCal.roll(Calendar.WEEK_OF_YEAR, -amount);
        log((ADDROLL[i] == ADD ? "add(WOY," : "roll(WOY,") + (-amount) + ") " + after + " => " + testCal.getTime());
        if (!before.equals(testCal.getTime())) {
            logln("  exp:" + before + "  FAIL");
            fail = true;
        } else
            logln(" ok");
    }
    if (fail)
        errln("Fail: Week of year misbehaving");
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) SimpleDateFormat(android.icu.text.SimpleDateFormat) 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