Search in sources :

Example 66 with Calendar

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

the class CompatibilityTest method TestDisambiguation765.

// Test the correct behavior of the disambiguation algorithm.
@Test
public void TestDisambiguation765() throws Exception {
    Calendar c = Calendar.getInstance();
    c.setLenient(false);
    c.clear();
    c.set(Calendar.YEAR, 1997);
    c.set(Calendar.MONTH, Calendar.JUNE);
    c.set(Calendar.DATE, 3);
    verify765("1997 third day of June = ", c, 1997, Calendar.JUNE, 3);
    c.clear();
    c.set(Calendar.YEAR, 1997);
    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    c.set(Calendar.MONTH, Calendar.JUNE);
    c.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1);
    verify765("1997 first Tuesday in June = ", c, 1997, Calendar.JUNE, 3);
    c.clear();
    c.set(Calendar.YEAR, 1997);
    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    c.set(Calendar.MONTH, Calendar.JUNE);
    c.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1);
    verify765("1997 last Tuesday in June = ", c, 1997, Calendar.JUNE, 24);
    IllegalArgumentException e = null;
    try {
        c.clear();
        c.set(Calendar.YEAR, 1997);
        c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
        c.set(Calendar.MONTH, Calendar.JUNE);
        c.set(Calendar.DAY_OF_WEEK_IN_MONTH, 0);
        c.getTime();
    } catch (IllegalArgumentException ex) {
        e = ex;
    }
    verify765("1997 zero-th Tuesday in June = ", e, c);
    c.clear();
    c.set(Calendar.YEAR, 1997);
    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    c.set(Calendar.MONTH, Calendar.JUNE);
    c.set(Calendar.WEEK_OF_MONTH, 1);
    verify765("1997 Tuesday in week 1 of June = ", c, 1997, Calendar.JUNE, 3);
    c.clear();
    c.set(Calendar.YEAR, 1997);
    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    c.set(Calendar.MONTH, Calendar.JUNE);
    c.set(Calendar.WEEK_OF_MONTH, 5);
    verify765("1997 Tuesday in week 5 of June = ", c, 1997, Calendar.JULY, 1);
    try {
        c.clear();
        c.set(Calendar.YEAR, 1997);
        c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
        c.set(Calendar.MONTH, Calendar.JUNE);
        c.set(Calendar.WEEK_OF_MONTH, 0);
        c.getTime();
    } catch (IllegalArgumentException ex) {
        e = ex;
    }
    verify765("1997 Tuesday in week 0 of June = ", e, c);
    c.clear();
    c.set(Calendar.YEAR, 1997);
    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    c.set(Calendar.WEEK_OF_YEAR, 1);
    verify765("1997 Tuesday in week 1 of year = ", c, 1996, Calendar.DECEMBER, 31);
    c.clear();
    c.set(Calendar.YEAR, 1997);
    c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    c.set(Calendar.WEEK_OF_YEAR, 10);
    verify765("1997 Tuesday in week 10 of year = ", c, 1997, Calendar.MARCH, 4);
    e = null;
    try {
        c.clear();
        c.set(Calendar.YEAR, 1997);
        c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
        c.set(Calendar.WEEK_OF_YEAR, 0);
        c.getTime();
    } catch (IllegalArgumentException ex) {
        e = ex;
    }
    verify765("1997 Tuesday in week 0 of year = ", e, c);
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Test(org.junit.Test)

Example 67 with Calendar

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

the class CompatibilityTest method TestEpochStartFields.

