Search in sources :

Example 6 with DateIntervalFormat

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

use of android.icu.text.DateIntervalFormat 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 8 with DateIntervalFormat

use of android.icu.text.DateIntervalFormat 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 9 with DateIntervalFormat

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

the class DateIntervalFormatTest method TestFPos_SkelWithSeconds.

@Test
public void TestFPos_SkelWithSeconds() {
    final long[] deltas = { // none
    0L, // 200 millisec
    200L, // 20 sec
    20000L, // 20 min
    1200000L, // 2 hrs
    7200000L, // 12 hrs
    43200000L, // 8 days
    691200000L, // 16 days,
    1382400000L, // 100 days
    8640000000L };
    class ExpectPosAndFormat {

        public int posBegin;

        public int posEnd;

        public String format;

        // Simple constructor
        public ExpectPosAndFormat(int pBegin, int pEnd, String fmt) {
            posBegin = pBegin;
            posEnd = pEnd;
            format = fmt;
        }
    }
    ;
    final ExpectPosAndFormat[] exp_en_HHmm = { new ExpectPosAndFormat(3, 5, "09:00"), new ExpectPosAndFormat(3, 5, "09:00"), new ExpectPosAndFormat(3, 5, "09:00"), new ExpectPosAndFormat(3, 5, "09:00 \u2013 09:20"), new ExpectPosAndFormat(3, 5, "09:00 \u2013 11:00"), new ExpectPosAndFormat(3, 5, "09:00 \u2013 21:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00 \u2013 11/28/2014, 09:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00 \u2013 12/6/2014, 09:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00 \u2013 2/28/2015, 09:00") };
    final ExpectPosAndFormat[] exp_en_HHmmss = { new ExpectPosAndFormat(3, 5, "09:00:00"), new ExpectPosAndFormat(3, 5, "09:00:00"), new ExpectPosAndFormat(3, 5, "09:00:00 \u2013 09:00:20"), new ExpectPosAndFormat(3, 5, "09:00:00 \u2013 09:20:00"), new ExpectPosAndFormat(3, 5, "09:00:00 \u2013 11:00:00"), new ExpectPosAndFormat(3, 5, "09:00:00 \u2013 21:00:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00:00 \u2013 11/28/2014, 09:00:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00:00 \u2013 12/6/2014, 09:00:00"), new ExpectPosAndFormat(15, 17, "11/20/2014, 09:00:00 \u2013 2/28/2015, 09:00:00") };
    final ExpectPosAndFormat[] exp_en_yyMMdd = { new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14"), new ExpectPosAndFormat(0, 0, "11/20/14 \u2013 11/28/14"), new ExpectPosAndFormat(0, 0, "11/20/14 \u2013 12/6/14"), new ExpectPosAndFormat(0, 0, "11/20/14 \u2013 2/28/15") };
    final ExpectPosAndFormat[] exp_en_yyMMddHHmm = { new ExpectPosAndFormat(13, 15, "11/20/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 09:20"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 11:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 21:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 11/28/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 12/06/14, 09:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00 \u2013 02/28/15, 09:00") };
    final ExpectPosAndFormat[] exp_en_yyMMddHHmmss = { new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 09:00:20"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 09:20:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 11:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 21:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 11/28/14, 09:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 12/06/14, 09:00:00"), new ExpectPosAndFormat(13, 15, "11/20/14, 09:00:00 \u2013 02/28/15, 09:00:00") };
    @SuppressWarnings("unused") final ExpectPosAndFormat[] exp_en_yMMMdhmmssz = { new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 9:00:20 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 9:20:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 11:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 9:00:00 PM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 Nov 28, 2014, 9:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 Dec 6, 2014, 9:00:00 AM GMT"), new ExpectPosAndFormat(16, 18, "Nov 20, 2014, 9:00:00 AM GMT \u2013 Feb 28, 2015, 9:00:00 AM GMT") };
    final ExpectPosAndFormat[] exp_ja_yyMMddHHmm = { new ExpectPosAndFormat(11, 13, "14/11/20 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9\u664200\u5206\uFF5E9\u664220\u5206"), new ExpectPosAndFormat(11, 13, "14/11/20 9\u664200\u5206\uFF5E11\u664200\u5206"), new ExpectPosAndFormat(11, 13, "14/11/20 9\u664200\u5206\uFF5E21\u664200\u5206"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00\uFF5E14/11/28 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00\uFF5E14/12/06 9:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00\uFF5E15/02/28 9:00") };
    final ExpectPosAndFormat[] exp_ja_yyMMddHHmmss = { new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E9:00:20"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E9:20:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E11:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E21:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E14/11/28 9:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E14/12/06 9:00:00"), new ExpectPosAndFormat(11, 13, "14/11/20 9:00:00\uFF5E15/02/28 9:00:00") };
    final ExpectPosAndFormat[] exp_ja_yMMMdHHmmss = { new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E9:00:20"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E9:20:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E11:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E21:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E2014\u5E7411\u670828\u65E5 9:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E2014\u5E7412\u67086\u65E5 9:00:00"), new ExpectPosAndFormat(14, 16, "2014\u5E7411\u670820\u65E5 9:00:00\uFF5E2015\u5E742\u670828\u65E5 9:00:00") };
    class LocaleAndSkeletonItem {

        public String locale;

        public String skeleton;

        public int fieldToCheck;

        public ExpectPosAndFormat[] expected;

        // Simple constructor
        public LocaleAndSkeletonItem(String loc, String skel, int field, ExpectPosAndFormat[] exp) {
            locale = loc;
            skeleton = skel;
            fieldToCheck = field;
            expected = exp;
        }
    }
    ;
    final LocaleAndSkeletonItem[] locSkelItems = { new LocaleAndSkeletonItem("en", "HHmm", DateFormat.MINUTE_FIELD, exp_en_HHmm), new LocaleAndSkeletonItem("en", "HHmmss", DateFormat.MINUTE_FIELD, exp_en_HHmmss), new LocaleAndSkeletonItem("en", "yyMMdd", DateFormat.MINUTE_FIELD, exp_en_yyMMdd), new LocaleAndSkeletonItem("en", "yyMMddHHmm", DateFormat.MINUTE_FIELD, exp_en_yyMMddHHmm), new LocaleAndSkeletonItem("en", "yyMMddHHmmss", DateFormat.MINUTE_FIELD, exp_en_yyMMddHHmmss), // new LocaleAndSkeletonItem( "en",		"yMMMdhmmssz",  DateFormat.MINUTE_FIELD, exp_en_yMMMdhmmssz ),
    new LocaleAndSkeletonItem("ja", "yyMMddHHmm", DateFormat.MINUTE_FIELD, exp_ja_yyMMddHHmm), new LocaleAndSkeletonItem("ja", "yyMMddHHmmss", DateFormat.MINUTE_FIELD, exp_ja_yyMMddHHmmss), new LocaleAndSkeletonItem("ja", "yMMMdHHmmss", DateFormat.MINUTE_FIELD, exp_ja_yMMMdHHmmss) };
    // final String zoneGMT = "GMT";
    // 2014 Nov 20 09:00 GMT
    final long startTimeGMT = 1416474000000L;
    TimeZone localZone = TimeZone.getDefault();
    long startTime = startTimeGMT - localZone.getOffset(startTimeGMT);
    for (LocaleAndSkeletonItem item : locSkelItems) {
        DateIntervalFormat difmt = DateIntervalFormat.getInstance(item.skeleton, new ULocale(item.locale));
        int dIdx, dCount = deltas.length;
        for (dIdx = 0; dIdx < dCount; dIdx++) {
            DateInterval di = new DateInterval(startTime, startTime + deltas[dIdx]);
            StringBuffer actual = new StringBuffer(64);
            FieldPosition pos = new FieldPosition(item.fieldToCheck);
            String actualString = difmt.format(di, actual, pos).toString();
            ExpectPosAndFormat expectPosFmt = item.expected[dIdx];
            if (!actualString.equals(expectPosFmt.format) || pos.getBeginIndex() != expectPosFmt.posBegin || pos.getEndIndex() != expectPosFmt.posEnd) {
                errln("For locale " + item.locale + ", skeleton " + item.skeleton + ", delta " + deltas[dIdx] + ": expect " + expectPosFmt.posBegin + "-" + expectPosFmt.posEnd + " \"" + expectPosFmt.format + "\"; get " + pos.getBeginIndex() + "-" + pos.getEndIndex() + " \"" + actualString + "\"");
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) FieldPosition(java.text.FieldPosition) TimeZone(android.icu.util.TimeZone) Test(org.junit.Test)

Example 10 with DateIntervalFormat

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

the class DateIntervalFormatTest method TestDateIntervalFormatCoverage.

@Test
public void TestDateIntervalFormatCoverage() throws Exception {
    long date1 = 1299090600000L;
    long date2 = 1299115800000L;
    DateInterval dtitv = new DateInterval(date1, date2);
    DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("MMMd", Locale.ENGLISH);
    DateIntervalInfo dtintinf = new DateIntervalInfo(ULocale.ENGLISH);
    // Check the default private constructor
    checkDefaultPrivateConstructor(DateIntervalFormat.class);
    // Check clone
    DateIntervalFormat dtitvfmtClone = (DateIntervalFormat) dtitvfmt.clone();
    assertEquals("DateIntervalFormat.clone() failed", dtitvfmt.format(dtitv), dtitvfmtClone.format(dtitv));
    // Coverage for getInstance
    assertNotNull("Expected DateIntervalFormat object", DateIntervalFormat.getInstance("MMMd", dtintinf));
    assertNotNull("Expected DateIntervalFormat object", DateIntervalFormat.getInstance("MMMdHHmm", Locale.ENGLISH, dtintinf));
    // Coverage for parseObject. Exception expected.
    try {
        dtitvfmt.parseObject("", new ParsePosition(0));
        errln("Exception was expected when calling DateIntervalFormat.parseObject()");
    } catch (Exception e) {
    /* No op */
    }
    // Check getPatterns()
    Output<String> secondPart = new Output<String>();
    Calendar fromCalendar = Calendar.getInstance(Locale.ENGLISH);
    fromCalendar.set(2016, 5, 22);
    Calendar toCalendar = Calendar.getInstance(Locale.ENGLISH);
    toCalendar.set(2016, 5, 23);
    assertEquals("Date interval pattern mismatch.", dtitvfmt.getPatterns(fromCalendar, toCalendar, secondPart), "MMM d – ");
    assertEquals("Date interval pattern mismatch.", secondPart.value, "d");
}
Also used : DateIntervalInfo(android.icu.text.DateIntervalInfo) Output(android.icu.util.Output) Calendar(android.icu.util.Calendar) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) ParseException(java.text.ParseException) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Aggregations

DateIntervalFormat (android.icu.text.DateIntervalFormat)14 DateInterval (android.icu.util.DateInterval)11 Test (org.junit.Test)10 DateIntervalInfo (android.icu.text.DateIntervalInfo)9 Calendar (android.icu.util.Calendar)9 FieldPosition (java.text.FieldPosition)8 ULocale (android.icu.util.ULocale)7 SimpleDateFormat (android.icu.text.SimpleDateFormat)5 ParseException (java.text.ParseException)5 Date (java.util.Date)4 TimeZone (android.icu.util.TimeZone)3 DateFormat (android.icu.text.DateFormat)2 PatternInfo (android.icu.text.DateIntervalInfo.PatternInfo)1 Output (android.icu.util.Output)1 ParsePosition (java.text.ParsePosition)1 ArrayList (java.util.ArrayList)1