Search in sources :

Example 41 with GregorianCalendar

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

the class CompatibilityTest method TestRoll.

@Test
public void TestRoll() {
    Calendar cal = new GregorianCalendar(1997, Calendar.JANUARY, 31);
    int[] dayValues = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 };
    for (int i = 0; i < dayValues.length; i++) {
        Calendar cal2 = (Calendar) cal.clone();
        cal2.roll(Calendar.MONTH, i);
        if (cal2.get(Calendar.DAY_OF_MONTH) != dayValues[i])
            errln("Rolling the month in 1/31/1997 up by " + i + " should have yielded " + ((i + 1) % 12) + "/" + dayValues[i] + "/1997, but actually yielded " + ((i + 1) % 12) + "/" + cal2.get(Calendar.DAY_OF_MONTH) + "/1997.");
    }
    cal.set(1996, Calendar.FEBRUARY, 29);
    // int[] monthValues = { 1, 2, 2, 2, 1, 2, 2, 2, 1, 2 };
    // int[] dayValues2 = { 29, 1, 1, 1, 29, 1, 1, 1, 29, 1 };
    // I've revised the expected values to make more sense -- rolling
    // the year should pin the DAY_OF_MONTH. - Liu 11/6/00
    int[] monthValues = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    int[] dayValues2 = { 29, 28, 28, 28, 29, 28, 28, 28, 29, 28 };
    for (int i = 0; i < dayValues2.length; i++) {
        Calendar cal2 = (Calendar) cal.clone();
        cal2.roll(Calendar.YEAR, i);
        if (cal2.get(Calendar.DAY_OF_MONTH) != dayValues2[i] || cal2.get(Calendar.MONTH) != monthValues[i])
            errln("Roll 2/29/1996 by " + i + " year: expected " + (monthValues[i] + 1) + "/" + dayValues2[i] + "/" + (1996 + i) + ", got " + (cal2.get(Calendar.MONTH) + 1) + "/" + cal2.get(Calendar.DAY_OF_MONTH) + "/" + cal2.get(Calendar.YEAR));
    }
    // Test rolling hour of day
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.roll(Calendar.HOUR_OF_DAY, -2);
    int f = cal.get(Calendar.HOUR_OF_DAY);
    if (f != 22)
        errln("Rolling HOUR_OF_DAY=0 delta=-2 gave " + f + " Wanted 22");
    cal.roll(Calendar.HOUR_OF_DAY, 5);
    f = cal.get(Calendar.HOUR_OF_DAY);
    if (f != 3)
        errln("Rolling HOUR_OF_DAY=22 delta=5 gave " + f + " Wanted 3");
    cal.roll(Calendar.HOUR_OF_DAY, 21);
    f = cal.get(Calendar.HOUR_OF_DAY);
    if (f != 0)
        errln("Rolling HOUR_OF_DAY=3 delta=21 gave " + f + " Wanted 0");
    // Test rolling hour
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.roll(Calendar.HOUR, -2);
    f = cal.get(Calendar.HOUR);
    if (f != 10)
        errln("Rolling HOUR=0 delta=-2 gave " + f + " Wanted 10");
    cal.roll(Calendar.HOUR, 5);
    f = cal.get(Calendar.HOUR);
    if (f != 3)
        errln("Rolling HOUR=10 delta=5 gave " + f + " Wanted 3");
    cal.roll(Calendar.HOUR, 9);
    f = cal.get(Calendar.HOUR);
    if (f != 0)
        errln("Rolling HOUR=3 delta=9 gave " + f + " Wanted 0");
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 42 with GregorianCalendar

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

the class CompatibilityTest method TestActualMinMax.

