Search in sources :

Example 71 with DateFormat

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

the class DateFormatTest method TestDotAndAtLeniency.

@Test
public void TestDotAndAtLeniency() {
    for (ULocale locale : Arrays.asList(ULocale.ENGLISH, ULocale.FRENCH)) {
        List<Object[]> tests = new ArrayList();
        for (int dateStyle = DateFormat.FULL; dateStyle <= DateFormat.SHORT; ++dateStyle) {
            DateFormat dateFormat = DateFormat.getDateInstance(dateStyle, locale);
            for (int timeStyle = DateFormat.FULL; timeStyle <= DateFormat.SHORT; ++timeStyle) {
                DateFormat format = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
                DateFormat timeFormat = DateFormat.getTimeInstance(timeStyle, locale);
                String formattedString = format.format(TEST_DATE);
                tests.add(new Object[] { format, formattedString });
                formattedString = dateFormat.format(TEST_DATE) + "  " + timeFormat.format(TEST_DATE);
                tests.add(new Object[] { format, formattedString });
                if (formattedString.contains("n ")) {
                    // will add "." after the end of text ending in 'n', like Jan.
                    tests.add(new Object[] { format, formattedString.replace("n ", "n. ") + "." });
                }
                if (formattedString.contains(". ")) {
                    // will subtract "." at the end of strings.
                    tests.add(new Object[] { format, formattedString.replace(". ", " ") });
                }
            }
        }
        for (Object[] test : tests) {
            DateFormat format = (DateFormat) test[0];
            String formattedString = (String) test[1];
            if (!showParse(format, formattedString)) {
            // showParse(format, formattedString); // for debugging
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 72 with DateFormat

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

the class HebrewTest method Test1624.

@Test
public void Test1624() {
    HebrewCalendar hc = new HebrewCalendar(5742, HebrewCalendar.AV, 22);
    DateFormat df = hc.getDateTimeFormat(DateFormat.FULL, DateFormat.FULL, Locale.getDefault());
    String dateString = df.format(hc.getTime());
    for (int year = 5600; year < 5800; year++) {
        boolean leapYear = HebrewCalendar.isLeapYear(year);
        for (int month = HebrewCalendar.TISHRI; month <= HebrewCalendar.ELUL; month++) {
            // skip the adar 1 month if year is not a leap year
            if (leapYear == false && month == HebrewCalendar.ADAR_1) {
                continue;
            }
            int day = 15;
            hc = new HebrewCalendar(year, month, day);
            dateString = df.format(hc.getTime());
            int dayHC = hc.get(HebrewCalendar.DATE);
            int monthHC = hc.get(HebrewCalendar.MONTH);
            int yearHC = hc.get(HebrewCalendar.YEAR);
            String header = "year:" + year + " isleap:" + leapYear + " " + dateString;
            if (dayHC != day) {
                errln(header + " ==> day:" + dayHC + " incorrect, should be:" + day);
                break;
            }
            if (monthHC != month) {
                errln(header + " ==> month:" + monthHC + " incorrect, should be:" + month);
                break;
            }
            if (yearHC != year) {
                errln(header + " ==> year:" + yearHC + " incorrecte, should be:" + year);
                break;
            }
        }
    }
}
Also used : HebrewCalendar(android.icu.util.HebrewCalendar) DateFormat(android.icu.text.DateFormat) Test(org.junit.Test)

Example 73 with DateFormat

use of android.icu.text.DateFormat 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)

Example 74 with DateFormat

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

the class DateFormatRegressionTest method TestHindiArabicDigits.

@Test
public void TestHindiArabicDigits() {
    String s;
    char first;
    String what;
    {
        DateFormat df = DateFormat.getInstance(new GregorianCalendar(), new ULocale("hi_IN@numbers=deva"));
        what = "Gregorian Calendar, hindi";
        s = df.format(new Date(0));
        /* 31/12/1969 */
        logln(what + "=" + s);
        first = s.charAt(0);
        if (first < kHindiZero || first > (kHindiZero + 9)) {
            errln(what + "- wrong digit,  got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
    {
        DateFormat df = DateFormat.getInstance(new IslamicCalendar(), new Locale("ar", "IQ"));
        s = df.format(new Date(0));
        /* 21/10/1989 */
        what = "Islamic Calendar, Arabic";
        logln(what + ": " + s);
        first = s.charAt(0);
        if (first < kArabicZero || first > (kArabicZero + 9)) {
            errln(what + " wrong digit, got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
    {
        DateFormat df = DateFormat.getInstance(new GregorianCalendar(), new Locale("ar", "IQ"));
        s = df.format(new Date(0));
        /* 31/12/1969 */
        what = "Gregorian,  ar_IQ, df.getInstance";
        logln(what + ": " + s);
        first = s.charAt(0);
        if (first < kArabicZero || first > (kArabicZero + 9)) {
            errln(what + " wrong  digit but got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
    {
        DateFormat df = DateFormat.getInstance(new GregorianCalendar(), new Locale("mt", "MT"));
        s = df.format(new Date(0));
        /* 31/12/1969 */
        what = "Gregorian,  mt_MT, df.getInstance";
        logln(what + ": " + s);
        first = s.charAt(0);
        if (first < kLatinZero || first > (kLatinZero + 9)) {
            errln(what + " wrong  digit but got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
    {
        DateFormat df = DateFormat.getInstance(new IslamicCalendar(), new Locale("ar", "IQ"));
        s = df.format(new Date(0));
        /* 31/12/1969 */
        what = "Islamic calendar, ar_IQ, df.getInstance";
        logln(what + ": " + s);
        first = s.charAt(0);
        if (first < kArabicZero || first > (kArabicZero + 9)) {
            errln(what + " wrong  digit but got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
    {
        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, new Locale("ar", "IQ"));
        s = df.format(new Date(0));
        /* 31/12/1969 */
        what = "ar_IQ, getDateTimeInstance";
        logln(what + ": " + s);
        first = s.charAt(0);
        if (first < kArabicZero || first > (kArabicZero + 9)) {
            errln(what + " wrong  digit but got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
    {
        DateFormat df = DateFormat.getInstance(new JapaneseCalendar(), new Locale("ar", "IQ"));
        s = df.format(new Date(0));
        /* 31/12/1969 */
        what = "ar_IQ, Japanese Calendar, getInstance";
        logln(what + ": " + s);
        first = s.charAt(0);
        if (first < kArabicZero || first > (kArabicZero + 9)) {
            errln(what + " wrong  digit but got " + s + " (integer digit value " + new Integer((int) first).toString());
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) JapaneseCalendar(android.icu.util.JapaneseCalendar) IslamicCalendar(android.icu.util.IslamicCalendar) Test(org.junit.Test)

Example 75 with DateFormat

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

the class DateFormatRegressionTest method Test4210209.

/**
 * j32 {JDK Bug 4210209 4209272}
 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
 */
@Test
public void Test4210209() {
    String pattern = "MMM d, yyyy";
    DateFormat fmt = new SimpleDateFormat(pattern, Locale.US);
    DateFormat disp = new SimpleDateFormat("MMM dd yyyy GG", Locale.US);
    Calendar calx = fmt.getCalendar();
    calx.setLenient(false);
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(2000, Calendar.FEBRUARY, 29);
    Date d = calendar.getTime();
    String s = fmt.format(d);
    logln(disp.format(d) + " f> " + pattern + " => \"" + s + "\"");
    ParsePosition pos = new ParsePosition(0);
    d = fmt.parse(s, pos);
    logln("\"" + s + "\" p> " + pattern + " => " + (d != null ? disp.format(d) : "null"));
    logln("Parse pos = " + pos.getIndex() + ", error pos = " + pos.getErrorIndex());
    if (pos.getErrorIndex() != -1) {
        errln("FAIL: Error index should be -1");
    }
    // The underlying bug is in GregorianCalendar.  If the following lines
    // succeed, the bug is fixed.  If the bug isn't fixed, they will throw
    // an exception.
    GregorianCalendar cal = new GregorianCalendar();
    cal.clear();
    cal.setLenient(false);
    // This should work!
    cal.set(2000, Calendar.FEBRUARY, 29);
    d = cal.getTime();
    logln("Attempt to set Calendar to Feb 29 2000: " + disp.format(d));
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Aggregations

DateFormat (android.icu.text.DateFormat)97 SimpleDateFormat (android.icu.text.SimpleDateFormat)80 Test (org.junit.Test)78 Date (java.util.Date)67 GregorianCalendar (android.icu.util.GregorianCalendar)44 ChineseDateFormat (android.icu.text.ChineseDateFormat)37 Calendar (android.icu.util.Calendar)37 ULocale (android.icu.util.ULocale)33 JapaneseCalendar (android.icu.util.JapaneseCalendar)27 IslamicCalendar (android.icu.util.IslamicCalendar)23 ChineseCalendar (android.icu.util.ChineseCalendar)22 ParseException (java.text.ParseException)21 BuddhistCalendar (android.icu.util.BuddhistCalendar)19 TimeZone (android.icu.util.TimeZone)19 HebrewCalendar (android.icu.util.HebrewCalendar)17 Locale (java.util.Locale)17 ParsePosition (java.text.ParsePosition)15 FieldPosition (java.text.FieldPosition)13 IOException (java.io.IOException)10 SimpleTimeZone (android.icu.util.SimpleTimeZone)7