Search in sources :

Example 71 with Calendar

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

the class DangiTest method TestOutOfRange.

/**
 * Test the behavior of fields that are out of range.
 */
@Test
public void TestOutOfRange() {
    int[] DATA = new int[] { // Input       Output
    4334, 13, 1, 4335, 1, 1, 4334, 18, 1, 4335, 6, 1, 4335, 0, 1, 4334, 12, 1, 4335, -6, 1, 4334, 6, 1, // 1-4334 has 30 days
    4334, // 1-4334 has 30 days
    1, // 1-4334 has 30 days
    32, // 1-4334 has 30 days
    4334, // 1-4334 has 30 days
    2, // 1-4334 has 30 days
    2, 4334, 2, -1, 4334, 1, 29 };
    Calendar cal = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi"));
    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 : ULocale(android.icu.util.ULocale) DangiCalendar(android.icu.util.DangiCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Test(org.junit.Test)

Example 72 with Calendar

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

the class DangiTest method TestResolution.

/**
 * Make sure IS_LEAP_MONTH participates in field resolution.
 */
@Test
public void TestResolution() {
    Calendar cal = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi"));
    DateFormat fmt = DateFormat.getDateInstance(cal, DateFormat.DEFAULT);
    // May 22 4334 = y4334 m4 d30 doy119
    // May 23 4334 = y4334 m4* d1 doy120
    final int THE_YEAR = 4334;
    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 };
    StringBuilder buf = new StringBuilder();
    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 : ULocale(android.icu.util.ULocale) DangiCalendar(android.icu.util.DangiCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) DateFormat(android.icu.text.DateFormat) Test(org.junit.Test)

Example 73 with Calendar

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

the class DangiTest method TestRoll.

/**
 * Test the behavior of KoreanLunarCalendar.roll().  The only real
 * nastiness with roll is the MONTH field around leap months.
 */
@Test
public void TestRoll() {
    int[][] tests = new int[][] { // normal
    { 4338, 3, 0, 15, MONTH, 3, 4338, 6, 0, 15 }, // normal
    { 4338, 3, 0, 15, MONTH, 11, 4338, 2, 0, 15 }, // across year
    { 4335, 12, 0, 15, MONTH, 1, 4335, 1, 0, 15 }, // across year
    { 4336, 1, 0, 15, MONTH, -1, 4336, 12, 0, 15 }, // 4=leap
    { 4334, 3, 0, 15, MONTH, 3, 4334, 5, 0, 15 }, // 4=leap
    { 4334, 3, 0, 15, MONTH, 16, 4334, 5, 0, 15 }, // 4=leap
    { 4334, 3, 0, 15, MONTH, 2, 4334, 4, 1, 15 }, // 4=leap
    { 4334, 3, 0, 15, MONTH, 28, 4334, 4, 1, 15 }, // 4=leap
    { 4334, 4, 0, 15, MONTH, 1, 4334, 4, 1, 15 }, // 4=leap
    { 4334, 4, 0, 15, MONTH, -12, 4334, 4, 1, 15 }, // 4=leap
    { 4334, 4, 1, 15, MONTH, 1, 4334, 5, 0, 15 }, // 4=leap
    { 4334, 4, 1, 15, MONTH, -25, 4334, 5, 0, 15 }, // dom should pin
    { 4334, 3, 0, 30, MONTH, 2, 4334, 4, 1, 29 }, // dom should pin
    { 4334, 3, 0, 30, MONTH, 15, 4334, 4, 1, 29 }, // no dom pin
    { 4334, 3, 0, 30, MONTH, 16, 4334, 5, 0, 30 }, // dom should pin
    { 4334, 3, 0, 30, MONTH, -9, 4334, 6, 0, 29 } };
    Calendar cal = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi"));
    doRollAddDangi(ROLL, cal, tests);
}
Also used : ULocale(android.icu.util.ULocale) DangiCalendar(android.icu.util.DangiCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) Test(org.junit.Test)

Example 74 with Calendar

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

the class DateFormatRegressionTest method Test4182066.

/**
 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" . 1999).
 */
@Test
public void Test4182066() {
    SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy", Locale.US);
    SimpleDateFormat dispFmt = new SimpleDateFormat("MMM dd yyyy HH:mm:ss GG", Locale.US);
    /* We expect 2-digit year formats to put 2-digit years in the right
         * window.  Out of range years, that is, anything less than "00" or
         * greater than "99", are treated as literal years.  So "1/2/3456"
         * becomes 3456 AD.  Likewise, "1/2/-3" becomes -3 AD == 2 BC.
         */
    final String[] STRINGS = { "02/29/00", "01/23/01", "04/05/-1", "01/23/-9", "11/12/1314", "10/31/1", "09/12/+1", "09/12/001" };
    int STRINGS_COUNT = STRINGS.length;
    Calendar cal = Calendar.getInstance();
    Date FAIL_DATE = cal.getTime();
    cal.clear();
    cal.set(2000, Calendar.FEBRUARY, 29);
    Date d0 = cal.getTime();
    cal.clear();
    cal.set(2001, Calendar.JANUARY, 23);
    Date d1 = cal.getTime();
    cal.clear();
    cal.set(-1, Calendar.APRIL, 5);
    Date d2 = cal.getTime();
    cal.clear();
    cal.set(-9, Calendar.JANUARY, 23);
    Date d3 = cal.getTime();
    cal.clear();
    cal.set(1314, Calendar.NOVEMBER, 12);
    Date d4 = cal.getTime();
    cal.clear();
    cal.set(1, Calendar.OCTOBER, 31);
    Date d5 = cal.getTime();
    cal.clear();
    cal.set(1, Calendar.SEPTEMBER, 12);
    Date d7 = cal.getTime();
    Date[] DATES = { d0, d1, d2, d3, d4, d5, FAIL_DATE, d7 };
    String out = "";
    boolean pass = true;
    for (int i = 0; i < STRINGS_COUNT; ++i) {
        String str = STRINGS[i];
        Date expected = DATES[i];
        Date actual = null;
        try {
            actual = fmt.parse(str);
        } catch (ParseException e) {
            actual = FAIL_DATE;
        }
        String actStr = "";
        if ((actual.getTime()) == FAIL_DATE.getTime()) {
            actStr += "null";
        } else {
            // Yuck: See j25
            actStr = ((DateFormat) dispFmt).format(actual);
        }
        if (expected.getTime() == (actual.getTime())) {
            out += str + " => " + actStr + "\n";
        } else {
            String expStr = "";
            if (expected.getTime() == FAIL_DATE.getTime()) {
                expStr += "null";
            } else {
                // Yuck: See j25
                expStr = ((DateFormat) dispFmt).format(expected);
            }
            out += "FAIL: " + str + " => " + actStr + ", expected " + expStr + "\n";
            pass = false;
        }
    }
    if (pass) {
        log(out);
    } else {
        err(out);
    }
}
Also used : IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 75 with Calendar

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

the class DataDrivenFormatTest method testConvertDate.

private void testConvertDate(TestDataModule.TestData testData, DataMap settings, boolean fmt) {
    DateFormat basicFmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
    int n = 0;
    for (Iterator iter = testData.getDataIterator(); iter.hasNext(); ) {
        ++n;
        long now = System.currentTimeMillis();
        DataMap currentCase = (DataMap) iter.next();
        String caseString = "[" + testData.getName() + "#" + n + (fmt ? "format" : "parse") + "]";
        String locale = currentCase.getString("locale");
        String zone = currentCase.getString("zone");
        String spec = currentCase.getString("spec");
        String date = currentCase.getString("date");
        String str = currentCase.getString("str");
        Date fromDate = null;
        boolean useDate = false;
        ULocale loc = new ULocale(locale);
        String pattern = null;
        // boolean usePattern = false;
        DateFormat format = null;
        DateTimeStyleSet styleSet;
        CalendarFieldsSet fromSet = null;
        // parse 'spec'  - either 'PATTERN=yy mm dd' or 'DATE=x,TIME=y'
        if (spec.startsWith(kPATTERN)) {
            pattern = spec.substring(kPATTERN.length());
            // usePattern = true;
            format = new SimpleDateFormat(pattern, loc);
        } else {
            styleSet = new DateTimeStyleSet();
            styleSet.parseFrom(spec);
            format = DateFormat.getDateTimeInstance(styleSet.getDateStyle(), styleSet.getTimeStyle(), loc);
        }
        Calendar cal = Calendar.getInstance(loc);
        if (zone.length() > 0) {
            TimeZone tz = TimeZone.getFrozenTimeZone(zone);
            cal.setTimeZone(tz);
            format.setTimeZone(tz);
        }
        // parse 'date' - either 'MILLIS=12345' or  a CalendarFieldsSet
        if (date.startsWith(kMILLIS)) {
            useDate = true;
            fromDate = new Date(Long.parseLong(date.substring(kMILLIS.length())));
        } else if (date.startsWith(kRELATIVE_MILLIS)) {
            useDate = true;
            fromDate = new Date(now + Long.parseLong(date.substring(kRELATIVE_MILLIS.length())));
        } else if (date.startsWith(kRELATIVE_ADD)) {
            // "add" is a string indicating which fields to add
            String add = date.substring(kRELATIVE_ADD.length());
            CalendarFieldsSet addSet = new CalendarFieldsSet();
            addSet.parseFrom(add);
            useDate = true;
            cal.clear();
            cal.setTimeInMillis(now);
            // / perform op on 'to calendar'
            for (int q = 0; q < addSet.fieldCount(); q++) {
                if (addSet.isSet(q)) {
                    if (q == Calendar.DATE) {
                        cal.add(q, addSet.get(q));
                    } else {
                        cal.set(q, addSet.get(q));
                    }
                }
            }
            fromDate = cal.getTime();
        } else {
            fromSet = new CalendarFieldsSet();
            fromSet.parseFrom(date);
        }
        // run the test
        if (fmt) {
            StringBuffer output = new StringBuffer();
            cal.clear();
            FieldPosition pos = new FieldPosition(0);
            if (useDate) {
                output = format.format(fromDate, output, pos);
            } else {
                fromSet.setOnCalendar(cal);
                format.format(cal, output, pos);
            }
            if (output.toString().equals(str)) {
                logln(caseString + " Success - strings match: " + output);
            } else {
                errln(caseString + " FAIL: got " + output + " expected " + str);
            }
        } else {
            // parse
            cal.clear();
            ParsePosition pos = new ParsePosition(0);
            format.parse(str, cal, pos);
            if (useDate) {
                Date gotDate = cal.getTime();
                if (gotDate.equals(fromDate)) {
                    logln(caseString + " SUCCESS: got=parse=" + str);
                } else {
                    errln(caseString + " FAIL: parsed " + str + " but got " + basicFmt.format(gotDate) + " - " + gotDate + "  expected " + basicFmt.format(fromDate));
                }
            } else {
                CalendarFieldsSet diffSet = new CalendarFieldsSet();
                if (!fromSet.matches(cal, diffSet)) {
                    String diffs = diffSet.diffFrom(fromSet);
                    errln(caseString + " FAIL:  differences: " + diffs);
                } else {
                    logln(caseString + " SUCCESS: got=parse: " + str + " - " + fromSet.toString());
                }
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Calendar(android.icu.util.Calendar) CalendarFieldsSet(android.icu.dev.test.util.CalendarFieldsSet) FieldPosition(java.text.FieldPosition) Date(java.util.Date) DataMap(android.icu.dev.test.TestDataModule.DataMap) TimeZone(android.icu.util.TimeZone) DateTimeStyleSet(android.icu.dev.test.util.DateTimeStyleSet) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Iterator(java.util.Iterator) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition)

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