Search in sources :

Example 36 with Calendar

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

the class IBMCalendarTest method TestWeekend.

/**
 * Test weekend support in IBMCalendar.
 *
 * NOTE: This test will have to be updated when the isWeekend() etc.
 *       API is finalized later.
 *
 *       In particular, the test will have to be rewritten to instantiate
 *       a Calendar in the given locale (using getInstance()) and call
 *       that Calendar's isWeekend() etc. methods.
 */
@Test
public void TestWeekend() {
    SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy G HH:mm:ss.SSS");
    // NOTE
    // This test tests for specific locale data.  This is probably okay
    // as far as US data is concerned, but if the Arabic/Yemen data
    // changes, this test will have to be updated.
    // Test specific days
    Object[] DATA1 = { Locale.US, new int[] { // Fri 23:00
    2000, // Fri 23:00
    Calendar.MARCH, // Fri 23:00
    17, // Fri 23:00
    23, // Fri 23:00
    0, // Fri 23:00
    0, // Fri 23:59:59.999
    2000, // Fri 23:59:59.999
    Calendar.MARCH, // Fri 23:59:59.999
    18, // Fri 23:59:59.999
    0, // Fri 23:59:59.999
    -1, // Fri 23:59:59.999
    0, // Sat 00:00
    2000, // Sat 00:00
    Calendar.MARCH, // Sat 00:00
    18, // Sat 00:00
    0, // Sat 00:00
    0, // Sat 00:00
    1, // Sat 15:00
    2000, // Sat 15:00
    Calendar.MARCH, // Sat 15:00
    18, // Sat 15:00
    15, // Sat 15:00
    0, // Sat 15:00
    1, // Sun 23:00
    2000, // Sun 23:00
    Calendar.MARCH, // Sun 23:00
    19, // Sun 23:00
    23, // Sun 23:00
    0, // Sun 23:00
    1, // Sun 23:59:59.999
    2000, // Sun 23:59:59.999
    Calendar.MARCH, // Sun 23:59:59.999
    20, // Sun 23:59:59.999
    0, // Sun 23:59:59.999
    -1, // Sun 23:59:59.999
    1, // Mon 00:00
    2000, // Mon 00:00
    Calendar.MARCH, // Mon 00:00
    20, // Mon 00:00
    0, // Mon 00:00
    0, // Mon 00:00
    0, // Mon 08:00
    2000, // Mon 08:00
    Calendar.MARCH, // Mon 08:00
    20, // Mon 08:00
    8, // Mon 08:00
    0, // Mon 08:00
    0 }, new Locale("ar", "OM"), new int[] { // Wed 23:00
    2000, // Wed 23:00
    Calendar.MARCH, // Wed 23:00
    15, // Wed 23:00
    23, // Wed 23:00
    0, // Wed 23:00
    0, // Wed 23:59:59.999
    2000, // Wed 23:59:59.999
    Calendar.MARCH, // Wed 23:59:59.999
    16, // Wed 23:59:59.999
    0, // Wed 23:59:59.999
    -1, // Wed 23:59:59.999
    0, // Thu 00:00
    2000, // Thu 00:00
    Calendar.MARCH, // Thu 00:00
    16, // Thu 00:00
    0, // Thu 00:00
    0, // Thu 00:00
    0, // Thu 15:00
    2000, // Thu 15:00
    Calendar.MARCH, // Thu 15:00
    16, // Thu 15:00
    15, // Thu 15:00
    0, // Thu 15:00
    0, // Fri 23:00
    2000, // Fri 23:00
    Calendar.MARCH, // Fri 23:00
    17, // Fri 23:00
    23, // Fri 23:00
    0, // Fri 23:00
    1, // Fri 23:59:59.999
    2000, // Fri 23:59:59.999
    Calendar.MARCH, // Fri 23:59:59.999
    18, // Fri 23:59:59.999
    0, // Fri 23:59:59.999
    -1, // Fri 23:59:59.999
    1, // Sat 00:00
    2000, // Sat 00:00
    Calendar.MARCH, // Sat 00:00
    18, // Sat 00:00
    0, // Sat 00:00
    0, // Sat 00:00
    1, // Sat 08:00
    2000, // Sat 08:00
    Calendar.MARCH, // Sat 08:00
    18, // Sat 08:00
    8, // Sat 08:00
    0, // Sat 08:00
    1 } };
    // Test days of the week
    Object[] DATA2 = { Locale.US, new int[] { Calendar.MONDAY, Calendar.WEEKDAY, Calendar.FRIDAY, Calendar.WEEKDAY, Calendar.SATURDAY, Calendar.WEEKEND, Calendar.SUNDAY, Calendar.WEEKEND }, new Locale("ar", "OM"), new int[] { // Friday:Saturday
    Calendar.WEDNESDAY, Calendar.WEEKDAY, Calendar.THURSDAY, Calendar.WEEKDAY, Calendar.FRIDAY, Calendar.WEEKEND, Calendar.SATURDAY, Calendar.WEEKEND }, new Locale("hi", "IN"), new int[] { // Sunday only
    Calendar.MONDAY, Calendar.WEEKDAY, Calendar.FRIDAY, Calendar.WEEKDAY, Calendar.SATURDAY, Calendar.WEEKDAY, Calendar.SUNDAY, Calendar.WEEKEND } };
    for (int i1 = 0; i1 < DATA1.length; i1 += 2) {
        Locale loc = (Locale) DATA1[i1];
        int[] data = (int[]) DATA1[i1 + 1];
        Calendar cal = Calendar.getInstance(loc);
        logln("Locale: " + loc);
        for (int i = 0; i < data.length; i += 6) {
            cal.clear();
            cal.set(data[i], data[i + 1], data[i + 2], data[i + 3], 0, 0);
            if (data[i + 4] != 0) {
                cal.setTime(new Date(cal.getTime().getTime() + data[i + 4]));
            }
            boolean isWeekend = cal.isWeekend();
            boolean ok = isWeekend == (data[i + 5] != 0);
            if (ok) {
                logln("Ok:   " + fmt.format(cal.getTime()) + " isWeekend=" + isWeekend);
            } else {
                errln("FAIL: " + fmt.format(cal.getTime()) + " isWeekend=" + isWeekend + ", expected=" + (!isWeekend));
            }
        }
    }
    for (int i2 = 0; i2 < DATA2.length; i2 += 2) {
        Locale loc = (Locale) DATA2[i2];
        int[] data = (int[]) DATA2[i2 + 1];
        logln("Locale: " + loc);
        Calendar cal = Calendar.getInstance(loc);
        for (int i = 0; i < data.length; i += 2) {
            int type = cal.getDayOfWeekType(data[i]);
            int exp = data[i + 1];
            if (type == exp) {
                logln("Ok:   DOW " + data[i] + " type=" + type);
            } else {
                errln("FAIL: DOW " + data[i] + " type=" + type + ", expected=" + exp);
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) 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) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 37 with Calendar

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

the class IBMCalendarTest method TestSimpleDateFormatCoverage.

public void TestSimpleDateFormatCoverage() {
    class StubSimpleDateFormat extends SimpleDateFormat {

        private static final long serialVersionUID = 1L;

        public StubSimpleDateFormat(String pattern, Locale loc) {
            new SimpleDateFormat(pattern, loc);
        }

        public void run() {
            Calendar cal = Calendar.getInstance(Locale.US);
            cal.clear();
            // Sat 15:00
            cal.set(2000, Calendar.MARCH, 18, 15, 0, 1);
            DateFormatSymbols theseSymbols = this.getSymbols();
            String shouldBeMonday = theseSymbols.getWeekdays()[Calendar.MONDAY];
            assertEquals("Should be Monday", "Monday", shouldBeMonday);
            String[] matchData = { "16", "2016", "2016AD", "Monday", "lunes" };
            int matchIndex = matchString("Monday March 28, 2016", 0, Calendar.DAY_OF_WEEK, matchData, cal);
            // Position of the pointer after the matched string.
            assertEquals("matchData for Monday", 6, matchIndex);
            matchIndex = matchString("Monday March 28, 2016 AD", 17, Calendar.YEAR, matchData, cal);
            // Position of the pointer after the matched string.
            assertEquals("matchData for 2016", 21, matchIndex);
            char ch = 'y';
            int count = 4;
            int beginOffset = 0;
            // Reset this
            cal.set(Calendar.YEAR, 2000);
            assertEquals("calendar year reset", 2000, cal.get(Calendar.YEAR));
            FieldPosition pos = new FieldPosition(java.text.DateFormat.YEAR_FIELD);
            String subFormatResult = subFormat(ch, count, beginOffset, pos, theseSymbols, cal);
            assertEquals("subFormat result", "2000", subFormatResult);
            String testParseString = "some text with a date 2017-03-15";
            int start = 22;
            boolean obeyCount = true;
            boolean allowNegative = false;
            boolean[] ambiguousYear = { true, false, true };
            int subParseResult = subParse(testParseString, start, ch, count, obeyCount, allowNegative, ambiguousYear, cal);
            assertEquals("subParseResult result", 26, subParseResult);
            assertEquals("parsed year", 2017, cal.get(Calendar.YEAR));
        }
    }
    StubSimpleDateFormat stub = new StubSimpleDateFormat("EEE MMM dd yyyy G HH:mm:ss.SSS", Locale.US);
    stub.run();
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) 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) DateFormatSymbols(android.icu.text.DateFormatSymbols) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat)

