Search in sources :

Example 11 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project robolectric by robolectric.

the class ShadowDateIntervalFormatTest method testDateInterval_FormatDateRange.

@Test
public void testDateInterval_FormatDateRange() throws ParseException {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, 2013);
    calendar.set(Calendar.MONTH, Calendar.JANUARY);
    calendar.set(Calendar.DAY_OF_MONTH, 20);
    long timeInMillis = calendar.getTimeInMillis();
    String actual = DateIntervalFormat.formatDateRange(ULocale.getDefault(), TimeZone.getDefault(), timeInMillis, timeInMillis, DateUtils.FORMAT_NUMERIC_DATE);
    DateFormat format = new SimpleDateFormat("MM/dd/yyyy", ULocale.getDefault());
    Date date = format.parse(actual);
    assertThat(date).isNotNull();
}
Also used : Calendar(java.util.Calendar) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 12 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat 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 13 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat 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 14 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project j2objc by google.

the class CalendarRegressionTest method Test4125881.

/**
 * Check isLeapYear for BC years.
 */
@Test
public void Test4125881() {
    GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
    DateFormat fmt = new SimpleDateFormat("MMMM d, yyyy G");
    cal.clear();
    for (int y = -20; y <= 10; ++y) {
        cal.set(Calendar.ERA, y < 1 ? GregorianCalendar.BC : GregorianCalendar.AD);
        cal.set(Calendar.YEAR, y < 1 ? 1 - y : y);
        logln(y + " = " + fmt.format(cal.getTime()) + " " + cal.isLeapYear(y));
        if (cal.isLeapYear(y) != ((y + 40) % 4 == 0))
            errln("Leap years broken");
    }
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 15 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project j2objc by google.

the class CalendarRegressionTest method Test4103271.

@Test
public void Test4103271() {
    SimpleDateFormat sdf = new SimpleDateFormat();
    int numYears = 40, startYear = 1997, numDays = 15;
    String output, testDesc;
    GregorianCalendar testCal = (GregorianCalendar) Calendar.getInstance();
    testCal.clear();
    sdf.setCalendar(testCal);
    sdf.applyPattern("d MMM yyyy");
    boolean fail = false;
    for (int firstDay = 1; firstDay <= 2; firstDay++) {
        for (int minDays = 1; minDays <= 7; minDays++) {
            testCal.setMinimalDaysInFirstWeek(minDays);
            testCal.setFirstDayOfWeek(firstDay);
            testDesc = ("Test" + String.valueOf(firstDay) + String.valueOf(minDays));
            logln(testDesc + " => 1st day of week=" + String.valueOf(firstDay) + ", minimum days in first week=" + String.valueOf(minDays));
            for (int j = startYear; j <= startYear + numYears; j++) {
                testCal.set(j, 11, 25);
                for (int i = 0; i < numDays; i++) {
                    testCal.add(Calendar.DATE, 1);
                    String calWOY;
                    int actWOY = testCal.get(Calendar.WEEK_OF_YEAR);
                    if (actWOY < 1 || actWOY > 53) {
                        Date d = testCal.getTime();
                        calWOY = String.valueOf(actWOY);
                        output = testDesc + " - " + sdf.format(d) + "\t";
                        output = output + "\t" + calWOY;
                        logln(output);
                        fail = true;
                    }
                }
            }
        }
    }
    int[] DATA = { 3, 52, 52, 52, 52, 52, 52, 52, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 4, 52, 52, 52, 52, 52, 52, 52, 53, 53, 53, 53, 53, 53, 53, 1, 1, 1, 1, 1, 1, 1 };
    testCal.setFirstDayOfWeek(Calendar.SUNDAY);
    for (int j = 0; j < DATA.length; j += 22) {
        logln("Minimal days in first week = " + DATA[j] + "  Week starts on Sunday");
        testCal.setMinimalDaysInFirstWeek(DATA[j]);
        testCal.set(1997, Calendar.DECEMBER, 21);
        for (int i = 0; i < 21; ++i) {
            int woy = testCal.get(Calendar.WEEK_OF_YEAR);
            log(testCal.getTime() + " " + woy);
            if (woy != DATA[j + 1 + i]) {
                log(" ERROR");
                fail = true;
            }
            // logln();
            // Now compute the time from the fields, and make sure we
            // get the same answer back. This is a round-trip test.
            Date save = testCal.getTime();
            testCal.clear();
            testCal.set(Calendar.YEAR, DATA[j + 1 + i] < 25 ? 1998 : 1997);
            testCal.set(Calendar.WEEK_OF_YEAR, DATA[j + 1 + i]);
            testCal.set(Calendar.DAY_OF_WEEK, (i % 7) + Calendar.SUNDAY);
            if (!testCal.getTime().equals(save)) {
                logln("  Parse failed: " + testCal.getTime());
                fail = true;
            }
            testCal.setTime(save);
            testCal.add(Calendar.DAY_OF_MONTH, 1);
        }
    }
    Date[] d = new Date[8];
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(1997, Calendar.DECEMBER, 28);
    d[0] = tempcal.getTime();
    tempcal.set(1998, Calendar.JANUARY, 10);
    d[1] = tempcal.getTime();
    tempcal.set(1998, Calendar.DECEMBER, 31);
    d[2] = tempcal.getTime();
    tempcal.set(1999, Calendar.JANUARY, 1);
    d[3] = tempcal.getTime();
    // Test field disambiguation with a few special hard-coded cases.
    // This shouldn't fail if the above cases aren't failing.
    Object[] DISAM = { new Integer(1998), new Integer(1), new Integer(Calendar.SUNDAY), d[0], new Integer(1998), new Integer(2), new Integer(Calendar.SATURDAY), d[1], new Integer(1998), new Integer(53), new Integer(Calendar.THURSDAY), d[2], new Integer(1998), new Integer(53), new Integer(Calendar.FRIDAY), d[3] };
    testCal.setMinimalDaysInFirstWeek(3);
    testCal.setFirstDayOfWeek(Calendar.SUNDAY);
    for (int i = 0; i < DISAM.length; i += 4) {
        int y = ((Integer) DISAM[i]).intValue();
        int woy = ((Integer) DISAM[i + 1]).intValue();
        int dow = ((Integer) DISAM[i + 2]).intValue();
        Date exp = (Date) DISAM[i + 3];
        testCal.clear();
        testCal.set(Calendar.YEAR, y);
        testCal.set(Calendar.WEEK_OF_YEAR, woy);
        testCal.set(Calendar.DAY_OF_WEEK, dow);
        log(y + "-W" + woy + "-DOW" + dow + " expect:" + exp + " got:" + testCal.getTime());
        if (!testCal.getTime().equals(exp)) {
            log("  FAIL");
            fail = true;
        }
    // logln();
    }
    // Now try adding and rolling
    Object ADD = new Object();
    Object ROLL = new Object();
    tempcal.set(1998, Calendar.DECEMBER, 25);
    d[0] = tempcal.getTime();
    tempcal.set(1999, Calendar.JANUARY, 1);
    d[1] = tempcal.getTime();
    tempcal.set(1997, Calendar.DECEMBER, 28);
    d[2] = tempcal.getTime();
    tempcal.set(1998, Calendar.JANUARY, 4);
    d[3] = tempcal.getTime();
    tempcal.set(1998, Calendar.DECEMBER, 27);
    d[4] = tempcal.getTime();
    tempcal.set(1997, Calendar.DECEMBER, 28);
    d[5] = tempcal.getTime();
    tempcal.set(1999, Calendar.JANUARY, 2);
    d[6] = tempcal.getTime();
    tempcal.set(1998, Calendar.JANUARY, 3);
    d[7] = tempcal.getTime();
    Object[] ADDROLL = { ADD, new Integer(1), d[0], d[1], ADD, new Integer(1), d[2], d[3], ROLL, new Integer(1), d[4], d[5], ROLL, new Integer(1), d[6], d[7] };
    testCal.setMinimalDaysInFirstWeek(3);
    testCal.setFirstDayOfWeek(Calendar.SUNDAY);
    for (int i = 0; i < ADDROLL.length; i += 4) {
        int amount = ((Integer) ADDROLL[i + 1]).intValue();
        Date before = (Date) ADDROLL[i + 2];
        Date after = (Date) ADDROLL[i + 3];
        testCal.setTime(before);
        if (ADDROLL[i] == ADD)
            testCal.add(Calendar.WEEK_OF_YEAR, amount);
        else
            testCal.roll(Calendar.WEEK_OF_YEAR, amount);
        log((ADDROLL[i] == ADD ? "add(WOY," : "roll(WOY,") + amount + ") " + before + " => " + testCal.getTime());
        if (!after.equals(testCal.getTime())) {
            logln("  exp:" + after + "  FAIL");
            fail = true;
        } else
            logln(" ok");
        testCal.setTime(after);
        if (ADDROLL[i] == ADD)
            testCal.add(Calendar.WEEK_OF_YEAR, -amount);
        else
            testCal.roll(Calendar.WEEK_OF_YEAR, -amount);
        log((ADDROLL[i] == ADD ? "add(WOY," : "roll(WOY,") + (-amount) + ") " + after + " => " + testCal.getTime());
        if (!before.equals(testCal.getTime())) {
            logln("  exp:" + before + "  FAIL");
            fail = true;
        } else
            logln(" ok");
    }
    if (fail)
        errln("Fail: Week of year misbehaving");
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Aggregations

SimpleDateFormat (android.icu.text.SimpleDateFormat)153 Test (org.junit.Test)113 Date (java.util.Date)103 GregorianCalendar (android.icu.util.GregorianCalendar)59 Calendar (android.icu.util.Calendar)53 ParseException (java.text.ParseException)42 DateFormat (android.icu.text.DateFormat)41 JapaneseCalendar (android.icu.util.JapaneseCalendar)41 ULocale (android.icu.util.ULocale)40 IslamicCalendar (android.icu.util.IslamicCalendar)37 ParsePosition (java.text.ParsePosition)36 FieldPosition (java.text.FieldPosition)29 ChineseCalendar (android.icu.util.ChineseCalendar)27 TimeZone (android.icu.util.TimeZone)27 BuddhistCalendar (android.icu.util.BuddhistCalendar)25 ChineseDateFormat (android.icu.text.ChineseDateFormat)21 HebrewCalendar (android.icu.util.HebrewCalendar)21 IOException (java.io.IOException)19 SimpleTimeZone (android.icu.util.SimpleTimeZone)16 Locale (java.util.Locale)14