Search in sources :

Example 11 with ChineseCalendar

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

the class ChineseTest method TestInitWithCurrentTime.

@Test
public void TestInitWithCurrentTime() {
    // jb4555
    // if the chinese calendar current millis isn't called, the default year is wrong.
    // this test is assuming the 'year' is the current cycle
    // so when we cross a cycle boundary, the target will need to change
    // that shouldn't be for awhile yet...
    ChineseCalendar cc = new ChineseCalendar();
    cc.set(Calendar.YEAR, 22);
    cc.set(Calendar.MONTH, 0);
    // need to set leap month flag off, otherwise, the test case always fails when
    // current time is in a leap month
    cc.set(Calendar.IS_LEAP_MONTH, 0);
    cc.set(Calendar.DATE, 19);
    cc.set(Calendar.HOUR_OF_DAY, 0);
    cc.set(Calendar.MINUTE, 0);
    cc.set(Calendar.SECOND, 0);
    cc.set(Calendar.MILLISECOND, 0);
    cc.add(Calendar.DATE, 1);
    Calendar cal = new GregorianCalendar(2005, Calendar.FEBRUARY, 28);
    Date target = cal.getTime();
    Date result = cc.getTime();
    assertEquals("chinese and gregorian date should match", target, result);
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 12 with ChineseCalendar

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

the class ChineseTest method TestLimits.

/**
 * Test minimum and maximum functions.
 */
@Test
public void TestLimits() {
    // The number of days and the start date can be adjusted
    // arbitrarily to either speed up the test or make it more
    // thorough, but try to test at least a full year, preferably a
    // full non-leap and a full leap year.
    // Final parameter is either number of days, if > 0, or test
    // duration in seconds, if < 0.
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(1989, Calendar.NOVEMBER, 1);
    ChineseCalendar chinese = new ChineseCalendar();
    doLimitsTest(chinese, null, tempcal.getTime());
    doTheoreticalLimitsTest(chinese, true);
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) Test(org.junit.Test)

Example 13 with ChineseCalendar

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

the class ChineseTest method TestJulianDayMapping.

/**
 * Run through several standard tests from Dershowitz & Reingold.
 */
@Test
public void TestJulianDayMapping() {
    final TestCase[] tests = { // Julian Day   Era  Year Month  Leap   DOM WkDay
    new ChineseTestCase(1507231.5, 35, 11, 6, false, 12, SUN), new ChineseTestCase(1660037.5, 42, 9, 10, false, 27, WED), new ChineseTestCase(1746893.5, 46, 7, 8, false, 4, WED), new ChineseTestCase(1770641.5, 47, 12, 8, false, 9, SUN), new ChineseTestCase(1892731.5, 52, 46, 11, false, 20, WED), new ChineseTestCase(1931579.5, 54, 33, 4, false, 5, MON), new ChineseTestCase(1974851.5, 56, 31, 10, false, 15, SAT), new ChineseTestCase(2091164.5, 61, 50, 3, false, 7, SUN), new ChineseTestCase(2121509.5, 63, 13, 4, false, 24, SUN), new ChineseTestCase(2155779.5, 64, 47, 2, false, 9, FRI), new ChineseTestCase(2174029.5, 65, 37, 2, false, 9, SAT), new ChineseTestCase(2191584.5, 66, 25, 2, false, 23, FRI), // *
    new ChineseTestCase(2195261.5, 66, 35, 3, false, 9, SUN), // *
    new ChineseTestCase(2229274.5, 68, 8, 5, false, 2, SUN), // **
    new ChineseTestCase(2245580.5, 68, 53, 1, false, 8, WED), new ChineseTestCase(2266100.5, 69, 49, 3, false, 4, SAT), // *
    new ChineseTestCase(2288542.5, 70, 50, 8, false, 2, SAT), // *
    new ChineseTestCase(2290901.5, 70, 57, 1, false, 29, SAT), // *
    new ChineseTestCase(2323140.5, 72, 25, 4, true, 20, WED), new ChineseTestCase(2334848.5, 72, 57, 6, false, 5, SUN), new ChineseTestCase(2348020.5, 73, 33, 6, false, 6, FRI), new ChineseTestCase(2366978.5, 74, 25, 5, false, 5, SUN), new ChineseTestCase(2385648.5, 75, 16, 6, false, 12, MON), new ChineseTestCase(2392825.5, 75, 36, 2, false, 13, WED), new ChineseTestCase(2416223.5, 76, 40, 3, false, 22, SUN), new ChineseTestCase(2425848.5, 77, 6, 7, false, 21, SUN), new ChineseTestCase(2430266.5, 77, 18, 8, false, 9, MON), new ChineseTestCase(2430833.5, 77, 20, 3, false, 15, MON), new ChineseTestCase(2431004.5, 77, 20, 9, false, 9, THU), new ChineseTestCase(2448698.5, 78, 9, 2, false, 14, TUE), new ChineseTestCase(2450138.5, 78, 13, 1, false, 7, SUN), new ChineseTestCase(2465737.5, 78, 55, 10, false, 14, WED), new ChineseTestCase(2486076.5, 79, 51, 6, false, 7, SUN), // year 60
    new ChineseTestCase(2467496.5, 78, 60, 8, false, 2, FRI) };
    ChineseCalendar cal = new ChineseCalendar();
    cal.setLenient(true);
    doTestCases(tests, cal);
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) Test(org.junit.Test)

Example 14 with ChineseCalendar

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

the class ChineseTest method TestOutOfRange.

/**
 * Test the behavior of fields that are out of range.
 */
@Test
public void TestOutOfRange() {
    int[] DATA = new int[] { // Input       Output
    4638, 13, 1, 4639, 1, 1, 4638, 18, 1, 4639, 6, 1, 4639, 0, 1, 4638, 12, 1, 4639, -6, 1, 4638, 6, 1, // 1-4638 has 30 days
    4638, // 1-4638 has 30 days
    1, // 1-4638 has 30 days
    32, // 1-4638 has 30 days
    4638, // 1-4638 has 30 days
    2, // 1-4638 has 30 days
    2, 4638, 2, -1, 4638, 1, 29 };
    ChineseCalendar cal = new ChineseCalendar();
    for (int i = 0; i < DATA.length; ) {
        int y1 = DATA[i++];
        int m1 = DATA[i++] - 1;
        int d1 = DATA[i++];
        int y2 = DATA[i++];
        int m2 = DATA[i++] - 1;
        int d2 = DATA[i++];
        cal.clear();
        cal.set(Calendar.EXTENDED_YEAR, y1);
        cal.set(MONTH, m1);
        cal.set(DATE, d1);
        int y = cal.get(Calendar.EXTENDED_YEAR);
        int m = cal.get(MONTH);
        int d = cal.get(DATE);
        if (y != y2 || m != m2 || d != d2) {
            errln("Fail: " + y1 + "/" + (m1 + 1) + "/" + d1 + " resolves to " + y + "/" + (m + 1) + "/" + d + ", expected " + y2 + "/" + (m2 + 1) + "/" + d2);
        } else if (isVerbose()) {
            logln("OK: " + y1 + "/" + (m1 + 1) + "/" + d1 + " resolves to " + y + "/" + (m + 1) + "/" + d);
        }
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) Test(org.junit.Test)

Example 15 with ChineseCalendar

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

the class ChineseTest method TestCoverage.

// public void TestFindLeapMonths() {
// ChineseCalendar cal = new ChineseCalendar();
// cal.setTime(new Date(2000-1900, Calendar.JANUARY, 1));
// long end = new Date(2100-1900, Calendar.JANUARY, 1).getTime();
// ChineseDateFormat fmt = (ChineseDateFormat) DateFormat.getInstance(cal);
// fmt.applyPattern("u-MMl-dd, 'Year' y, 'Cycle' G");
// while (cal.getTimeInMillis() < end) {
// if (cal.get(ChineseCalendar.IS_LEAP_MONTH) != 0) {
// cal.set(Calendar.DAY_OF_MONTH, 1);
// logln(cal.getTime() + " = " + fmt.format(cal.getTime()));
// cal.set(Calendar.DAY_OF_MONTH, 29);
// }
// cal.add(Calendar.DAY_OF_YEAR, 25);
// }
// }
@Test
public void TestCoverage() {
    // Coverage for constructors
    {
        // new ChineseCalendar(Date)
        ChineseCalendar cal = new ChineseCalendar(new Date());
        if (cal == null) {
            errln("could not create ChineseCalendar with Date");
        }
    }
    {
        // new ChineseCalendar(int year, int month, int isLeapMonth, int date)
        ChineseCalendar cal = new ChineseCalendar(23, Calendar.JULY, 1, 2);
        if (cal == null) {
            errln("could not create ChineseCalendar with year,month,isLeapMonth,date");
        }
        // Make sure the given values are properly set
        if (cal.get(Calendar.YEAR) != 23 || cal.get(Calendar.MONTH) != Calendar.JULY || cal.get(Calendar.IS_LEAP_MONTH) != 1 || cal.get(Calendar.DATE) != 2 || cal.get(Calendar.MILLISECONDS_IN_DAY) != 0) {
            errln("ChineseCalendar was initialized incorrectly with year,month,isLeapMonth,date");
        }
    }
    {
        // new ChineseCalendar(int year, int month, int isLeapMonth, int date, int hour, int minute, int second)
        ChineseCalendar cal = new ChineseCalendar(23, Calendar.JULY, 1, 2, 12, 34, 56);
        if (cal == null) {
            errln("could not create ChineseCalendar with year,month,isLeapMonth,date,hour,minute,second");
        }
        // Make sure the given values are properly set
        if (cal.get(Calendar.YEAR) != 23 || cal.get(Calendar.MONTH) != Calendar.JULY || cal.get(Calendar.IS_LEAP_MONTH) != 1 || cal.get(Calendar.DATE) != 2 || cal.get(Calendar.HOUR_OF_DAY) != 12 || cal.get(Calendar.MINUTE) != 34 || cal.get(Calendar.SECOND) != 56 || cal.get(Calendar.MILLISECOND) != 0) {
            errln("ChineseCalendar was initialized incorrectly with year,month,isLeapMonth,date,hour,minute,second");
        }
    }
    {
        // new ChineseCalendar(Locale)
        ChineseCalendar cal = new ChineseCalendar(Locale.getDefault());
        if (cal == null) {
            errln("could not create ChineseCalendar with Locale");
        }
    }
    {
        // new ChineseCalendar(ULocale)
        ChineseCalendar cal = new ChineseCalendar(ULocale.getDefault());
        if (cal == null) {
            errln("could not create ChineseCalendar with ULocale");
        }
    }
    {
        // new ChineseCalendar(TimeZone)
        ChineseCalendar cal = new ChineseCalendar(TimeZone.getDefault());
        if (cal == null) {
            errln("could not create ChineseCalendar with TimeZone");
        }
    }
    {
        // new ChineseCalendar(TimeZone, Locale)
        ChineseCalendar cal = new ChineseCalendar(TimeZone.getDefault(), Locale.getDefault());
        if (cal == null) {
            errln("could not create ChineseCalendar with TimeZone,Locale");
        }
    }
    {
        // new ChineseCalendar(TimeZone, ULocale)
        ChineseCalendar cal = new ChineseCalendar(TimeZone.getDefault(), ULocale.getDefault());
        if (cal == null) {
            errln("could not create ChineseCalendar with TimeZone,ULocale");
        }
    }
    // Note: ICU 50 or later versions, DateFormat.getInstance(ChineseCalendar) no longer
    // returns an instance of ChineseDateFormat. Chinese calendar formatting support was
    // changed and integrated into SimpleDateFormat since ICU 49. Also, ChineseDateFormat
    // specific pattern letter "l" is no longer used by the new implementation.
    // ChineseCalendar cal = new ChineseCalendar();
    // DateFormat format = DateFormat.getInstance(cal);
    // if(!(format instanceof ChineseDateFormat)){
    // errln("DateFormat.getInstance("+cal+") did not return a ChineseDateFormat");
    // }
    // ChineseDateFormat fmt = (ChineseDateFormat)format;
    // fmt.applyPattern("llyyll");
    // Date time = getDate(2100, Calendar.JANUARY, 1);
    // String str = fmt.format(time);
    // try {
    // Date e = fmt.parse(str);
    // logln("chinese calendar time: " + time + " result: " + str + " --> " + e);
    // } catch (java.text.ParseException ex) {
    // logln(ex.getMessage()); // chinese calendar can't parse this, no error for now
    // }
    // new ChineseCalendar(TimeZone,ULocale)
    ChineseCalendar ccal2 = new ChineseCalendar(TimeZone.getDefault(), ULocale.CHINA);
    if (ccal2 == null) {
        errln("could not create ChineseCalendar with TimeZone ULocale");
    } else {
        DateFormat fmt2 = DateFormat.getDateInstance(ccal2, DateFormat.DEFAULT, ULocale.CHINA);
        Date time2 = getDate(2001, Calendar.MAY, 23);
        String str2 = fmt2.format(time2);
        logln("Chinese calendar time: " + time2 + " result: " + str2);
    }
}
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