// Verify that the fields are as expected (mostly zero) at the epoch start.
// Note that we adjust for the default timezone to get most things to zero.
@Test
public void TestEpochStartFields() {
    TimeZone z = TimeZone.getDefault();
    Calendar c = Calendar.getInstance();
    Date d = new Date(-z.getRawOffset());
    if (z.inDaylightTime(d)) {
        logln("Warning: Skipping test because " + d + " is in DST.");
    } else {
        c.setTime(d);
        c.setMinimalDaysInFirstWeek(1);
        for (int i = 0; i < Calendar.ZONE_OFFSET; ++i) {
            if (c.get(i) != EPOCH_FIELDS[i])
                errln("Expected field " + i + " to have value " + EPOCH_FIELDS[i] + "; saw " + c.get(i) + " instead");
        }
        if (c.get(Calendar.ZONE_OFFSET) != z.getRawOffset())
            errln("Expected field ZONE_OFFSET to have value " + z.getRawOffset() + "; saw " + c.get(Calendar.ZONE_OFFSET) + " instead");
        if (c.get(Calendar.DST_OFFSET) != 0)
            errln("Expected field DST_OFFSET to have value 0" + "; saw " + c.get(Calendar.DST_OFFSET) + " instead");
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Date(java.util.Date) Test(org.junit.Test)

Example 68 with Calendar

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

the class CompatibilityTest method aux4064654.

void aux4064654(int yr, int mo, int dt, int hr, int mn, int sc) {
    Date date;
    Calendar gmtcal = Calendar.getInstance();
    gmtcal.setTimeZone(TimeZone.getTimeZone("Africa/Casablanca"));
    gmtcal.set(yr, mo - 1, dt, hr, mn, sc);
    gmtcal.set(Calendar.MILLISECOND, 0);
    date = gmtcal.getTime();
    logln("date = " + date);
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("America/Los_Angels"));
    cal.setTime(date);
    int offset = cal.getTimeZone().getOffset(cal.get(Calendar.ERA), cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), cal.get(Calendar.DAY_OF_WEEK), cal.get(Calendar.MILLISECOND));
    logln("offset for " + date + "= " + (offset / 1000 / 60 / 60.0) + "hr");
    int utc = ((cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE)) * 60 + cal.get(Calendar.SECOND)) * 1000 + cal.get(Calendar.MILLISECOND) - offset;
    int expected = ((hr * 60 + mn) * 60 + sc) * 1000;
    if (utc != expected)
        errln("FAIL: Discrepancy of " + (utc - expected) + " millis = " + ((utc - expected) / 1000 / 60 / 60.0) + " hr");
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Date(java.util.Date)

Example 69 with Calendar

use of android.icu.util.Calendar 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)

Example 70 with Calendar

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

the class DangiTest method TestAdd.

/**
 * Test the behavior of KoreanLunarCalendar.add().  The only real
 * nastiness with roll is the MONTH field around leap months.
 */
@Test
public void TestAdd() {
    int[][] tests = new int[][] { // normal
    { 4338, 3, 0, 15, MONTH, 3, 4338, 6, 0, 15 }, // across year
    { 4335, 12, 0, 15, MONTH, 1, 4336, 1, 0, 15 }, // across year
    { 4336, 1, 0, 15, MONTH, -1, 4335, 12, 0, 15 }, // 4=leap
    { 4334, 3, 0, 15, MONTH, 3, 4334, 5, 0, 15 }, // 4=leap
    { 4334, 3, 0, 15, MONTH, 2, 4334, 4, 1, 15 }, // 4=leap
    { 4334, 4, 0, 15, MONTH, 1, 4334, 4, 1, 15 }, // 4=leap
    { 4334, 4, 1, 15, MONTH, 1, 4334, 5, 0, 15 }, // dom should pin
    { 4334, 3, 0, 30, MONTH, 2, 4334, 4, 1, 29 }, // no dom pin
    { 4334, 3, 0, 30, MONTH, 3, 4334, 5, 0, 30 }, // dom should pin
    { 4334, 3, 0, 30, MONTH, 4, 4334, 6, 0, 29 } };
    Calendar cal = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi"));
    doRollAddDangi(ADD, cal, tests);
}
Also used : ULocale(android.icu.util.ULocale) DangiCalendar(android.icu.util.DangiCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) 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