Example 38 with Calendar

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

the class IBMCalendarTest method TestSkippedWallTime.

@Test
public void TestSkippedWallTime() {
    final Object[][] TESTDATA = { // Time zone            Input wall time                     Valid wall time?
    { "America/New_York", new CalFields(2011, 3, 13, 1, 59, 59), true, // WALLTIME_LAST in GMT                WALLTIME_FIRST in GMT           WALLTIME_NEXT_VALID in GMT
    new CalFields(2011, 3, 13, 6, 59, 59), new CalFields(2011, 3, 13, 6, 59, 59), new CalFields(2011, 3, 13, 6, 59, 59) }, { "America/New_York", new CalFields(2011, 3, 13, 2, 0, 0), false, new CalFields(2011, 3, 13, 7, 0, 0), new CalFields(2011, 3, 13, 6, 0, 0), new CalFields(2011, 3, 13, 7, 0, 0) }, { "America/New_York", new CalFields(2011, 3, 13, 2, 1, 0), false, new CalFields(2011, 3, 13, 7, 1, 0), new CalFields(2011, 3, 13, 6, 1, 0), new CalFields(2011, 3, 13, 7, 0, 0) }, { "America/New_York", new CalFields(2011, 3, 13, 2, 30, 0), false, new CalFields(2011, 3, 13, 7, 30, 0), new CalFields(2011, 3, 13, 6, 30, 0), new CalFields(2011, 3, 13, 7, 0, 0) }, { "America/New_York", new CalFields(2011, 3, 13, 2, 59, 59), false, new CalFields(2011, 3, 13, 7, 59, 59), new CalFields(2011, 3, 13, 6, 59, 59), new CalFields(2011, 3, 13, 7, 0, 0) }, { "America/New_York", new CalFields(2011, 3, 13, 3, 0, 0), true, new CalFields(2011, 3, 13, 7, 0, 0), new CalFields(2011, 3, 13, 7, 0, 0), new CalFields(2011, 3, 13, 7, 0, 0) }, { "Pacific/Apia", new CalFields(2011, 12, 29, 23, 59, 59), true, new CalFields(2011, 12, 30, 9, 59, 59), new CalFields(2011, 12, 30, 9, 59, 59), new CalFields(2011, 12, 30, 9, 59, 59) }, { "Pacific/Apia", new CalFields(2011, 12, 30, 0, 0, 0), false, new CalFields(2011, 12, 30, 10, 0, 0), new CalFields(2011, 12, 29, 10, 0, 0), new CalFields(2011, 12, 30, 10, 0, 0) }, { "Pacific/Apia", new CalFields(2011, 12, 30, 12, 0, 0), false, new CalFields(2011, 12, 30, 22, 0, 0), new CalFields(2011, 12, 29, 22, 0, 0), new CalFields(2011, 12, 30, 10, 0, 0) }, { "Pacific/Apia", new CalFields(2011, 12, 30, 23, 59, 59), false, new CalFields(2011, 12, 31, 9, 59, 59), new CalFields(2011, 12, 30, 9, 59, 59), new CalFields(2011, 12, 30, 10, 0, 0) }, { "Pacific/Apia", new CalFields(2011, 12, 31, 0, 0, 0), true, new CalFields(2011, 12, 30, 10, 0, 0), new CalFields(2011, 12, 30, 10, 0, 0), new CalFields(2011, 12, 30, 10, 0, 0) } };
    Calendar calGMT = Calendar.getInstance(TimeZone.GMT_ZONE);
    Calendar calDefault = Calendar.getInstance();
    Calendar calLast = Calendar.getInstance();
    Calendar calFirst = Calendar.getInstance();
    Calendar calNextAvail = Calendar.getInstance();
    calLast.setSkippedWallTimeOption(Calendar.WALLTIME_LAST);
    calFirst.setSkippedWallTimeOption(Calendar.WALLTIME_FIRST);
    calNextAvail.setSkippedWallTimeOption(Calendar.WALLTIME_NEXT_VALID);
    for (Object[] test : TESTDATA) {
        String tzid = (String) test[0];
        TimeZone tz = TimeZone.getTimeZone(tzid);
        CalFields in = (CalFields) test[1];
        boolean isValid = (Boolean) test[2];
        CalFields expLastGMT = (CalFields) test[3];
        CalFields expFirstGMT = (CalFields) test[4];
        CalFields expNextAvailGMT = (CalFields) test[5];
        for (int i = 0; i < 2; i++) {
            boolean bLenient = (i == 0);
            // WALLTIME_LAST
            calLast.setLenient(bLenient);
            calLast.setTimeZone(tz);
            try {
                in.setTo(calLast);
                calGMT.setTimeInMillis(calLast.getTimeInMillis());
                CalFields outLastGMT = CalFields.createFrom(calGMT);
                if (!bLenient && !isValid) {
                    errln("Fail: IllegalArgumentException expected - " + in + "[" + tzid + "] (WALLTIME_LAST)");
                } else if (!outLastGMT.equals(expLastGMT)) {
                    errln("Fail: WALLTIME_LAST " + in + "[" + tzid + "] is parsed as " + outLastGMT + "[GMT]. Expected: " + expLastGMT + "[GMT]");
                }
            } catch (IllegalArgumentException e) {
                if (bLenient || isValid) {
                    errln("Fail: Unexpected IllegalArgumentException - " + in + "[" + tzid + "] (WALLTIME_LAST)");
                }
            }
            // default
            calDefault.setLenient(bLenient);
            calDefault.setTimeZone(tz);
            try {
                in.setTo(calDefault);
                calGMT.setTimeInMillis(calDefault.getTimeInMillis());
                CalFields outDefGMT = CalFields.createFrom(calGMT);
                if (!bLenient && !isValid) {
                    errln("Fail: IllegalArgumentException expected - " + in + "[" + tzid + "] (default)");
                } else if (!outDefGMT.equals(expLastGMT)) {
                    errln("Fail: (default) " + in + "[" + tzid + "] is parsed as " + outDefGMT + "[GMT]. Expected: " + expLastGMT + "[GMT]");
                }
            } catch (IllegalArgumentException e) {
                if (bLenient || isValid) {
                    errln("Fail: Unexpected IllegalArgumentException - " + in + "[" + tzid + "] (default)");
                }
            }
            // WALLTIME_FIRST
            calFirst.setLenient(bLenient);
            calFirst.setTimeZone(tz);
            try {
                in.setTo(calFirst);
                calGMT.setTimeInMillis(calFirst.getTimeInMillis());
                CalFields outFirstGMT = CalFields.createFrom(calGMT);
                if (!bLenient && !isValid) {
                    errln("Fail: IllegalArgumentException expected - " + in + "[" + tzid + "] (WALLTIME_FIRST)");
                } else if (!outFirstGMT.equals(expFirstGMT)) {
                    errln("Fail: WALLTIME_FIRST " + in + "[" + tzid + "] is parsed as " + outFirstGMT + "[GMT]. Expected: " + expFirstGMT + "[GMT]");
                }
            } catch (IllegalArgumentException e) {
                if (bLenient || isValid) {
                    errln("Fail: Unexpected IllegalArgumentException - " + in + "[" + tzid + "] (WALLTIME_FIRST)");
                }
            }
            // WALLTIME_NEXT_VALID
            calNextAvail.setLenient(bLenient);
            calNextAvail.setTimeZone(tz);
            try {
                in.setTo(calNextAvail);
                calGMT.setTimeInMillis(calNextAvail.getTimeInMillis());
                CalFields outNextAvailGMT = CalFields.createFrom(calGMT);
                if (!bLenient && !isValid) {
                    errln("Fail: IllegalArgumentException expected - " + in + "[" + tzid + "] (WALLTIME_NEXT_VALID)");
                } else if (!outNextAvailGMT.equals(expNextAvailGMT)) {
                    errln("Fail: WALLTIME_NEXT_VALID " + in + "[" + tzid + "] is parsed as " + outNextAvailGMT + "[GMT]. Expected: " + expNextAvailGMT + "[GMT]");
                }
            } catch (IllegalArgumentException e) {
                if (bLenient || isValid) {
                    errln("Fail: Unexpected IllegalArgumentException - " + in + "[" + tzid + "] (WALLTIME_NEXT_VALID)");
                }
            }
        }
    }
}
Also used : TimeZone(android.icu.util.TimeZone) 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) Test(org.junit.Test)

