Search in sources :

Example 16 with ChineseCalendar

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

the class ChineseTest 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() {
    ChineseCalendar cal = new ChineseCalendar();
    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.EXTENDED_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 : ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 17 with ChineseCalendar

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

the class ChineseTest method TestRoll.

/**
 * Test the behavior of ChineseCalendar.roll().  The only real
 * nastiness with roll is the MONTH field around leap months.
 */
@Test
public void TestRoll() {
    int[][] tests = new int[][] { // normal
    { 4642, 3, 0, 15, MONTH, 3, 4642, 6, 0, 15 }, // normal
    { 4642, 3, 0, 15, MONTH, 11, 4642, 2, 0, 15 }, // across year
    { 4639, 12, 0, 15, MONTH, 1, 4639, 1, 0, 15 }, // across year
    { 4640, 1, 0, 15, MONTH, -1, 4640, 12, 0, 15 }, // 4=leap
    { 4638, 3, 0, 15, MONTH, 3, 4638, 5, 0, 15 }, // 4=leap
    { 4638, 3, 0, 15, MONTH, 16, 4638, 5, 0, 15 }, // 4=leap
    { 4638, 3, 0, 15, MONTH, 2, 4638, 4, 1, 15 }, // 4=leap
    { 4638, 3, 0, 15, MONTH, 28, 4638, 4, 1, 15 }, // 4=leap
    { 4638, 4, 0, 15, MONTH, 1, 4638, 4, 1, 15 }, // 4=leap
    { 4638, 4, 0, 15, MONTH, -12, 4638, 4, 1, 15 }, // 4=leap
    { 4638, 4, 1, 15, MONTH, 1, 4638, 5, 0, 15 }, // 4=leap
    { 4638, 4, 1, 15, MONTH, -25, 4638, 5, 0, 15 }, // dom should pin
    { 4638, 4, 0, 30, MONTH, 1, 4638, 4, 1, 29 }, // dom should pin
    { 4638, 4, 0, 30, MONTH, 14, 4638, 4, 1, 29 }, // no dom pin
    { 4638, 4, 0, 30, MONTH, 15, 4638, 5, 0, 30 }, // dom should pin
    { 4638, 4, 0, 30, MONTH, -10, 4638, 6, 0, 29 } };
    ChineseCalendar cal = new ChineseCalendar();
    doRollAdd(ROLL, cal, tests);
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) Test(org.junit.Test)

Example 18 with ChineseCalendar

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

the class ChineseTest method TestFormat.

/**
 * Test formatting.
 * Q: Why is this in Calendar tests instead of Format tests?
 * Note: This test assumes that Chinese calendar formatted dates can be parsed
 * unambiguously to recover the original Date that was formatted. This is not
 * currently true since Chinese calendar formatted dates do not include an era.
 * To address this will require formatting/parsing of fields from some other
 * associated calendar, as per ICU ticket #9043. This test should be timebombed
 * until that ticket is addressed.
 */
@Test
public void TestFormat() {
    ChineseCalendar cal = new ChineseCalendar();
    DateFormat fmt = DateFormat.getDateTimeInstance(cal, DateFormat.DEFAULT, DateFormat.DEFAULT);
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    Date[] DATA = new Date[2];
    tempcal.set(2001, Calendar.MAY, 22);
    DATA[0] = tempcal.getTime();
    tempcal.set(2001, Calendar.MAY, 23);
    DATA[1] = tempcal.getTime();
    for (int i = 0; i < DATA.length; ++i) {
        String s = fmt.format(DATA[i]);
        try {
            Date e = fmt.parse(s);
            if (e.equals(DATA[i])) {
                logln("Ok: " + DATA[i] + " -> " + s + " -> " + e);
            } else {
                errln("FAIL: " + DATA[i] + " -> " + s + " -> " + e);
            }
        } catch (java.text.ParseException e) {
            errln("Fail: " + s + " -> parse failure at " + e.getErrorOffset());
            errln(e.toString());
        }
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ChineseCalendar (android.icu.util.ChineseCalendar)18 Test (org.junit.Test)17 SimpleDateFormat (android.icu.text.SimpleDateFormat)9 GregorianCalendar (android.icu.util.GregorianCalendar)9 Date (java.util.Date)9 DateFormat (android.icu.text.DateFormat)7 Calendar (android.icu.util.Calendar)7 ChineseDateFormat (android.icu.text.ChineseDateFormat)6 BuddhistCalendar (android.icu.util.BuddhistCalendar)4 JapaneseCalendar (android.icu.util.JapaneseCalendar)4 ChineseDateFormatSymbols (android.icu.text.ChineseDateFormatSymbols)3 HebrewCalendar (android.icu.util.HebrewCalendar)3 IslamicCalendar (android.icu.util.IslamicCalendar)3 ULocale (android.icu.util.ULocale)3 DateFormatSymbols (android.icu.text.DateFormatSymbols)2 IOException (java.io.IOException)2 ParseException (java.text.ParseException)2 CalendarAstronomer (android.icu.impl.CalendarAstronomer)1 ICUResourceBundle (android.icu.impl.ICUResourceBundle)1 Field (android.icu.text.ChineseDateFormat.Field)1