Search in sources :

Example 46 with Calendar

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

the class CalendarRegressionTest method Test4071385.

@Test
public void Test4071385() {
    // work around bug for jdk1.4 on solaris 2.6, which uses funky timezone
    // names
    // jdk1.4.1 will drop support for 2.6 so we should be ok when it comes out
    java.util.TimeZone javazone = java.util.TimeZone.getTimeZone("GMT");
    TimeZone icuzone = TimeZone.getTimeZone("GMT");
    Calendar cal = Calendar.getInstance(icuzone);
    java.util.Calendar tempcal = java.util.Calendar.getInstance(javazone);
    tempcal.clear();
    tempcal.set(1998, Calendar.JUNE, 24);
    cal.setTime(tempcal.getTime());
    // change a field
    cal.set(Calendar.MONTH, Calendar.NOVEMBER);
    logln(cal.getTime().toString());
    tempcal.set(1998, Calendar.NOVEMBER, 24);
    if (!cal.getTime().equals(tempcal.getTime()))
        errln("Fail");
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Test(org.junit.Test)

Example 47 with Calendar

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

the class CalendarRegressionTest method TestT9403.

/**
 * Test case for ticket 9403
 * semantic API change when attempting to call setTimeInMillis(long) with a value outside the bounds.
 * In strict mode an IllegalIcuArgumentException will be thrown
 * In lenient mode the value will be pinned to the relative min/max
 */
@Test
public void TestT9403() {
    Calendar myCal = Calendar.getInstance();
    long dateBit1, dateBit2, testMillis = 0L;
    boolean missedException = true;
    testMillis = -184303902611600000L;
    logln("Testing invalid setMillis value in lienent mode - using value: " + testMillis);
    try {
        myCal.setTimeInMillis(testMillis);
    } catch (IllegalArgumentException e) {
        logln("Fail: detected as bad millis");
        missedException = false;
    }
    assertTrue("Fail: out of bound millis did not trigger exception!", missedException);
    dateBit1 = myCal.get(Calendar.MILLISECOND);
    assertNotEquals("Fail: millis not changed to MIN_MILLIS", testMillis, dateBit1);
    logln("Testing invalid setMillis value in strict mode - using value: " + testMillis);
    myCal.setLenient(false);
    try {
        myCal.setTimeInMillis(testMillis);
    } catch (IllegalArgumentException e) {
        logln("Pass: correctly detected bad millis");
        missedException = false;
    }
    dateBit1 = myCal.get(Calendar.DAY_OF_MONTH);
    dateBit2 = myCal.getTimeInMillis();
    assertFalse("Fail: error in setMillis, allowed invalid value : " + testMillis + "...returned dayOfMonth : " + dateBit1 + " millis : " + dateBit2, missedException);
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Test(org.junit.Test)

Example 48 with Calendar

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

the class CalendarRegressionTest method Test4209071.

/**
 * Calendar DAY_OF_WEEK_IN_MONTH fields->time broken. The problem is in the
 * field disambiguation code in GregorianCalendar. This code is supposed to
 * choose the most recent set of fields among the following:
 *
 * MONTH + DAY_OF_MONTH MONTH + WEEK_OF_MONTH + DAY_OF_WEEK MONTH +
 * DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK DAY_OF_YEAR WEEK_OF_YEAR + DAY_OF_WEEK
 */
@Test
public void Test4209071() {
    Calendar cal = Calendar.getInstance(Locale.US);
    // General field setting test
    int Y = 1995;
    Date[] d = new Date[13];
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(Y, Calendar.JANUARY, 1);
    d[0] = tempcal.getTime();
    tempcal.set(Y, Calendar.MARCH, 1);
    d[1] = tempcal.getTime();
    tempcal.set(Y, Calendar.JANUARY, 4);
    d[2] = tempcal.getTime();
    tempcal.set(Y, Calendar.JANUARY, 18);
    d[3] = tempcal.getTime();
    tempcal.set(Y, Calendar.JANUARY, 18);
    d[4] = tempcal.getTime();
    tempcal.set(Y - 1, Calendar.DECEMBER, 22);
    d[5] = tempcal.getTime();
    tempcal.set(Y, Calendar.JANUARY, 26);
    d[6] = tempcal.getTime();
    tempcal.set(Y, Calendar.JANUARY, 26);
    d[7] = tempcal.getTime();
    tempcal.set(Y, Calendar.MARCH, 1);
    d[8] = tempcal.getTime();
    tempcal.set(Y, Calendar.OCTOBER, 6);
    d[9] = tempcal.getTime();
    tempcal.set(Y, Calendar.OCTOBER, 13);
    d[10] = tempcal.getTime();
    tempcal.set(Y, Calendar.AUGUST, 10);
    d[11] = tempcal.getTime();
    tempcal.set(Y, Calendar.DECEMBER, 7);
    d[12] = tempcal.getTime();
    Object[] FIELD_DATA = { // 0
    new int[] {}, d[0], // 1
    new int[] { Calendar.MONTH, Calendar.MARCH }, d[1], // 2
    new int[] { Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY }, d[2], // 3
    new int[] { Calendar.DAY_OF_WEEK, Calendar.THURSDAY, Calendar.DAY_OF_MONTH, 18 }, d[3], // 4
    new int[] { Calendar.DAY_OF_MONTH, 18, Calendar.DAY_OF_WEEK, Calendar.THURSDAY }, d[4], // 5 (WOM -1 is in previous month)
    new int[] { Calendar.DAY_OF_MONTH, 18, Calendar.WEEK_OF_MONTH, -1, Calendar.DAY_OF_WEEK, Calendar.THURSDAY }, d[5], // 6
    new int[] { Calendar.DAY_OF_MONTH, 18, Calendar.WEEK_OF_MONTH, 4, Calendar.DAY_OF_WEEK, Calendar.THURSDAY }, d[6], // 7 (DIM -1 is in same month)
    new int[] { Calendar.DAY_OF_MONTH, 18, Calendar.DAY_OF_WEEK_IN_MONTH, -1, Calendar.DAY_OF_WEEK, Calendar.THURSDAY }, d[7], // 8
    new int[] { Calendar.WEEK_OF_YEAR, 9, Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY }, d[8], // 9
    new int[] { Calendar.MONTH, Calendar.OCTOBER, Calendar.DAY_OF_WEEK_IN_MONTH, 1, Calendar.DAY_OF_WEEK, Calendar.FRIDAY }, d[9], // 10
    new int[] { Calendar.MONTH, Calendar.OCTOBER, Calendar.WEEK_OF_MONTH, 2, Calendar.DAY_OF_WEEK, Calendar.FRIDAY }, d[10], // 11
    new int[] { Calendar.MONTH, Calendar.OCTOBER, Calendar.DAY_OF_MONTH, 15, Calendar.DAY_OF_YEAR, 222 }, d[11], // 12
    new int[] { Calendar.DAY_OF_WEEK, Calendar.THURSDAY, Calendar.MONTH, Calendar.DECEMBER }, d[12] };
    for (int i = 0; i < FIELD_DATA.length; i += 2) {
        int[] fields = (int[]) FIELD_DATA[i];
        Date exp = (Date) FIELD_DATA[i + 1];
        cal.clear();
        cal.set(Calendar.YEAR, Y);
        for (int j = 0; j < fields.length; j += 2) {
            cal.set(fields[j], fields[j + 1]);
        }
        Date act = cal.getTime();
        if (!act.equals(exp)) {
            errln("FAIL: Test " + (i / 2) + " got " + act + ", want " + exp + " (see test/java/util/Calendar/CalendarRegressionTest.java");
        }
    }
    tempcal.set(1997, Calendar.JANUARY, 5);
    d[0] = tempcal.getTime();
    tempcal.set(1997, Calendar.JANUARY, 26);
    d[1] = tempcal.getTime();
    tempcal.set(1997, Calendar.FEBRUARY, 23);
    d[2] = tempcal.getTime();
    tempcal.set(1997, Calendar.JANUARY, 26);
    d[3] = tempcal.getTime();
    tempcal.set(1997, Calendar.JANUARY, 5);
    d[4] = tempcal.getTime();
    tempcal.set(1996, Calendar.DECEMBER, 8);
    d[5] = tempcal.getTime();
    // Test specific failure reported in bug
    Object[] DATA = { new Integer(1), d[0], new Integer(4), d[1], new Integer(8), d[2], new Integer(-1), d[3], new Integer(-4), d[4], new Integer(-8), d[5] };
    for (int i = 0; i < DATA.length; i += 2) {
        cal.clear();
        cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, ((Number) DATA[i]).intValue());
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.YEAR, 1997);
        Date actual = cal.getTime();
        if (!actual.equals(DATA[i + 1])) {
            errln("FAIL: Sunday " + DATA[i] + " of Jan 1997 -> " + actual + ", want " + DATA[i + 1]);
        }
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Date(java.util.Date) Test(org.junit.Test)

Example 49 with Calendar

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

the class CalendarRegressionTest method TestYearJump3279.

@Test
public void TestYearJump3279() {
    final long time = 1041148800000L;
    Calendar c = new GregorianCalendar();
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);
    c.setTimeInMillis(time);
    int year1 = c.get(Calendar.YEAR);
    logln("time: " + fmt.format(new Date(c.getTimeInMillis())));
    logln("setting DOW to " + c.getFirstDayOfWeek());
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    logln("week: " + c.getTime());
    logln("week adjust: " + fmt.format(new Date(c.getTimeInMillis())));
    int year2 = c.get(Calendar.YEAR);
    if (year1 != year2) {
        errln("Error: adjusted day of week, and year jumped from " + year1 + " to " + year2);
    } else {
        logln("Year remained " + year2 + " - PASS.");
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 50 with Calendar

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

the class CalendarRegressionTest method Test4061476.

@Test
public void Test4061476() {
    SimpleDateFormat fmt = new SimpleDateFormat("ddMMMyy", Locale.UK);
    Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.UK);
    fmt.setCalendar(cal);
    try {
        Date date = fmt.parse("29MAY97");
        cal.setTime(date);
    } catch (Exception e) {
        System.out.print("");
    }
    cal.set(Calendar.HOUR_OF_DAY, 13);
    logln("Hour: " + cal.get(Calendar.HOUR_OF_DAY));
    cal.add(Calendar.HOUR_OF_DAY, 6);
    logln("Hour: " + cal.get(Calendar.HOUR_OF_DAY));
    if (cal.get(Calendar.HOUR_OF_DAY) != 19)
        errln("Fail: Want 19 Got " + cal.get(Calendar.HOUR_OF_DAY));
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Calendar (android.icu.util.Calendar)233 Test (org.junit.Test)155 GregorianCalendar (android.icu.util.GregorianCalendar)135 Date (java.util.Date)96 JapaneseCalendar (android.icu.util.JapaneseCalendar)72 SimpleDateFormat (android.icu.text.SimpleDateFormat)69 ULocale (android.icu.util.ULocale)60 BuddhistCalendar (android.icu.util.BuddhistCalendar)53 ChineseCalendar (android.icu.util.ChineseCalendar)52 IslamicCalendar (android.icu.util.IslamicCalendar)48 TimeZone (android.icu.util.TimeZone)41 DateFormat (android.icu.text.DateFormat)37 HebrewCalendar (android.icu.util.HebrewCalendar)32 FieldPosition (java.text.FieldPosition)29 SimpleTimeZone (android.icu.util.SimpleTimeZone)26 ParsePosition (java.text.ParsePosition)23 TaiwanCalendar (android.icu.util.TaiwanCalendar)22 ParseException (java.text.ParseException)21 ChineseDateFormat (android.icu.text.ChineseDateFormat)17 IOException (java.io.IOException)16