Search in sources :

Example 56 with GregorianCalendar

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

the class CalendarRegressionTest method Test4197699.

/**
 * Week of year is wrong at the start and end of the year.
 */
@Test
public void Test4197699() {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.setMinimalDaysInFirstWeek(4);
    DateFormat fmt = new SimpleDateFormat("E dd MMM yyyy  'DOY='D 'WOY='w");
    fmt.setCalendar(cal);
    int[] DATA = { 2000, Calendar.JANUARY, 1, 52, 2001, Calendar.DECEMBER, 31, 1 };
    for (int i = 0; i < DATA.length; ) {
        cal.set(DATA[i++], DATA[i++], DATA[i++]);
        int expWOY = DATA[i++];
        int actWOY = cal.get(Calendar.WEEK_OF_YEAR);
        if (expWOY == actWOY) {
            logln("Ok: " + fmt.format(cal.getTime()));
        } else {
            errln("FAIL: " + fmt.format(cal.getTime()) + ", expected WOY=" + expWOY);
            cal.add(Calendar.DATE, -8);
            for (int j = 0; j < 14; ++j) {
                cal.add(Calendar.DATE, 1);
                logln(fmt.format(cal.getTime()));
            }
        }
    }
}
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 57 with GregorianCalendar

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

the class CalendarRegressionTest method Test4166109.

/**
 * GregorianCalendar.getActualMaximum() does not account for first day of
 * week.
 */
@Test
public void Test4166109() {
    /*
         * Test month:
         * 
         * March 1998 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
         * 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
         */
    boolean passed = true;
    int field = Calendar.WEEK_OF_MONTH;
    GregorianCalendar calendar = new GregorianCalendar(Locale.US);
    calendar.set(1998, Calendar.MARCH, 1);
    calendar.setMinimalDaysInFirstWeek(1);
    logln("Date:  " + calendar.getTime());
    int firstInMonth = calendar.get(Calendar.DAY_OF_MONTH);
    for (int firstInWeek = Calendar.SUNDAY; firstInWeek <= Calendar.SATURDAY; firstInWeek++) {
        calendar.setFirstDayOfWeek(firstInWeek);
        int returned = calendar.getActualMaximum(field);
        int expected = (31 + ((firstInMonth - firstInWeek + 7) % 7) + 6) / 7;
        logln("First day of week = " + firstInWeek + "  getActualMaximum(WEEK_OF_MONTH) = " + returned + "  expected = " + expected + ((returned == expected) ? "  ok" : "  FAIL"));
        if (returned != expected) {
            passed = false;
        }
    }
    if (!passed) {
        errln("Test failed");
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 58 with GregorianCalendar

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

the class CalendarRegressionTest method Test4174361.

@Test
public void Test4174361() {
    GregorianCalendar calendar = new GregorianCalendar(1996, 1, 29);
    calendar.add(Calendar.MONTH, 10);
    // Date date1 = calendar.getTime();
    // date1 = null;
    int d1 = calendar.get(Calendar.DAY_OF_MONTH);
    calendar = new GregorianCalendar(1996, 1, 29);
    calendar.add(Calendar.MONTH, 11);
    // Date date2 = calendar.getTime();
    // date2 = null;
    int d2 = calendar.get(Calendar.DAY_OF_MONTH);
    if (d1 != d2) {
        errln("adding months to Feb 29 broken");
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 59 with GregorianCalendar

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

the class CalendarRegressionTest method Test4125892.

// I am disabling this test -- it is currently failing because of a bug
// in Sun's latest change to STZ.getOffset(). I have filed a Sun bug
// against this problem.
// Re-enabled after 'porting' TZ and STZ from java.util to android.icu.util.
/**
 * Prove that GregorianCalendar is proleptic (it used to cut off at 45 BC,
 * and not have leap years before then).
 */
@Test
public void Test4125892() {
    GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
    // DateFormat fmt = new SimpleDateFormat("MMMM d, yyyy G");
    // fmt = null;
    cal.clear();
    cal.set(Calendar.ERA, GregorianCalendar.BC);
    // 81 BC is a leap year (proleptically)
    cal.set(Calendar.YEAR, 81);
    cal.set(Calendar.MONTH, Calendar.FEBRUARY);
    cal.set(Calendar.DATE, 28);
    cal.add(Calendar.DATE, 1);
    if (cal.get(Calendar.DATE) != 29 || // -80 == 81 BC
    !cal.isLeapYear(-80))
        errln("Calendar not proleptic");
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 60 with GregorianCalendar

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

the class CalendarRegressionTest method Test9019.

/**
 * Test case for ticket:9019
 */
@Test
public void Test9019() {
    GregorianCalendar cal1 = new GregorianCalendar(TimeZone.GMT_ZONE, ULocale.US);
    GregorianCalendar cal2 = new GregorianCalendar(TimeZone.GMT_ZONE, ULocale.US);
    cal1.clear();
    cal2.clear();
    cal1.set(2011, Calendar.MAY, 06);
    cal2.set(2012, Calendar.JANUARY, 06);
    cal1.setLenient(false);
    cal1.add(Calendar.MONTH, 8);
    if (!cal1.getTime().equals(cal2.getTime())) {
        errln("Error: Calendar is " + cal1.getTime() + " but expected " + cal2.getTime());
    } else {
        logln("Pass: rolled calendar is " + cal1.getTime());
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) 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