Search in sources :

Example 41 with SimpleDateFormat

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

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

the class DateIntervalFormatTest method TestTicket11583.

@Test
public void TestTicket11583() {
    ULocale[] locales = { ULocale.ENGLISH, SPANISH, LA_SPANISH };
    String[] skeletons = { "yMMMMd", "yMMMM", "MMMM", "yMMMd", "yMMM", "MMM", "yMMd", "yMMdd", "yMM", "MM", "yMdd", "yMd", "yM", "M" };
    final long startDate = 1232364615000L;
    final long endDate = 1240399815000L;
    // "yMMM";
    String filterPattern = null;
    for (ULocale locale : locales) {
        for (String skeleton : skeletons) {
            if (filterPattern != null && !skeleton.equals(filterPattern)) {
                continue;
            }
            DateFormat dateFormat = DateFormat.getPatternInstance(skeleton, locale);
            String dateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern();
            DateIntervalFormat intervalFormat = DateIntervalFormat.getInstance(skeleton, locale);
            DateIntervalInfo intervalInfo = intervalFormat.getDateIntervalInfo();
            if (skeleton.equals(filterPattern)) {
                logln(filterPattern + " => " + intervalInfo.getRawPatterns().get(filterPattern));
            }
            DateInterval date_interval = new DateInterval(startDate, endDate);
            String interval = intervalFormat.format(date_interval);
            String formattedStart = dateFormat.format(startDate);
            String formattedEnd = dateFormat.format(endDate);
            PatternInfo patternInfo = intervalFormat.getRawPatterns().get("M");
            String firstPart = patternInfo.getFirstPart();
            String secondPart = patternInfo.getSecondPart();
            if (!matches(dateFormatPattern, firstPart, secondPart)) {
                if (logKnownIssue("11585", "incompatible pattern between date format and date interval format")) {
                    logln("For skeleton " + skeleton + "/locale " + locale + ": mismatch between date format «" + dateFormatPattern + "» and date interval format «" + firstPart + secondPart + "».");
                } else {
                    errln("For skeleton " + skeleton + "/locale " + locale + ": mismatch between date format «" + dateFormatPattern + "» and date interval format «" + firstPart + secondPart + "».");
                }
            }
            logln(locale + "\tskeleton: «" + skeleton + "»\tpattern: «" + dateFormatPattern + "»\tintervalPattern1: «" + firstPart + "»\tintervalPattern2: «" + secondPart + "»\tstartDate: «" + formattedStart + "»\tendDate: «" + formattedEnd + "»\tinterval: «" + interval + "»");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DateIntervalInfo(android.icu.text.DateIntervalInfo) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) PatternInfo(android.icu.text.DateIntervalInfo.PatternInfo) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 43 with SimpleDateFormat

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

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

the class DateTimeGeneratorTest method TestSimple.

@Test
public void TestSimple() {
    // some simple use cases
    ULocale locale = ULocale.GERMANY;
    TimeZone zone = TimeZone.getTimeZone("Europe/Paris");
    // make from locale
    DateTimePatternGenerator gen = DateTimePatternGenerator.getInstance(locale);
    SimpleDateFormat format = new SimpleDateFormat(gen.getBestPattern("MMMddHmm"), locale);
    format.setTimeZone(zone);
    assertEquals("simple format: MMMddHmm", "14. Okt., 08:58", format.format(sampleDate));
    // (a generator can be built from scratch, but that is not a typical use case)
    // modify the generator by adding patterns
    DateTimePatternGenerator.PatternInfo returnInfo = new DateTimePatternGenerator.PatternInfo();
    gen.addPattern("d'. von' MMMM", true, returnInfo);
    // the returnInfo is mostly useful for debugging problem cases
    format.applyPattern(gen.getBestPattern("MMMMdHmm"));
    assertEquals("modified format: MMMdHmm", "14. von Oktober, 08:58", format.format(sampleDate));
    // get a pattern and modify it
    format = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
    format.setTimeZone(zone);
    String pattern = format.toPattern();
    assertEquals("full-date", "Donnerstag, 14. Oktober 1999 um 08:58:59 Mitteleurop\u00E4ische Sommerzeit", format.format(sampleDate));
    // modify it to change the zone.
    String newPattern = gen.replaceFieldTypes(pattern, "vvvv");
    format.applyPattern(newPattern);
    assertEquals("full-date: modified zone", "Donnerstag, 14. Oktober 1999 um 08:58:59 Mitteleurop\u00E4ische Zeit", format.format(sampleDate));
    // add test of basic cases
    // lang  YYYYMMM MMMd    MMMdhmm hmm hhmm    Full Date-Time
    // en  Mar 2007    Mar 4   6:05 PM Mar 4   6:05 PM 06:05 PM    Sunday, March 4, 2007 6:05:05 PM PT
    DateTimePatternGenerator enGen = DateTimePatternGenerator.getInstance(ULocale.ENGLISH);
    TimeZone enZone = TimeZone.getTimeZone("Etc/GMT");
    SimpleDateFormat enFormat = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, ULocale.ENGLISH);
    enFormat.setTimeZone(enZone);
    String[][] tests = { { "yyyyMMMdd", "Oct 14, 1999" }, { "yyyyqqqq", "4th quarter 1999" }, { "yMMMdd", "Oct 14, 1999" }, { "EyyyyMMMdd", "Thu, Oct 14, 1999" }, { "yyyyMMdd", "10/14/1999" }, { "yyyyMMM", "Oct 1999" }, { "yyyyMM", "10/1999" }, { "yyMM", "10/99" }, // narrow format
    { "yMMMMMd", "O 14, 1999" }, // narrow format
    { "EEEEEMMMMMd", "T, O 14" }, { "MMMd", "Oct 14" }, { "MMMdhmm", "Oct 14, 6:58 AM" }, { "EMMMdhmms", "Thu, Oct 14, 6:58:59 AM" }, { "MMdhmm", "10/14, 6:58 AM" }, { "EEEEMMMdhmms", "Thursday, Oct 14, 6:58:59 AM" }, // (fixed expected result per ticket 6872<-7180)
    { "yyyyMMMddhhmmss", "Oct 14, 1999, 6:58:59 AM" }, // (fixed expected result per ticket 6872<-7180)
    { "EyyyyMMMddhhmmss", "Thu, Oct 14, 1999, 6:58:59 AM" }, { "hmm", "6:58 AM" }, // (fixed expected result per ticket 6872<-7180)
    { "hhmm", "6:58 AM" }, // (fixed expected result per ticket 6872<-7180)
    { "hhmmVVVV", "6:58 AM GMT" } };
    for (int i = 0; i < tests.length; ++i) {
        final String testSkeleton = tests[i][0];
        String pat = enGen.getBestPattern(testSkeleton);
        enFormat.applyPattern(pat);
        String formattedDate = enFormat.format(sampleDate);
        assertEquals("Testing skeleton '" + testSkeleton + "' with  " + sampleDate, tests[i][1], formattedDate);
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 45 with SimpleDateFormat

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

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