@Test
public void TestActualMinMax() {
    Calendar cal = new GregorianCalendar(1967, Calendar.MARCH, 10);
    cal.setFirstDayOfWeek(Calendar.SUNDAY);
    cal.setMinimalDaysInFirstWeek(3);
    if (cal.getActualMinimum(Calendar.DAY_OF_MONTH) != 1)
        errln("Actual minimum date for 3/10/1967 should have been 1; got " + cal.getActualMinimum(Calendar.DAY_OF_MONTH));
    if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) != 31)
        errln("Actual maximum date for 3/10/1967 should have been 31; got " + cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    cal.set(Calendar.MONTH, Calendar.FEBRUARY);
    if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) != 28)
        errln("Actual maximum date for 2/10/1967 should have been 28; got " + cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    if (cal.getActualMaximum(Calendar.DAY_OF_YEAR) != 365)
        errln("Number of days in 1967 should have been 365; got " + cal.getActualMaximum(Calendar.DAY_OF_YEAR));
    cal.set(Calendar.YEAR, 1968);
    if (cal.getActualMaximum(Calendar.DAY_OF_MONTH) != 29)
        errln("Actual maximum date for 2/10/1968 should have been 29; got " + cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    if (cal.getActualMaximum(Calendar.DAY_OF_YEAR) != 366)
        errln("Number of days in 1968 should have been 366; got " + cal.getActualMaximum(Calendar.DAY_OF_YEAR));
    // Using week settings of SUNDAY/3 (see above)
    if (cal.getActualMaximum(Calendar.WEEK_OF_YEAR) != 52)
        errln("Number of weeks in 1968 should have been 52; got " + cal.getActualMaximum(Calendar.WEEK_OF_YEAR));
    cal.set(Calendar.YEAR, 1976);
    // Added - Liu 11/6/00
    cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
    // Using week settings of SUNDAY/3 (see above)
    if (cal.getActualMaximum(Calendar.WEEK_OF_YEAR) != 53)
        errln("Number of weeks in 1976 should have been 53; got " + cal.getActualMaximum(Calendar.WEEK_OF_YEAR));
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 43 with GregorianCalendar

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

the class CompatibilityTest method dowTest.

void dowTest(boolean lenient) {
    GregorianCalendar cal = new GregorianCalendar();
    // Wednesday
    cal.set(1997, Calendar.AUGUST, 12);
    // Force update
    cal.getTime();
    cal.setLenient(lenient);
    // Set the date to be December 1, 1996
    cal.set(1996, Calendar.DECEMBER, 1);
    int dow = cal.get(Calendar.DAY_OF_WEEK);
    int min = cal.getMinimum(Calendar.DAY_OF_WEEK);
    int max = cal.getMaximum(Calendar.DAY_OF_WEEK);
    if (dow < min || dow > max)
        errln("FAIL: Day of week " + dow + " out of range");
    if (dow != Calendar.SUNDAY) {
        errln("FAIL2: Day of week should be SUNDAY; is " + dow + ": " + cal.getTime());
    }
    if (min != Calendar.SUNDAY || max != Calendar.SATURDAY)
        errln("FAIL: Min/max bad");
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar)

Example 44 with GregorianCalendar

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

the class CompatibilityTest method TestFields060.

// Test that certain fields on a certain date are as expected.
@Test
public void TestFields060() {
    int year = 1997;
    // october
    int month = java.util.Calendar.OCTOBER;
    // DAYOFWEEK should return 3 for Wednesday
    int dDate = 22;
    GregorianCalendar calendar = null;
    calendar = new GregorianCalendar(year, month, dDate);
    for (int i = 0; i < EXPECTED_FIELDS.length; ) {
        int field = EXPECTED_FIELDS[i++];
        int expected = EXPECTED_FIELDS[i++];
        if (calendar.get(field) != expected) {
            errln("Expected field " + field + " to have value " + expected + "; received " + calendar.get(field) + " instead");
        }
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 45 with GregorianCalendar

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

the class DangiTest method TestZeroDOM.

/**
 * Make sure no Gregorian dates map to Chinese 1-based day of
 * month zero.  This was a problem with some of the astronomical
 * new moon determinations.
 */
@Test
public void TestZeroDOM() {
    Calendar cal = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi"));
    GregorianCalendar greg = new GregorianCalendar(1989, Calendar.SEPTEMBER, 1);
    logln("Start: " + greg.getTime());
    for (int i = 0; i < 1000; ++i) {
        cal.setTimeInMillis(greg.getTimeInMillis());
        if (cal.get(Calendar.DAY_OF_MONTH) == 0) {
            errln("Fail: " + greg.getTime() + " -> " + cal.get(Calendar.YEAR) + "/" + cal.get(Calendar.MONTH) + (cal.get(Calendar.IS_LEAP_MONTH) == 1 ? "(leap)" : "") + "/" + cal.get(Calendar.DAY_OF_MONTH));
        }
        greg.add(Calendar.DAY_OF_YEAR, 1);
    }
    logln("End: " + greg.getTime());
}
Also used : ULocale(android.icu.util.ULocale) DangiCalendar(android.icu.util.DangiCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) 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