Search in sources :

Example 76 with GregorianCalendar

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

the class DateFormatTest method TestCoverage.

@Test
public void TestCoverage() {
    Date now = new Date();
    Calendar cal = new GregorianCalendar();
    DateFormat f = DateFormat.getTimeInstance();
    logln("time: " + f.format(now));
    // sigh, everyone overrides this
    int hash = f.hashCode();
    f = DateFormat.getInstance(cal);
    if (hash == f.hashCode()) {
        errln("FAIL: hashCode equal for inequal objects");
    }
    logln("time again: " + f.format(now));
    f = DateFormat.getTimeInstance(cal, DateFormat.FULL);
    logln("time yet again: " + f.format(now));
    f = DateFormat.getDateInstance();
    logln("time yet again: " + f.format(now));
    ICUResourceBundle rb = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, "de_DE");
    DateFormatSymbols sym = new DateFormatSymbols(rb, Locale.GERMANY);
    DateFormatSymbols sym2 = (DateFormatSymbols) sym.clone();
    if (sym.hashCode() != sym2.hashCode()) {
        errln("fail, date format symbols hashcode not equal");
    }
    if (!sym.equals(sym2)) {
        errln("fail, date format symbols not equal");
    }
    Locale foo = new Locale("fu", "FU", "BAR");
    rb = null;
    sym = new DateFormatSymbols(GregorianCalendar.class, foo);
    sym.equals(null);
    sym = new ChineseDateFormatSymbols();
    sym = new ChineseDateFormatSymbols(new Locale("en_US"));
    try {
        sym = new ChineseDateFormatSymbols(null, new Locale("en_US"));
        errln("ChineseDateFormatSymbols(Calender, Locale) was suppose to return a null " + "pointer exception for a null paramater.");
    } catch (Exception e) {
    }
    sym = new ChineseDateFormatSymbols(new ChineseCalendar(), new Locale("en_US"));
    try {
        sym = new ChineseDateFormatSymbols(null, new ULocale("en_US"));
        errln("ChineseDateFormatSymbols(Calender, ULocale) was suppose to return a null " + "pointer exception for a null paramater.");
    } catch (Exception e) {
    }
    sym = new ChineseDateFormatSymbols(new ChineseCalendar(), foo);
    // cover new ChineseDateFormatSymbols(Calendar, ULocale)
    ChineseCalendar ccal = new ChineseCalendar();
    // gclsh1 add
    sym = new ChineseDateFormatSymbols(ccal, ULocale.CHINA);
    StringBuffer buf = new StringBuffer();
    FieldPosition pos = new FieldPosition(0);
    f.format((Object) cal, buf, pos);
    f.format((Object) now, buf, pos);
    f.format((Object) new Long(now.getTime()), buf, pos);
    try {
        f.format((Object) "Howdy", buf, pos);
    } catch (Exception e) {
    }
    NumberFormat nf = f.getNumberFormat();
    f.setNumberFormat(nf);
    boolean lenient = f.isLenient();
    f.setLenient(lenient);
    ULocale uloc = f.getLocale(ULocale.ACTUAL_LOCALE);
    DateFormat sdfmt = new SimpleDateFormat();
    if (f.hashCode() != f.hashCode()) {
        errln("hashCode is not stable");
    }
    if (!f.equals(f)) {
        errln("f != f");
    }
    if (f.equals(null)) {
        errln("f should not equal null");
    }
    if (f.equals(sdfmt)) {
        errln("A time instance shouldn't equal a default date format");
    }
    Date d;
    {
        ChineseDateFormat fmt = new ChineseDateFormat("yymm", Locale.US);
        try {
            // fewer symbols than required 2
            fmt.parse("2");
            errln("whoops");
        } catch (ParseException e) {
            logln("ok");
        }
        try {
            // should succeed with obeycount
            fmt.parse("2255");
            logln("ok");
        } catch (ParseException e) {
            errln("whoops");
        }
        try {
            // not a number, should fail
            fmt.parse("ni hao");
            errln("whoops ni hao");
        } catch (ParseException e) {
            logln("ok ni hao");
        }
    }
    {
        Calendar xcal = new GregorianCalendar();
        xcal.set(Calendar.HOUR_OF_DAY, 0);
        DateFormat fmt = new SimpleDateFormat("k");
        StringBuffer xbuf = new StringBuffer();
        FieldPosition fpos = new FieldPosition(Calendar.HOUR_OF_DAY);
        fmt.format(xcal, xbuf, fpos);
        try {
            fmt.parse(xbuf.toString());
            logln("ok");
            xbuf.setLength(0);
            xcal.set(Calendar.HOUR_OF_DAY, 25);
            fmt.format(xcal, xbuf, fpos);
            Date d2 = fmt.parse(xbuf.toString());
            logln("ok again - d2=" + d2);
        } catch (ParseException e) {
            errln("whoops");
        }
    }
    {
        // cover gmt+hh:mm
        DateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
        try {
            d = fmt.parse("07/10/53 GMT+10:00");
            logln("ok : d = " + d);
        } catch (ParseException e) {
            errln("Parse of 07/10/53 GMT+10:00 for pattern MM/dd/yy z");
        }
        // cover invalid separator after GMT
        {
            ParsePosition pp = new ParsePosition(0);
            String text = "07/10/53 GMT=10:00";
            d = fmt.parse(text, pp);
            if (pp.getIndex() != 12) {
                errln("Parse of 07/10/53 GMT=10:00 for pattern MM/dd/yy z");
            }
            logln("Parsing of the text stopped at pos: " + pp.getIndex() + " as expected and length is " + text.length());
        }
        // cover bad text after GMT+.
        try {
            fmt.parse("07/10/53 GMT+blecch");
            logln("ok GMT+blecch");
        } catch (ParseException e) {
            errln("whoops GMT+blecch");
        }
        // cover bad text after GMT+hh:.
        try {
            fmt.parse("07/10/53 GMT+07:blecch");
            logln("ok GMT+xx:blecch");
        } catch (ParseException e) {
            errln("whoops GMT+xx:blecch");
        }
        // cover no ':' GMT+#, # < 24 (hh)
        try {
            d = fmt.parse("07/10/53 GMT+07");
            logln("ok GMT+07");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 GMT+07 for pattern MM/dd/yy z");
        }
        // cover no ':' GMT+#, # > 24 (hhmm)
        try {
            d = fmt.parse("07/10/53 GMT+0730");
            logln("ok");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 GMT+0730 for pattern MM/dd/yy z");
        }
        // cover GMT+#, # with second field
        try {
            d = fmt.parse("07/10/53 GMT+07:30:15");
            logln("ok GMT+07:30:15");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 GMT+07:30:15 for pattern MM/dd/yy z");
        }
        // cover no ':' GMT+#, # with second field, no leading zero
        try {
            d = fmt.parse("07/10/53 GMT+73015");
            logln("ok GMT+73015");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 GMT+73015 for pattern MM/dd/yy z");
        }
        // cover no ':' GMT+#, # with 1 digit second field
        try {
            d = fmt.parse("07/10/53 GMT+07300");
            logln("ok GMT+07300");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 GMT+07300 for pattern MM/dd/yy z");
        }
        // cover raw digits with no leading sign (bad RFC822)
        try {
            d = fmt.parse("07/10/53 07");
            errln("Parse of 07/10/53 07 for pattern MM/dd/yy z passed!");
        } catch (ParseException e) {
            logln("ok");
        }
        // cover raw digits (RFC822)
        try {
            d = fmt.parse("07/10/53 +07");
            logln("ok");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 +07 for pattern MM/dd/yy z failed");
        }
        // cover raw digits (RFC822)
        try {
            d = fmt.parse("07/10/53 -0730");
            logln("ok");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 -00730 for pattern MM/dd/yy z failed");
        }
        // cover raw digits (RFC822) in DST
        try {
            fmt.setTimeZone(TimeZone.getTimeZone("PDT"));
            d = fmt.parse("07/10/53 -0730");
            logln("ok");
        } catch (ParseException e) {
            errln("Parse of 07/10/53 -0730 for pattern MM/dd/yy z failed");
        }
    }
    // TODO: revisit toLocalizedPattern
    if (false) {
        SimpleDateFormat fmt = new SimpleDateFormat("aabbcc");
        try {
            String pat = fmt.toLocalizedPattern();
            errln("whoops, shouldn't have been able to localize aabbcc");
        } catch (IllegalArgumentException e) {
            logln("aabbcc localize ok");
        }
    }
    {
        SimpleDateFormat fmt = new SimpleDateFormat("'aabbcc");
        try {
            fmt.toLocalizedPattern();
            errln("whoops, localize unclosed quote");
        } catch (IllegalArgumentException e) {
            logln("localize unclosed quote ok");
        }
    }
    {
        SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
        // bogus time zone
        String text = "08/15/58 DBDY";
        try {
            fmt.parse(text);
            errln("recognized bogus time zone DBDY");
        } catch (ParseException e) {
            logln("time zone ex ok");
        }
    }
    {
        // force fallback to default timezone when fmt timezone
        // is not named
        SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
        // force fallback to default time zone, still fails
        // not in equivalency group
        fmt.setTimeZone(TimeZone.getTimeZone("GMT+0147"));
        String text = "08/15/58 DBDY";
        try {
            fmt.parse(text);
            errln("Parse of 07/10/53 DBDY for pattern MM/dd/yy z passed");
        } catch (ParseException e) {
            logln("time zone ex2 ok");
        }
        // force success on fallback
        text = "08/15/58 " + TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT);
        try {
            fmt.parse(text);
            logln("found default tz");
        } catch (ParseException e) {
            errln("whoops, got parse exception");
        }
    }
    {
        // force fallback to symbols list of timezones when neither
        // fmt and default timezone is named
        SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy z");
        TimeZone oldtz = TimeZone.getDefault();
        // nonstandard tz
        TimeZone newtz = TimeZone.getTimeZone("GMT+0137");
        fmt.setTimeZone(newtz);
        // todo: fix security issue
        TimeZone.setDefault(newtz);
        // fallback to symbol list, but fail
        // try to parse the bogus time zone
        String text = "08/15/58 DBDY";
        try {
            fmt.parse(text);
            errln("Parse of 07/10/53 DBDY for pattern MM/dd/yy z passed");
        } catch (ParseException e) {
            logln("time zone ex3 ok");
        } catch (Exception e) {
            // hmmm... this shouldn't happen.  don't want to exit this
            // fn with timezone improperly set, so just in case
            TimeZone.setDefault(oldtz);
            throw new IllegalStateException(e.getMessage());
        }
    }
    {
        // cover getAvailableULocales
        final ULocale[] locales = DateFormat.getAvailableULocales();
        long count = locales.length;
        if (count == 0) {
            errln(" got a empty list for getAvailableULocales");
        } else {
            logln("" + count + " available ulocales");
        }
    }
    {
        // cover DateFormatSymbols.getDateFormatBundle
        cal = new GregorianCalendar();
        Locale loc = Locale.getDefault();
        DateFormatSymbols mysym = new DateFormatSymbols(cal, loc);
        if (mysym == null)
            errln("FAIL: constructs DateFormatSymbols with calendar and locale failed");
        uloc = ULocale.getDefault();
        // These APIs are obsolete and return null
        ResourceBundle resb = DateFormatSymbols.getDateFormatBundle(cal, loc);
        ResourceBundle resb2 = DateFormatSymbols.getDateFormatBundle(cal, uloc);
        ResourceBundle resb3 = DateFormatSymbols.getDateFormatBundle(cal.getClass(), loc);
        ResourceBundle resb4 = DateFormatSymbols.getDateFormatBundle(cal.getClass(), uloc);
        if (resb != null) {
            logln("resb is not null");
        }
        if (resb2 != null) {
            logln("resb2 is not null");
        }
        if (resb3 != null) {
            logln("resb3 is not null");
        }
        if (resb4 != null) {
            logln("resb4 is not null");
        }
    }
    {
        // cover DateFormatSymbols.getInstance
        DateFormatSymbols datsym1 = DateFormatSymbols.getInstance();
        DateFormatSymbols datsym2 = new DateFormatSymbols();
        if (!datsym1.equals(datsym2)) {
            errln("FAIL: DateFormatSymbols returned by getInstance()" + "does not match new DateFormatSymbols().");
        }
        datsym1 = DateFormatSymbols.getInstance(Locale.JAPAN);
        datsym2 = DateFormatSymbols.getInstance(ULocale.JAPAN);
        if (!datsym1.equals(datsym2)) {
            errln("FAIL: DateFormatSymbols returned by getInstance(Locale.JAPAN)" + "does not match the one returned by getInstance(ULocale.JAPAN).");
        }
    }
    {
        // cover DateFormatSymbols.getAvailableLocales/getAvailableULocales
        Locale[] allLocales = DateFormatSymbols.getAvailableLocales();
        if (allLocales.length == 0) {
            errln("FAIL: Got a empty list for DateFormatSymbols.getAvailableLocales");
        } else {
            logln("PASS: " + allLocales.length + " available locales returned by DateFormatSymbols.getAvailableLocales");
        }
        ULocale[] allULocales = DateFormatSymbols.getAvailableULocales();
        if (allULocales.length == 0) {
            errln("FAIL: Got a empty list for DateFormatSymbols.getAvailableLocales");
        } else {
            logln("PASS: " + allULocales.length + " available locales returned by DateFormatSymbols.getAvailableULocales");
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) ChineseDateFormatSymbols(android.icu.text.ChineseDateFormatSymbols) ParsePosition(java.text.ParsePosition) ChineseCalendar(android.icu.util.ChineseCalendar) ULocale(android.icu.util.ULocale) 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) GregorianCalendar(android.icu.util.GregorianCalendar) ICUResourceBundle(android.icu.impl.ICUResourceBundle) FieldPosition(java.text.FieldPosition) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException) TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormatSymbols(android.icu.text.DateFormatSymbols) ChineseDateFormatSymbols(android.icu.text.ChineseDateFormatSymbols) ICUResourceBundle(android.icu.impl.ICUResourceBundle) UResourceBundle(android.icu.util.UResourceBundle) ResourceBundle(java.util.ResourceBundle) ParseException(java.text.ParseException) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 77 with GregorianCalendar

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

