Search in sources :

Example 96 with Calendar

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

the class DateIntervalFormatTest method TestTicket9914.

@Test
public void TestTicket9914() {
    DateIntervalInfo dateIntervalInfo = new DateIntervalInfo(ULocale.ENGLISH);
    Calendar from = Calendar.getInstance();
    Calendar to = Calendar.getInstance();
    from.set(113, 3, 26);
    to.set(113, 3, 28);
    DateIntervalFormat dif = DateIntervalFormat.getInstance("yyyyMd", ULocale.ENGLISH, dateIntervalInfo);
    assertEquals("yyyyMd skeleton.", "4/26/0113 \u2013 4/28/0113", dif.format(from, to, new StringBuffer(), new FieldPosition(0)).toString());
    dif = DateIntervalFormat.getInstance("yyMd", ULocale.ENGLISH, dateIntervalInfo);
    assertEquals("yyMd skeleton.", "4/26/13 \u2013 4/28/13", dif.format(from, to, new StringBuffer(), new FieldPosition(0)).toString());
    dif = DateIntervalFormat.getInstance("yMd", ULocale.ENGLISH, dateIntervalInfo);
    assertEquals("yMd skeleton.", "4/26/113 \u2013 4/28/113", dif.format(from, to, new StringBuffer(), new FieldPosition(0)).toString());
}
Also used : DateIntervalInfo(android.icu.text.DateIntervalInfo) Calendar(android.icu.util.Calendar) DateIntervalFormat(android.icu.text.DateIntervalFormat) FieldPosition(java.text.FieldPosition) Test(org.junit.Test)

Example 97 with Calendar

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

the class DateIntervalFormatTest method expectUserCLDR.

private void expectUserCLDR(String[] data, int data_length) {
    int i = 1;
    while (i < data_length) {
        String locName = data[i++];
        ULocale loc = new ULocale(locName);
        SimpleDateFormat ref = new SimpleDateFormat(data[0], loc);
        // 'f'
        String datestr = data[i++];
        String datestr_2 = data[i++];
        Date date, date_2;
        try {
            date = ref.parse(datestr);
            date_2 = ref.parse(datestr_2);
        } catch (ParseException e) {
            errln("parse exception" + e);
            continue;
        }
        DateInterval dtitv = new DateInterval(date.getTime(), date_2.getTime());
        DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("yyyyMMMdd", loc);
        // DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("yMd");
        // SimpleDateFormat dtfmt = new SimpleDateFormat("yyyy 'year' MMM 'month' dd 'day'", loc);
        // dtitvfmt.setDateFormat(dtfmt);
        DateIntervalInfo dtitvinf = new DateIntervalInfo();
        dtitvinf.setFallbackIntervalPattern("{0} --- {1}");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.YEAR, "'all diff'");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.MONTH, "yyyy 'diff' MMM d - MMM y");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.DATE, "yyyy MMM d ~ d");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.HOUR_OF_DAY, "yyyy MMMd HH:mm ~ HH:mm");
        dtitvfmt.setDateIntervalInfo(dtitvinf);
        FieldPosition pos = new FieldPosition(0);
        StringBuffer str = new StringBuffer("");
        DateFormat dtfmt = dtitvfmt.getDateFormat();
        Calendar fromCalendar = (Calendar) dtfmt.getCalendar().clone();
        Calendar toCalendar = (Calendar) dtfmt.getCalendar().clone();
        fromCalendar.setTimeInMillis(dtitv.getFromDate());
        toCalendar.setTimeInMillis(dtitv.getToDate());
        dtitvfmt.format(fromCalendar, toCalendar, str, pos);
        String expected = data[i++];
        String formatted = dtitvfmt.format(dtitv).toString();
        if (!formatted.equals(Utility.unescape(expected))) {
            errln("CLDR: \"" + locName + "\\" + datestr + "\\" + datestr_2 + "\"\t expected: " + expected + "\tgot: " + formatted + "\n");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DateIntervalInfo(android.icu.text.DateIntervalInfo) Calendar(android.icu.util.Calendar) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) FieldPosition(java.text.FieldPosition) Date(java.util.Date) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat)

Example 98 with Calendar

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

the class DateTimeGeneratorTest method TestReplacingZoneString.

