Search in sources :

Example 66 with GregorianCalendar

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

the class CalendarRegressionTest method TestJ9.

/**
 * Set behavior of DST_OFFSET field. ICU4J Jitterbug 9.
 */
@Test
public void TestJ9() {
    int HOURS = 60 * 60 * 1000;
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("PST"), Locale.US);
    final int END_FIELDS = 0x1234;
    int[] DATA = { // With no explicit ZONE/DST expect 12:00 am
    Calendar.MONTH, Calendar.JUNE, END_FIELDS, // expected hour, min
    0, // expected hour, min
    0, // Normal ZONE/DST for June 1 Pacific is 8:00/1:00
    Calendar.MONTH, Calendar.JUNE, Calendar.ZONE_OFFSET, -8 * HOURS, Calendar.DST_OFFSET, HOURS, END_FIELDS, // expected hour, min
    0, // expected hour, min
    0, // With ZONE/DST of 8:00/0:30 expect time of 12:30 am
    Calendar.MONTH, Calendar.JUNE, Calendar.ZONE_OFFSET, -8 * HOURS, Calendar.DST_OFFSET, HOURS / 2, END_FIELDS, // expected hour, min
    0, // expected hour, min
    30, // With ZONE/DST of 8:00/UNSET expect time of 1:00 am
    Calendar.MONTH, Calendar.JUNE, Calendar.ZONE_OFFSET, -8 * HOURS, END_FIELDS, // expected hour, min
    1, // expected hour, min
    0, // With ZONE/DST of UNSET/0:30 expect 4:30 pm (day before)
    Calendar.MONTH, Calendar.JUNE, Calendar.DST_OFFSET, HOURS / 2, END_FIELDS, // expected hour, min
    16, // expected hour, min
    30 };
    for (int i = 0; i < DATA.length; ) {
        int start = i;
        cal.clear();
        // Set fields
        while (DATA[i] != END_FIELDS) {
            cal.set(DATA[i++], DATA[i++]);
        }
        // skip over END_FIELDS
        ++i;
        // Get hour/minute
        int h = cal.get(Calendar.HOUR_OF_DAY);
        int m = cal.get(Calendar.MINUTE);
        // Check
        if (h != DATA[i] || m != DATA[i + 1]) {
            errln("Fail: expected " + DATA[i] + ":" + DATA[i + 1] + ", got " + h + ":" + m + " after:");
            while (DATA[start] != END_FIELDS) {
                logln("set(" + FIELD_NAME[DATA[start++]] + ", " + DATA[start++] + ");");
            }
        }
        // skip over expected hour, min
        i += 2;
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 67 with GregorianCalendar

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

the class CalendarRegressionTest method Test4040996.

@Test
public void Test4040996() {
    try {
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
        Calendar calendar = new GregorianCalendar(pdt);
        calendar.set(Calendar.MONTH, 3);
        calendar.set(Calendar.DAY_OF_MONTH, 18);
        calendar.set(Calendar.SECOND, 30);
        logln("MONTH: " + calendar.get(Calendar.MONTH));
        logln("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
        logln("MINUTE: " + calendar.get(Calendar.MINUTE));
        logln("SECOND: " + calendar.get(Calendar.SECOND));
        calendar.add(Calendar.SECOND, 6);
        // This will print out todays date for MONTH and DAY_OF_MONTH
        // instead of the date it was set to.
        // This happens when adding MILLISECOND or MINUTE also
        logln("MONTH: " + calendar.get(Calendar.MONTH));
        logln("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
        logln("MINUTE: " + calendar.get(Calendar.MINUTE));
        logln("SECOND: " + calendar.get(Calendar.SECOND));
        if (calendar.get(Calendar.MONTH) != 3 || calendar.get(Calendar.DAY_OF_MONTH) != 18 || calendar.get(Calendar.SECOND) != 36)
            errln("Fail: Calendar.add misbehaves");
    } catch (Exception e) {
        warnln("Could not load data. " + e.getMessage());
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) Test(org.junit.Test)

Example 68 with GregorianCalendar

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

the class CalendarRegressionTest method TestT8596.

/*
     * Test case for ticket#8596.
     * Setting an year followed by getActualMaximum(Calendar.WEEK_OF_YEAR)
     * may result wrong maximum week.
     */
@Test
public void TestT8596() {
    GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("Etc/GMT"));
    gc.setFirstDayOfWeek(Calendar.MONDAY);
    gc.setMinimalDaysInFirstWeek(4);
    // Force the calender to resolve the fields once.
    // The maximum week number in 2011 is 52.
    gc.set(Calendar.YEAR, 2011);
    gc.get(Calendar.YEAR);
    // Set a date in year 2009, but not calling get to resolve
    // the calendar's internal field yet.
    gc.set(2009, Calendar.JULY, 1);
    // Then call getActuamMaximum for week of year.
    // #8596 was caused by conflict between year set
    // above and internal work calendar field resolution.
    int maxWeeks = gc.getActualMaximum(Calendar.WEEK_OF_YEAR);
    if (maxWeeks != 53) {
        errln("FAIL: Max week in 2009 in ISO calendar is 53, but got " + maxWeeks);
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 69 with GregorianCalendar

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

the class CalendarRegressionTest method Test4035301.

@Test
public void Test4035301() {
    try {
        GregorianCalendar c = new GregorianCalendar(98, 8, 7);
        GregorianCalendar d = new GregorianCalendar(98, 8, 7);
        if (c.after(d) || c.after(c) || c.before(d) || c.before(c) || !c.equals(c) || !c.equals(d))
            errln("Fail");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        warnln("Could not load data. " + e.getMessage());
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) MissingResourceException(java.util.MissingResourceException) IOException(java.io.IOException) Test(org.junit.Test)

Example 70 with GregorianCalendar

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

the class CalendarRegressionTest method Test4145158.

/**
 * GregorianCalendar handling of Dates Long.MIN_VALUE and Long.MAX_VALUE is
 * confusing; unless the time zone has a raw offset of zero, one or the
 * other of these will wrap. We've modified the test given in the bug report
 * to therefore only check the behavior of a calendar with a zero raw offset
 * zone.
 */
@Test
public void Test4145158() {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
    calendar.setTime(new Date(Long.MIN_VALUE));
    int year1 = calendar.get(Calendar.YEAR);
    int era1 = calendar.get(Calendar.ERA);
    calendar.setTime(new Date(Long.MAX_VALUE));
    int year2 = calendar.get(Calendar.YEAR);
    int era2 = calendar.get(Calendar.ERA);
    if (year1 == year2 && era1 == era2) {
        errln("Fail: Long.MIN_VALUE or Long.MAX_VALUE wrapping around");
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Aggregations

GregorianCalendar (android.icu.util.GregorianCalendar)114 Test (org.junit.Test)102 Date (java.util.Date)64 Calendar (android.icu.util.Calendar)41 SimpleDateFormat (android.icu.text.SimpleDateFormat)34 TimeZone (android.icu.util.TimeZone)29 SimpleTimeZone (android.icu.util.SimpleTimeZone)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