the class IBMCalendarTest method TestGregorianLimits.

/**
 * Test limits of the Gregorian calendar.
 */
@Test
public void TestGregorianLimits() {
    // Final parameter is either number of days, if > 0, or test
    // duration in seconds, if < 0.
    Calendar cal = Calendar.getInstance();
    cal.set(2004, Calendar.JANUARY, 1);
    GregorianCalendar gregorian = new GregorianCalendar();
    doLimitsTest(gregorian, null, cal.getTime());
    doTheoreticalLimitsTest(gregorian, false);
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) TaiwanCalendar(android.icu.util.TaiwanCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) BuddhistCalendar(android.icu.util.BuddhistCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 78 with GregorianCalendar

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

the class IBMCalendarTest method TestComparable.

@Test
public void TestComparable() {
    GregorianCalendar c0 = new GregorianCalendar();
    GregorianCalendar c1 = new GregorianCalendar();
    c1.add(Calendar.DAY_OF_MONTH, 1);
    if (c0.compareTo(c1) >= 0) {
        errln("calendar " + c0 + " not < " + c1);
    }
    c0.add(Calendar.MONTH, 1);
    if (c0.compareTo(c1) <= 0) {
        errln("calendar " + c0 + " not > " + c1);
    }
    c0.setTimeInMillis(c1.getTimeInMillis());
    if (c0.compareTo(c1) != 0) {
        errln("calendar " + c0 + " not == " + c1);
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Test(org.junit.Test)

Example 79 with GregorianCalendar

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

the class IBMCalendarTest method TestJB1684.

@Test
public void TestJB1684() {
    class TestData {

        int year;

        int month;

        int date;

        int womyear;

        int wommon;

        int wom;

        int dow;

        String data;

        String normalized;

        public TestData(int year, int month, int date, int womyear, int wommon, int wom, int dow, String data, String normalized) {
            this.year = year;
            this.month = month - 1;
            this.date = date;
            this.womyear = womyear;
            this.wommon = wommon - 1;
            this.wom = wom;
            this.dow = dow;
            // year, month, week of month, day
            this.data = data;
            this.normalized = data;
            if (normalized != null)
                this.normalized = normalized;
        }
    }
    // July 2001            August 2001           January 2002
    // Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
    // 1  2  3  4  5  6  7            1  2  3  4         1  2  3  4  5
    // 8  9 10 11 12 13 14   5  6  7  8  9 10 11   6  7  8  9 10 11 12
    // 15 16 17 18 19 20 21  12 13 14 15 16 17 18  13 14 15 16 17 18 19
    // 22 23 24 25 26 27 28  19 20 21 22 23 24 25  20 21 22 23 24 25 26
    // 29 30 31              26 27 28 29 30 31     27 28 29 30 31
    TestData[] tests = { new TestData(2001, 8, 6, 2001, 8, 2, Calendar.MONDAY, "2001 08 02 Mon", null), new TestData(2001, 8, 7, 2001, 8, 2, Calendar.TUESDAY, "2001 08 02 Tue", null), new TestData(2001, 8, 5, /*12,*/
    2001, 8, 2, Calendar.SUNDAY, "2001 08 02 Sun", null), new TestData(2001, 8, 6, /*7,  30,*/
    2001, 7, 6, Calendar.MONDAY, "2001 07 06 Mon", "2001 08 02 Mon"), new TestData(2001, 8, 7, /*7,  31,*/
    2001, 7, 6, Calendar.TUESDAY, "2001 07 06 Tue", "2001 08 02 Tue"), new TestData(2001, 8, 5, 2001, 7, 6, Calendar.SUNDAY, "2001 07 06 Sun", "2001 08 02 Sun"), new TestData(2001, 7, 30, 2001, 8, 1, Calendar.MONDAY, "2001 08 01 Mon", "2001 07 05 Mon"), new TestData(2001, 7, 31, 2001, 8, 1, Calendar.TUESDAY, "2001 08 01 Tue", "2001 07 05 Tue"), new TestData(2001, 7, 29, /*8,  5,*/
    2001, 8, 1, Calendar.SUNDAY, "2001 08 01 Sun", "2001 07 05 Sun"), new TestData(2001, 12, 31, 2001, 12, 6, Calendar.MONDAY, "2001 12 06 Mon", null), new TestData(2002, 1, 1, 2002, 1, 1, Calendar.TUESDAY, "2002 01 01 Tue", null), new TestData(2002, 1, 2, 2002, 1, 1, Calendar.WEDNESDAY, "2002 01 01 Wed", null), new TestData(2002, 1, 3, 2002, 1, 1, Calendar.THURSDAY, "2002 01 01 Thu", null), new TestData(2002, 1, 4, 2002, 1, 1, Calendar.FRIDAY, "2002 01 01 Fri", null), new TestData(2002, 1, 5, 2002, 1, 1, Calendar.SATURDAY, "2002 01 01 Sat", null), new TestData(2001, 12, 30, /*2002, 1,  6,*/
    2002, 1, 1, Calendar.SUNDAY, "2002 01 01 Sun", "2001 12 06 Sun") };
    int pass = 0, error = 0, warning = 0;
    final String pattern = "yyyy MM WW EEE";
    GregorianCalendar cal = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    sdf.setCalendar(cal);
    cal.setFirstDayOfWeek(Calendar.SUNDAY);
    cal.setMinimalDaysInFirstWeek(1);
    for (int i = 0; i < tests.length; ++i) {
        TestData test = tests[i];
        log("\n-----\nTesting round trip of " + test.year + " " + (test.month + 1) + " " + test.date + " (written as) " + test.data);
        cal.clear();
        cal.set(test.year, test.month, test.date);
        Date ms = cal.getTime();
        cal.clear();
        cal.set(Calendar.YEAR, test.womyear);
        cal.set(Calendar.MONTH, test.wommon);
        cal.set(Calendar.WEEK_OF_MONTH, test.wom);
        cal.set(Calendar.DAY_OF_WEEK, test.dow);
        Date ms2 = cal.getTime();
        if (!ms2.equals(ms)) {
            log("\nError: GregorianCalendar.DOM gave " + ms + "\n       GregorianCalendar.WOM gave " + ms2);
            error++;
        } else {
            pass++;
        }
        ms2 = null;
        try {
            ms2 = sdf.parse(test.data);
        } catch (ParseException e) {
            errln("parse exception: " + e);
        }
        if (!ms2.equals(ms)) {
            log("\nError: GregorianCalendar gave      " + ms + "\n       SimpleDateFormat.parse gave " + ms2);
            error++;
        } else {
            pass++;
        }
        String result = sdf.format(ms);
        if (!result.equals(test.normalized)) {
            log("\nWarning: format of '" + test.data + "' gave" + "\n                   '" + result + "'" + "\n          expected '" + test.normalized + "'");
            warning++;
        } else {
            pass++;
        }
        Date ms3 = null;
        try {
            ms3 = sdf.parse(result);
        } catch (ParseException e) {
            errln("parse exception 2: " + e);
        }
        if (!ms3.equals(ms)) {
            error++;
            log("\nError: Re-parse of '" + result + "' gave time of " + "\n        " + ms3 + "\n    not " + ms);
        } else {
            pass++;
        }
    }
    String info = "\nPassed: " + pass + ", Warnings: " + warning + ", Errors: " + error;
    if (error > 0) {
        errln(info);
    } else {
        logln(info);
    }
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 80 with GregorianCalendar

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

the class IBMCalendarTest method TestCoverage.

/**
 * Miscellaneous tests to increase coverage.
 */
@Test
public void TestCoverage() {
    // BuddhistCalendar
    BuddhistCalendar bcal = new BuddhistCalendar();
    /*int i =*/
    bcal.getMinimum(Calendar.ERA);
    bcal.add(Calendar.YEAR, 1);
    bcal.add(Calendar.MONTH, 1);
    /*Date d = */
    bcal.getTime();
    // CalendarAstronomer
    // (This class should probably be made package-private.)
    CalendarAstronomer astro = new CalendarAstronomer();
    /*String s = */
    astro.local(0);
    // ChineseCalendar
    ChineseCalendar ccal = new ChineseCalendar(TimeZone.getDefault(), Locale.getDefault());
    ccal.add(Calendar.MONTH, 1);
    ccal.add(Calendar.YEAR, 1);
    ccal.roll(Calendar.MONTH, 1);
    ccal.roll(Calendar.YEAR, 1);
    ccal.getTime();
    // ICU 2.6
    Calendar cal = Calendar.getInstance(Locale.US);
    logln(cal.toString());
    logln(cal.getDisplayName(Locale.US));
    int weekendOnset = -1;
    int weekendCease = -1;
    for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; ++i) {
        if (cal.getDayOfWeekType(i) == Calendar.WEEKEND_ONSET) {
            weekendOnset = i;
        }
        if (cal.getDayOfWeekType(i) == Calendar.WEEKEND_CEASE) {
            weekendCease = i;
        }
    }
    // but make the call anyway for coverage reasons
    try {
        /*int x=*/
        cal.getWeekendTransition(weekendOnset);
        /*int x=*/
        cal.getWeekendTransition(weekendCease);
    } catch (IllegalArgumentException e) {
    }
    /*int x=*/
    cal.isWeekend(new Date());
    // new GregorianCalendar(ULocale)
    GregorianCalendar gcal = new GregorianCalendar(ULocale.getDefault());
    if (gcal == null) {
        errln("could not create GregorianCalendar with ULocale");
    } else {
        logln("Calendar display name: " + gcal.getDisplayName(ULocale.getDefault()));
    }
    // cover getAvailableULocales
    final ULocale[] locales = Calendar.getAvailableULocales();
    long count = locales.length;
    if (count == 0)
        errln("getAvailableULocales return empty list");
    logln("" + count + " available ulocales in Calendar.");
    // Jitterbug 4451, for coverage
    class StubCalendar extends Calendar {

        /**
         * For serialization
         */
        private static final long serialVersionUID = -4558903444622684759L;

        @Override
        protected int handleGetLimit(int field, int limitType) {
            if (limitType == Calendar.LEAST_MAXIMUM) {
                return 1;
            } else if (limitType == Calendar.GREATEST_MINIMUM) {
                return 7;
            }
            return -1;
        }

        @Override
        protected int handleComputeMonthStart(int eyear, int month, boolean useMonth) {
            if (useMonth) {
                return eyear * 365 + month * 31;
            } else {
                return eyear * 365;
            }
        }

        @Override
        protected int handleGetExtendedYear() {
            return 2017;
        }

        public void run() {
            if (Calendar.gregorianPreviousMonthLength(2000, 2) != 29) {
                errln("Year 2000 Feb should have 29 days.");
            }
            long millis = Calendar.julianDayToMillis(Calendar.MAX_JULIAN);
            if (millis != Calendar.MAX_MILLIS) {
                errln("Did not get the expected value from julianDayToMillis. Got:" + millis);
            }
            DateFormat df = handleGetDateFormat("", Locale.getDefault());
            if (!df.equals(handleGetDateFormat("", ULocale.getDefault()))) {
                errln("Calendar.handleGetDateFormat(String, Locale) should delegate to ( ,ULocale)");
            }
            if (!getType().equals("unknown")) {
                errln("Calendar.getType() should be 'unknown'");
            }
            // Tests for complete coverage of Calendar functions.
            int julianDay = Calendar.millisToJulianDay(millis - 1);
            assertEquals("Julian max day -1", julianDay, Calendar.MAX_JULIAN - 1);
            DateFormat df1 = handleGetDateFormat("GG yyyy-d:MM", "option=xyz", Locale.getDefault());
            if (!df1.equals(handleGetDateFormat("GG yyyy-d:MM", "option=xyz", ULocale.getDefault()))) {
                errln("Calendar.handleGetDateFormat(String, Locale) should delegate to ( ,ULocale)");
            }
            // Prove that the local overrides are used.
            int leastMsInDay = handleGetLimit(Calendar.MILLISECONDS_IN_DAY, Calendar.LEAST_MAXIMUM);
            assertEquals("getLimit test 1", leastMsInDay, 1);
            int maxMsInDay = handleGetLimit(Calendar.WEEK_OF_MONTH, Calendar.GREATEST_MINIMUM);
            assertEquals("getLimit test 2", 7, maxMsInDay);
            int febLeapLength = handleGetMonthLength(2020, Calendar.FEBRUARY);
            assertEquals("handleMonthLength", 31, febLeapLength);
            int exYear = handleGetExtendedYear();
            assertEquals("handleGetExtendeYear", exYear, 2017);
            int monthStart = handleComputeMonthStart(2016, 4, false);
            assertEquals("handleComputeMonthStart false", 735840, monthStart);
            monthStart = handleComputeMonthStart(2016, 4, true);
            assertEquals("handleComputeMonthStart true", 735964, monthStart);
            Calendar cal = Calendar.getInstance();
            cal.set(1980, 5, 2);
            this.setTime(cal.getTime());
            assertEquals("handleComputeFields: year set", 1980, get(YEAR));
            assertEquals("handleComputeFields: month set", 5, get(MONTH));
            assertEquals("handleComputeFields: day set", 2, get(DAY_OF_MONTH));
        }
    }
    StubCalendar stub = new StubCalendar();
    stub.run();
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ULocale(android.icu.util.ULocale) ChineseCalendar(android.icu.util.ChineseCalendar) TaiwanCalendar(android.icu.util.TaiwanCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) BuddhistCalendar(android.icu.util.BuddhistCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) BuddhistCalendar(android.icu.util.BuddhistCalendar) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) CalendarAstronomer(android.icu.impl.CalendarAstronomer) Test(org.junit.Test)

Aggregations

GregorianCalendar (android.icu.util.GregorianCalendar)114 Test (org.junit.Test)102 Date (java.util.Date)64 Calendar (android.icu.util.Calendar)41 SimpleDateFormat (android.icu.text.SimpleDateFormat)34 TimeZone (android.icu.util.TimeZone)29 SimpleTimeZone (android.icu.util.SimpleTimeZone)28 DateFormat (android.icu.text.DateFormat)24 JapaneseCalendar (android.icu.util.JapaneseCalendar)17 ULocale (android.icu.util.ULocale)16 ChineseCalendar (android.icu.util.ChineseCalendar)15 IslamicCalendar (android.icu.util.IslamicCalendar)14 BuddhistCalendar (android.icu.util.BuddhistCalendar)12 ChineseDateFormat (android.icu.text.ChineseDateFormat)8 IOException (java.io.IOException)8 ParsePosition (java.text.ParsePosition)8 CalendarAstronomer (android.icu.impl.CalendarAstronomer)7 HebrewCalendar (android.icu.util.HebrewCalendar)7 FieldPosition (java.text.FieldPosition)7 ParseException (java.text.ParseException)6