Example 39 with Calendar

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

the class IBMCalendarTest method TestTaiwanLimits.

/**
 * Test limits of the Taiwan calendar.
 */
@Test
public void TestTaiwanLimits() {
    // Final parameter is either number of days, if > 0, or test
    // duration in seconds, if < 0.
    Calendar cal = Calendar.getInstance();
    cal.set(2007, Calendar.JANUARY, 1);
    TaiwanCalendar taiwan = new TaiwanCalendar();
    doLimitsTest(taiwan, null, cal.getTime());
    doTheoreticalLimitsTest(taiwan, 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) TaiwanCalendar(android.icu.util.TaiwanCalendar) Test(org.junit.Test)

Example 40 with Calendar

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

the class IBMCalendarTest method TestBuddhistLimits.

/**
 * Test limits of the Buddhist calendar.
 */
@Test
public void TestBuddhistLimits() {
    // Final parameter is either number of days, if > 0, or test
    // duration in seconds, if < 0.
    Calendar cal = Calendar.getInstance();
    cal.set(2007, Calendar.JANUARY, 1);
    BuddhistCalendar buddhist = new BuddhistCalendar();
    doLimitsTest(buddhist, null, cal.getTime());
    doTheoreticalLimitsTest(buddhist, false);
}
Also used : BuddhistCalendar(android.icu.util.BuddhistCalendar) 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) Test(org.junit.Test)

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