@Test
public void TestReplacingZoneString() {
    Date testDate = new Date();
    TimeZone testTimeZone = TimeZone.getTimeZone("America/New_York");
    TimeZone bogusTimeZone = new SimpleTimeZone(1234, "Etc/Unknown");
    Calendar calendar = Calendar.getInstance();
    ParsePosition parsePosition = new ParsePosition(0);
    ULocale[] locales = ULocale.getAvailableLocales();
    int count = 0;
    for (int i = 0; i < locales.length; ++i) {
        // skip the country locales unless we are doing exhaustive tests
        if (getExhaustiveness() < 6) {
            if (locales[i].getCountry().length() > 0) {
                continue;
            }
        }
        count++;
        // ticket#6503
        if (getExhaustiveness() <= 5 && count % 3 != 0) {
            continue;
        }
        logln(locales[i].toString());
        DateTimePatternGenerator dtpgen = DateTimePatternGenerator.getInstance(locales[i]);
        for (int style1 = DateFormat.FULL; style1 <= DateFormat.SHORT; ++style1) {
            final SimpleDateFormat oldFormat = (SimpleDateFormat) DateFormat.getTimeInstance(style1, locales[i]);
            String pattern = oldFormat.toPattern();
            // replaceZoneString(pattern, "VVVV");
            String newPattern = dtpgen.replaceFieldTypes(pattern, "VVVV");
            if (newPattern.equals(pattern)) {
                continue;
            }
            // verify that it roundtrips parsing
            SimpleDateFormat newFormat = new SimpleDateFormat(newPattern, locales[i]);
            newFormat.setTimeZone(testTimeZone);
            String formatted = newFormat.format(testDate);
            calendar.setTimeZone(bogusTimeZone);
            parsePosition.setIndex(0);
            newFormat.parse(formatted, calendar, parsePosition);
            if (parsePosition.getErrorIndex() >= 0) {
                errln("Failed parse with VVVV:\t" + locales[i] + ",\t\"" + pattern + "\",\t\"" + newPattern + "\",\t\"" + formatted.substring(0, parsePosition.getErrorIndex()) + "{}" + formatted.substring(parsePosition.getErrorIndex()) + "\"");
            } else if (!calendar.getTimeZone().getID().equals(testTimeZone.getID())) {
                errln("Failed timezone roundtrip with VVVV:\t" + locales[i] + ",\t\"" + pattern + "\",\t\"" + newPattern + "\",\t\"" + formatted + "\",\t" + calendar.getTimeZone().getID() + " != " + testTimeZone.getID());
            } else {
                logln(locales[i] + ":\t\"" + pattern + "\" => \t\"" + newPattern + "\"\t" + formatted);
            }
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 99 with Calendar

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

the class DateFormatTest method TestFormatsWithNumberSystems.

@Test
public void TestFormatsWithNumberSystems() {
    TimeZone zone = TimeZone.getFrozenTimeZone("UTC");
    // for UTC: grego 31-Dec-2015 10 AM, hebrew 19 tevet 5776, chinese yi-wei 11mo 21day
    long date = 1451556000000L;
    class TestFmtWithNumSysItem {

        public String localeID;

        public int style;

        public String expectPattern;

        public String expectFormat;

        // Simple constructor
        public TestFmtWithNumSysItem(String loc, int styl, String pat, String exp) {
            localeID = loc;
            style = styl;
            expectPattern = pat;
            expectFormat = exp;
        }
    }
    ;
    final TestFmtWithNumSysItem[] items = { new TestFmtWithNumSysItem("haw@calendar=gregorian", DateFormat.SHORT, "d/M/yy", "31/xii/15"), new TestFmtWithNumSysItem("he@calendar=hebrew", DateFormat.LONG, "d \u05D1MMMM y", "\u05D9\u05F4\u05D8 \u05D1\u05D8\u05D1\u05EA \u05EA\u05E9\u05E2\u05F4\u05D5"), // "2015乙未年十一月廿一"
    new TestFmtWithNumSysItem("zh@calendar=chinese", DateFormat.LONG, "rU\u5E74MMMd", "2015\u4E59\u672A\u5E74\u5341\u4E00\u6708\u5EFF\u4E00"), // "2015乙未年冬月廿一"
    new TestFmtWithNumSysItem("zh_Hant@calendar=chinese", DateFormat.LONG, "rU\u5E74MMMd", "2015\u4E59\u672A\u5E74\u51AC\u6708\u5EFF\u4E00"), // "乙未年十一月二一日"
    new TestFmtWithNumSysItem("ja@calendar=chinese", DateFormat.LONG, "U\u5E74MMMd\u65E5", "\u4E59\u672A\u5E74\u5341\u4E00\u6708\u4E8C\u4E00\u65E5") };
    for (TestFmtWithNumSysItem item : items) {
        ULocale locale = new ULocale(item.localeID);
        Calendar cal = Calendar.getInstance(zone, locale);
        cal.setTimeInMillis(date);
        SimpleDateFormat sdfmt = (SimpleDateFormat) DateFormat.getDateInstance(item.style, locale);
        StringBuffer getFormat = new StringBuffer();
        FieldPosition fp = new FieldPosition(0);
        sdfmt.format(cal, getFormat, fp);
        if (getFormat.toString().compareTo(item.expectFormat) != 0) {
            errln("FAIL: date format for locale " + item.localeID + ", expected \"" + item.expectFormat + "\", got \"" + getFormat.toString() + "\"");
        }
        String getPattern = sdfmt.toPattern();
        if (getPattern.compareTo(item.expectPattern) != 0) {
            errln("FAIL: date pattern for locale " + item.localeID + ", expected \"" + item.expectPattern + "\", got \"" + getPattern + "\"");
        }
    }
}
Also used : TimeZone(android.icu.util.TimeZone) 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) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 100 with Calendar

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

the class DateFormatTest method TestDayOfYearPattern195.

/**
 * Test the day of year pattern.
 */
@Test
public void TestDayOfYearPattern195() {
    Calendar cal = Calendar.getInstance();
    Date today = cal.getTime();
    int year, month, day;
    year = cal.get(Calendar.YEAR);
    month = cal.get(Calendar.MONTH);
    day = cal.get(Calendar.DAY_OF_MONTH);
    cal.clear();
    cal.set(year, month, day);
    Date expected = cal.getTime();
    logln("Test Date: " + today);
    SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateInstance();
    tryPattern(sdf, today, null, expected);
    tryPattern(sdf, today, "G yyyy DDD", expected);
}
Also used : 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) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) 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