Search in sources :

Example 1 with ChineseCalendar

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

the class ChineseTest method TestAdd.

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

Example 2 with ChineseCalendar

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

the class ChineseTest method TestMapping.

/**
 * Test basic mapping to and from Gregorian.
 */
@Test
public void TestMapping() {
    final int[] DATA = { // Gregorian    Chinese
    1964, 9, 4, 4601, 7, 0, 28, 1964, 9, 5, 4601, 7, 0, 29, 1964, 9, 6, 4601, 8, 0, 1, 1964, 9, 7, 4601, 8, 0, 2, 1961, 12, 25, 4598, 11, 0, 18, 1999, 6, 4, 4636, 4, 0, 21, 1990, 5, 23, 4627, 4, 0, 29, 1990, 5, 24, 4627, 5, 0, 1, 1990, 6, 22, 4627, 5, 0, 30, 1990, 6, 23, 4627, 5, 1, 1, 1990, 7, 20, 4627, 5, 1, 28, 1990, 7, 21, 4627, 5, 1, 29, 1990, 7, 22, 4627, 6, 0, 1 };
    ChineseCalendar cal = new ChineseCalendar();
    StringBuffer buf = new StringBuffer();
    logln("Gregorian -> Chinese");
    // java.util.Calendar grego = java.util.Calendar.getInstance();
    Calendar grego = Calendar.getInstance();
    grego.clear();
    for (int i = 0; i < DATA.length; ) {
        grego.set(DATA[i++], DATA[i++] - 1, DATA[i++]);
        Date date = grego.getTime();
        cal.setTime(date);
        int y = cal.get(Calendar.EXTENDED_YEAR);
        // 0-based -> 1-based
        int m = cal.get(Calendar.MONTH) + 1;
        int L = cal.get(Calendar.IS_LEAP_MONTH);
        int d = cal.get(Calendar.DAY_OF_MONTH);
        // Expected y, m, isLeapMonth, d
        int yE = DATA[i++];
        // 1-based
        int mE = DATA[i++];
        int LE = DATA[i++];
        int dE = DATA[i++];
        buf.setLength(0);
        buf.append(date + " -> ");
        buf.append(y + "/" + m + (L == 1 ? "(leap)" : "") + "/" + d);
        if (y == yE && m == mE && L == LE && d == dE) {
            logln("OK: " + buf.toString());
        } else {
            errln("Fail: " + buf.toString() + ", expected " + yE + "/" + mE + (LE == 1 ? "(leap)" : "") + "/" + dE);
        }
    }
    logln("Chinese -> Gregorian");
    for (int i = 0; i < DATA.length; ) {
        grego.set(DATA[i++], DATA[i++] - 1, DATA[i++]);
        Date dexp = grego.getTime();
        int cyear = DATA[i++];
        int cmonth = DATA[i++];
        int cisleapmonth = DATA[i++];
        int cdayofmonth = DATA[i++];
        cal.clear();
        cal.set(Calendar.EXTENDED_YEAR, cyear);
        cal.set(Calendar.MONTH, cmonth - 1);
        cal.set(Calendar.IS_LEAP_MONTH, cisleapmonth);
        cal.set(Calendar.DAY_OF_MONTH, cdayofmonth);
        Date date = cal.getTime();
        buf.setLength(0);
        buf.append(cyear + "/" + cmonth + (cisleapmonth == 1 ? "(leap)" : "") + "/" + cdayofmonth);
        buf.append(" -> " + date);
        if (date.equals(dexp)) {
            logln("OK: " + buf.toString());
        } else {
            errln("Fail: " + buf.toString() + ", expected " + dexp);
        }
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Date(java.util.Date) Test(org.junit.Test)

Example 3 with ChineseCalendar

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

the class ChineseTest method Test6510.

@Test
public void Test6510() {
    Calendar gregorianCalendar;
    ChineseCalendar chineseCalendar, chineseCalendar2;
    ChineseDateFormat dateFormat;
    SimpleDateFormat simpleDateFormat;
    simpleDateFormat = new android.icu.text.SimpleDateFormat("MM/dd/yyyy G 'at' HH:mm:ss vvvv", Locale.US);
    dateFormat = new android.icu.text.ChineseDateFormat("MM/dd/yyyy(G) HH:mm:ss", Locale.CHINA);
    // lunar to gregorian
    chineseCalendar = new ChineseCalendar(77, 26, Calendar.JANUARY, 0, 6, 0, 0, 0);
    // coverage
    assertEquals("equivalent ChineseCalendar() constructors", chineseCalendar, new ChineseCalendar(77, 26, Calendar.JANUARY, 0, 6));
    gregorianCalendar = Calendar.getInstance(Locale.US);
    gregorianCalendar.setTime(chineseCalendar.getTime());
    // gregorian to lunar
    chineseCalendar2 = new ChineseCalendar();
    chineseCalendar2.setTimeInMillis(gregorianCalendar.getTimeInMillis());
    // validate roundtrip
    if (chineseCalendar.getTimeInMillis() != chineseCalendar2.getTimeInMillis()) {
        errln("time1: " + chineseCalendar.getTimeInMillis());
        errln("time2: " + chineseCalendar2.getTimeInMillis());
        errln("Lunar [MM/dd/y(G) HH:mm:ss] " + dateFormat.format(chineseCalendar));
        errln("**PROBLEM Grego [MM/dd/y(G) HH:mm:ss] " + simpleDateFormat.format(gregorianCalendar));
        errln("Grego [MM/dd/y(G) HH:mm:ss] " + simpleDateFormat.format(gregorianCalendar));
        errln("Lunar [MM/dd/y(G) HH:mm:ss] " + dateFormat.format(chineseCalendar2));
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ChineseDateFormat(android.icu.text.ChineseDateFormat) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 4 with ChineseCalendar

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

the class ChineseTest method TestResolution.

/**
 * Make sure IS_LEAP_MONTH participates in field resolution.
 */
@Test
public void TestResolution() {
    ChineseCalendar cal = new ChineseCalendar();
    DateFormat fmt = DateFormat.getDateInstance(cal, DateFormat.DEFAULT);
    // May 22 2001 = y4638 m4 d30 doy119
    // May 23 2001 = y4638 m4* d1 doy120
    final int THE_YEAR = 4638;
    final int END = -1;
    int[] DATA = { // If we set DAY_OF_YEAR only, that should be used
    Calendar.DAY_OF_YEAR, 1, END, // Expect 1-1
    1, // Expect 1-1
    0, // Expect 1-1
    1, // If we set MONTH only, that should be used
    Calendar.IS_LEAP_MONTH, 1, Calendar.DAY_OF_MONTH, 1, Calendar.MONTH, 3, END, // Expect 4*-1
    4, // Expect 4*-1
    1, // Expect 4*-1
    1, // Should ignore
    Calendar.MONTH, // Should ignore
    1, // Should ignore
    Calendar.IS_LEAP_MONTH, // Should ignore
    1, // Should ignore
    Calendar.DAY_OF_MONTH, // Should ignore
    1, Calendar.DAY_OF_YEAR, 121, END, // Expect 4*-2
    4, // Expect 4*-2
    1, // Expect 4*-2
    2, // If we set IS_LEAP_MONTH last, that should take precedence
    Calendar.MONTH, 3, Calendar.DAY_OF_MONTH, 1, // Should ignore
    Calendar.DAY_OF_YEAR, // Should ignore
    5, Calendar.IS_LEAP_MONTH, 1, END, // Expect 4*-1
    4, // Expect 4*-1
    1, // Expect 4*-1
    1 };
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < DATA.length; ) {
        cal.clear();
        cal.set(Calendar.EXTENDED_YEAR, THE_YEAR);
        buf.setLength(0);
        buf.append("EXTENDED_YEAR=" + THE_YEAR);
        while (DATA[i] != END) {
            cal.set(DATA[i++], DATA[i++]);
            buf.append(" " + fieldName(DATA[i - 2]) + "=" + DATA[i - 1]);
        }
        // Skip over END mark
        ++i;
        int expMonth = DATA[i++] - 1;
        int expIsLeapMonth = DATA[i++];
        int expDOM = DATA[i++];
        int month = cal.get(Calendar.MONTH);
        int isLeapMonth = cal.get(Calendar.IS_LEAP_MONTH);
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        if (expMonth == month && expIsLeapMonth == isLeapMonth && dom == expDOM) {
            logln("OK: " + buf + " => " + fmt.format(cal.getTime()));
        } else {
            String s = fmt.format(cal.getTime());
            cal.clear();
            cal.set(Calendar.EXTENDED_YEAR, THE_YEAR);
            cal.set(Calendar.MONTH, expMonth);
            cal.set(Calendar.IS_LEAP_MONTH, expIsLeapMonth);
            cal.set(Calendar.DAY_OF_MONTH, expDOM);
            errln("Fail: " + buf + " => " + s + "=" + (month + 1) + "," + isLeapMonth + "," + dom + ", expected " + fmt.format(cal.getTime()) + "=" + (expMonth + 1) + "," + expIsLeapMonth + "," + expDOM);
        }
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) Test(org.junit.Test)

Example 5 with ChineseCalendar

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

the class DateFormatTest method TestFormatToCharacterIteratorCoverage.

/*
     * API coverage test case for formatToCharacterIterator
     */
@Test
public void TestFormatToCharacterIteratorCoverage() {
    // Calling formatToCharacterIterator, using various argument types
    DateFormat df = DateFormat.getDateTimeInstance();
    AttributedCharacterIterator acit = null;
    Calendar cal = Calendar.getInstance();
    try {
        acit = df.formatToCharacterIterator(cal);
        if (acit == null) {
            errln("FAIL: null AttributedCharacterIterator returned by formatToCharacterIterator(Calendar)");
        }
    } catch (IllegalArgumentException iae) {
        errln("FAIL: Calendar must be accepted by formatToCharacterIterator");
    }
    Date d = cal.getTime();
    try {
        acit = df.formatToCharacterIterator(d);
        if (acit == null) {
            errln("FAIL: null AttributedCharacterIterator returned by formatToCharacterIterator(Date)");
        }
    } catch (IllegalArgumentException iae) {
        errln("FAIL: Date must be accepted by formatToCharacterIterator");
    }
    Number num = new Long(d.getTime());
    try {
        acit = df.formatToCharacterIterator(num);
        if (acit == null) {
            errln("FAIL: null AttributedCharacterIterator returned by formatToCharacterIterator(Number)");
        }
    } catch (IllegalArgumentException iae) {
        errln("FAIL: Number must be accepted by formatToCharacterIterator");
    }
    boolean isException = false;
    String str = df.format(d);
    try {
        acit = df.formatToCharacterIterator(str);
        if (acit == null) {
            errln("FAIL: null AttributedCharacterIterator returned by formatToCharacterIterator(String)");
        }
    } catch (IllegalArgumentException iae) {
        logln("IllegalArgumentException is thrown by formatToCharacterIterator");
        isException = true;
    }
    if (!isException) {
        errln("FAIL: String must not be accepted by formatToCharacterIterator");
    }
    // DateFormat.Field#ofCalendarField and getCalendarField
    for (int i = 0; i < DATEFORMAT_FIELDS.length; i++) {
        int calField = DATEFORMAT_FIELDS[i].getCalendarField();
        if (calField != -1) {
            DateFormat.Field field = DateFormat.Field.ofCalendarField(calField);
            if (field != DATEFORMAT_FIELDS[i]) {
                errln("FAIL: " + field + " is returned for a Calendar field " + calField + " - Expected: " + DATEFORMAT_FIELDS[i]);
            }
        }
    }
    // IllegalArgument for ofCalendarField
    isException = false;
    try {
        DateFormat.Field.ofCalendarField(-1);
    } catch (IllegalArgumentException iae) {
        logln("IllegalArgumentException is thrown by ofCalendarField");
        isException = true;
    }
    if (!isException) {
        errln("FAIL: IllegalArgumentException must be thrown by ofCalendarField for calendar field value -1");
    }
    // ChineseDateFormat.Field#ofCalendarField and getCalendarField
    int ccalField = ChineseDateFormat.Field.IS_LEAP_MONTH.getCalendarField();
    if (ccalField != Calendar.IS_LEAP_MONTH) {
        errln("FAIL: ChineseCalendar field " + ccalField + " is returned for ChineseDateFormat.Field.IS_LEAP_MONTH.getCalendarField()");
    } else {
        DateFormat.Field cfield = ChineseDateFormat.Field.ofCalendarField(ccalField);
        if (cfield != ChineseDateFormat.Field.IS_LEAP_MONTH) {
            errln("FAIL: " + cfield + " is returned for a ChineseCalendar field " + ccalField + " - Expected: " + ChineseDateFormat.Field.IS_LEAP_MONTH);
        }
    }
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) BuddhistCalendar(android.icu.util.BuddhistCalendar) HebrewCalendar(android.icu.util.HebrewCalendar) IslamicCalendar(android.icu.util.IslamicCalendar) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) Field(android.icu.text.ChineseDateFormat.Field) Date(java.util.Date) AttributedCharacterIterator(java.text.AttributedCharacterIterator) 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