Search in sources :

Example 61 with Calendar

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

the class JapaneseTest method Test3860.

@Test
public void Test3860() {
    ULocale loc = new ULocale("ja_JP@calendar=japanese");
    Calendar cal = new JapaneseCalendar(loc);
    DateFormat enjformat = cal.getDateTimeFormat(0, 0, new ULocale("en_JP@calendar=japanese"));
    DateFormat format = cal.getDateTimeFormat(0, 0, loc);
    // Note: just 'y' doesn't work here.
    ((SimpleDateFormat) format).applyPattern("y.M.d");
    ParsePosition pos = new ParsePosition(0);
    // after the start of heisei accession.  Jan 1, 1H wouldn't work  because it is actually showa 64
    Date aDate = format.parse("1.1.9", pos);
    String inEn = enjformat.format(aDate);
    cal.clear();
    cal.setTime(aDate);
    int gotYear = cal.get(Calendar.YEAR);
    int gotEra = cal.get(Calendar.ERA);
    int expectYear = 1;
    int expectEra = JapaneseCalendar.CURRENT_ERA;
    if ((gotYear != expectYear) || (gotEra != expectEra)) {
        errln("Expected year " + expectYear + ", era " + expectEra + ", but got year " + gotYear + " and era " + gotEra + ", == " + inEn);
    } else {
        logln("Got year " + gotYear + " and era " + gotEra + ", == " + inEn);
    }
    // Test parse with missing era (should default to current era, heisei)
    // Test parse with incomplete information
    logln("Testing parse w/ just year...");
    Calendar cal2 = new JapaneseCalendar(loc);
    SimpleDateFormat fmt = new SimpleDateFormat("y", loc);
    SimpleDateFormat fmt2 = new SimpleDateFormat("HH:mm:ss.S MMMM d, yyyy G", new ULocale("en_US@calendar=gregorian"));
    cal2.clear();
    String samplestr = "1";
    logln("Test Year: " + samplestr);
    try {
        aDate = fmt.parse(samplestr);
    } catch (ParseException pe) {
        errln("Error parsing " + samplestr);
    }
    ParsePosition pp = new ParsePosition(0);
    fmt.parse(samplestr, cal2, pp);
    logln("cal2 after 1 parse:");
    String str = fmt2.format(aDate);
    logln("as Gregorian Calendar: " + str);
    cal2.setTime(aDate);
    gotYear = cal2.get(Calendar.YEAR);
    gotEra = cal2.get(Calendar.ERA);
    expectYear = 1;
    expectEra = JapaneseCalendar.CURRENT_ERA;
    if ((gotYear != 1) || (gotEra != expectEra)) {
        errln("parse " + samplestr + " of 'y' as Japanese Calendar, expected year " + expectYear + " and era " + expectEra + ", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str + ")");
    } else {
        logln(" year: " + gotYear + ", era: " + gotEra);
    }
}
Also used : ULocale(android.icu.util.ULocale) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 62 with Calendar

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

the class JapaneseTest method TestLimits.

/**
 * Test limits of the Japanese calendar
 */
@Test
public void TestLimits() {
    Calendar cal = Calendar.getInstance();
    cal.set(1988, Calendar.DECEMBER, 1);
    JapaneseCalendar jcal = new JapaneseCalendar();
    doLimitsTest(jcal, null, cal.getTime());
    doTheoreticalLimitsTest(jcal, true);
}
Also used : Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) Test(org.junit.Test)

Example 63 with Calendar

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

the class CompatibilityTest method marchByDelta.

// Supply a delta which is not a multiple of 7.
void marchByDelta(Calendar cal, int delta) {
    Calendar cur = (Calendar) cal.clone();
    int initialDOW = cur.get(Calendar.DAY_OF_WEEK);
    int DOW, newDOW = initialDOW;
    do {
        DOW = newDOW;
        logln("DOW = " + DOW + "  " + cur.getTime());
        cur.add(Calendar.DAY_OF_WEEK, delta);
        newDOW = cur.get(Calendar.DAY_OF_WEEK);
        int expectedDOW = 1 + (DOW + delta - 1) % 7;
        if (newDOW != expectedDOW) {
            errln("Day of week should be " + expectedDOW + " instead of " + newDOW + " on " + cur.getTime());
            return;
        }
    } while (newDOW != initialDOW);
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar)

Example 64 with Calendar

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

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

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