Search in sources :

Example 66 with DateFormat

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

the class DateFormatTest method TestDateFormatZone061.

/**
 * Test the formatting of time zones.
 */
@Test
public void TestDateFormatZone061() {
    Date date;
    DateFormat formatter;
    date = new Date(859248000000L);
    logln("Date 1997/3/25 00:00 GMT: " + date);
    formatter = new SimpleDateFormat("dd-MMM-yyyyy HH:mm", Locale.UK);
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    String temp = formatter.format(date);
    logln("Formatted in GMT to: " + temp);
    try {
        Date tempDate = formatter.parse(temp);
        logln("Parsed to: " + tempDate);
        if (!tempDate.equals(date))
            errln("FAIL: Expected " + date + " Got: " + tempDate);
    } catch (Throwable t) {
        System.out.println(t);
    }
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 67 with DateFormat

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

the class DateFormatTest method TestFieldPosition.

/**
 * Verify that returned field position indices are correct.
 */
@Test
public void TestFieldPosition() {
    int i, j, exp;
    StringBuffer buf = new StringBuffer();
    // Verify data
    if (VersionInfo.ICU_VERSION.compareTo(VersionInfo.getInstance(3, 7)) >= 0) {
        DateFormatSymbols rootSyms = new DateFormatSymbols(new Locale("", "", ""));
        assertEquals("patternChars", PATTERN_CHARS, rootSyms.getLocalPatternChars());
    }
    assertTrue("DATEFORMAT_FIELD_NAMES", DATEFORMAT_FIELD_NAMES.length == DateFormat.FIELD_COUNT);
    if (DateFormat.FIELD_COUNT != PATTERN_CHARS.length() + 1) {
        // +1 for missing TIME_SEPARATOR pattern char
        errln("Did not get the correct value for DateFormat.FIELD_COUNT. Expected:  " + PATTERN_CHARS.length() + 1);
    }
    // Create test formatters
    final int COUNT = 4;
    DateFormat[] dateFormats = new DateFormat[COUNT];
    dateFormats[0] = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);
    dateFormats[1] = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.FRANCE);
    // Make the pattern "G y M d..."
    buf.append(PATTERN_CHARS);
    for (j = buf.length() - 1; j >= 0; --j) buf.insert(j, ' ');
    dateFormats[2] = new SimpleDateFormat(buf.toString(), Locale.US);
    // Make the pattern "GGGG yyyy MMMM dddd..."
    for (j = buf.length() - 1; j >= 0; j -= 2) {
        for (i = 0; i < 3; ++i) {
            buf.insert(j, buf.charAt(j));
        }
    }
    dateFormats[3] = new SimpleDateFormat(buf.toString(), Locale.US);
    Date aug13 = new Date((long) 871508052513.0);
    // Expected output field values for above DateFormats on aug13
    // Fields are given in order of DateFormat field number
    final String[] EXPECTED = { "", "1997", "August", "13", "", "", "34", "12", "", "Wednesday", "", "", "", "", "PM", "2", "", "Pacific Daylight Time", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "1997", "ao\u00FBt", "13", "", "14", "34", "12", "", "mercredi", "", "", "", "", "", "", "", "heure d\u2019\u00E9t\u00E9 du Pacifique", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "AD", "1997", "8", "13", "14", "14", "34", "12", "5", "Wed", "225", "2", "33", "3", "PM", "2", "2", "PDT", "1997", "4", "1997", "2450674", "52452513", "-0700", "PT", "4", "8", "3", "3", "uslax", "1997", "GMT-7", "-07", "-07", "1997", "PM", "in the afternoon", "", "Anno Domini", "1997", "August", "0013", "0014", "0014", "0034", "0012", "5130", "Wednesday", "0225", "0002", "0033", "0003", "PM", "0002", "0002", "Pacific Daylight Time", "1997", "Wednesday", "1997", "2450674", "52452513", "GMT-07:00", "Pacific Time", "Wednesday", "August", "3rd quarter", "3rd quarter", "Los Angeles Time", "1997", "GMT-07:00", "-0700", "-0700", "1997", "PM", "in the afternoon", "" };
    assertTrue("data size", EXPECTED.length == COUNT * DateFormat.FIELD_COUNT);
    final DateFormat.Field[] DTFMT_FIELDS = { DateFormat.Field.AM_PM, DateFormat.Field.DAY_OF_MONTH, DateFormat.Field.DAY_OF_WEEK, DateFormat.Field.DAY_OF_WEEK_IN_MONTH, DateFormat.Field.DAY_OF_YEAR, DateFormat.Field.DOW_LOCAL, DateFormat.Field.ERA, DateFormat.Field.EXTENDED_YEAR, DateFormat.Field.HOUR_OF_DAY0, DateFormat.Field.HOUR_OF_DAY1, DateFormat.Field.HOUR0, DateFormat.Field.HOUR1, DateFormat.Field.JULIAN_DAY, DateFormat.Field.MILLISECOND, DateFormat.Field.MILLISECONDS_IN_DAY, DateFormat.Field.MINUTE, DateFormat.Field.MONTH, DateFormat.Field.QUARTER, DateFormat.Field.SECOND, DateFormat.Field.TIME_ZONE, DateFormat.Field.WEEK_OF_MONTH, DateFormat.Field.WEEK_OF_YEAR, DateFormat.Field.YEAR, DateFormat.Field.YEAR_WOY };
    final String[][] EXPECTED_BY_FIELD = { { "PM", "13", "Wednesday", "", "", "", "", "", "", "", "", "2", "", "", "", "34", "August", "", "12", "Pacific Daylight Time", "", "", "1997", "" }, { "", "13", "mercredi", "", "", "", "", "", "14", "", "", "", "", "", "", "34", "ao\u00FBt", "", "12", "heure d\u2019\u00E9t\u00E9 du Pacifique", "", "", "1997", "" }, { "PM", "13", "Wed", "2", "225", "4", "AD", "1997", "14", "14", "2", "2", "2450674", "5", "52452513", "34", "8", "3", "12", "PDT", "3", "33", "1997", "1997" }, { "PM", "0013", "Wednesday", "0002", "0225", "Wednesday", "Anno Domini", "1997", "0014", "0014", "0002", "0002", "2450674", "5130", "52452513", "0034", "August", "3rd quarter", "0012", "Pacific Daylight Time", "0003", "0033", "1997", "1997" } };
    TimeZone PT = TimeZone.getTimeZone("America/Los_Angeles");
    for (j = 0, exp = 0; j < COUNT; ++j) {
        // String str;
        DateFormat df = dateFormats[j];
        df.setTimeZone(PT);
        logln(" Pattern = " + ((SimpleDateFormat) df).toPattern());
        try {
            logln("  Result = " + df.format(aug13));
        } catch (Exception e) {
            errln("FAIL: " + e);
            e.printStackTrace();
            continue;
        }
        FieldPosition pos;
        String field;
        for (i = 0; i < DateFormat.FIELD_COUNT; ++i, ++exp) {
            pos = new FieldPosition(i);
            buf.setLength(0);
            df.format(aug13, buf, pos);
            field = buf.substring(pos.getBeginIndex(), pos.getEndIndex());
            assertEquals("pattern#" + j + " field #" + i + " " + DATEFORMAT_FIELD_NAMES[i], EXPECTED[exp], field);
        }
        // FieldPostion initialized by DateFormat.Field trac#6089
        for (i = 0; i < DTFMT_FIELDS.length; i++) {
            // The format method only set position for the first occurrence of
            // the specified field.
            pos = new FieldPosition(DTFMT_FIELDS[i]);
            buf.setLength(0);
            df.format(aug13, buf, pos);
            field = buf.substring(pos.getBeginIndex(), pos.getEndIndex());
            assertEquals("pattern#" + j + " " + DTFMT_FIELDS[i].toString(), EXPECTED_BY_FIELD[j][i], field);
        }
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) FieldPosition(java.text.FieldPosition) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException) Field(android.icu.text.ChineseDateFormat.Field) TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormatSymbols(android.icu.text.DateFormatSymbols) ChineseDateFormatSymbols(android.icu.text.ChineseDateFormatSymbols) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 68 with DateFormat

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

the class DateFormatTest method TestFormattingLocaleTimeSeparator.

@Test
public void TestFormattingLocaleTimeSeparator() {
    // Sun Sep 14 21:52:00 CET 1997
    Date date = new Date(874266720000L);
    TimeZone tz = TimeZone.getTimeZone("CET");
    DateFormat dfArab = DateFormat.getTimeInstance(DateFormat.SHORT, new ULocale("ar"));
    DateFormat dfLatn = DateFormat.getTimeInstance(DateFormat.SHORT, new ULocale("ar-u-nu-latn"));
    dfArab.setTimeZone(tz);
    dfLatn.setTimeZone(tz);
    String expectedArab = "\u0669:\u0665\u0662 \u0645";
    String expectedLatn = "9:52 \u0645";
    String actualArab = dfArab.format(date);
    String actualLatn = dfLatn.format(date);
    if (!actualArab.equals(expectedArab)) {
        errln("FAIL: Expected " + expectedArab + " Got " + actualArab);
    }
    if (!actualLatn.equals(expectedLatn)) {
        errln("FAIL: Expected " + expectedLatn + " Got " + actualLatn);
    }
}
Also used : TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 69 with DateFormat

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

the class DateFormatTest method TestBadInput135a.

/**
 * Verify the correct behavior when parsing an array of inputs against an
 * array of patterns, with known results.  The results are encoded after
 * the input strings in each row.
 */
@Test
public void TestBadInput135a() {
    SimpleDateFormat dateParse = new SimpleDateFormat("", Locale.US);
    final String ss;
    Date date;
    String[] parseFormats = { "MMMM d, yyyy", "MMMM d yyyy", "M/d/yy", "d MMMM, yyyy", "d MMMM yyyy", "d MMMM", "MMMM d", "yyyy", "h:mm a MMMM d, yyyy" };
    String[] inputStrings = { "bogus string", null, null, null, null, null, null, null, null, null, "April 1, 1997", "April 1, 1997", null, null, null, null, null, "April 1", null, null, "Jan 1, 1970", "January 1, 1970", null, null, null, null, null, "January 1", null, null, "Jan 1 2037", null, "January 1 2037", null, null, null, null, "January 1", null, null, "1/1/70", null, null, "1/1/70", null, null, null, null, "0001", null, "5 May 1997", null, null, null, null, "5 May 1997", "5 May", null, "0005", null, "16 May", null, null, null, null, null, "16 May", null, "0016", null, "April 30", null, null, null, null, null, null, "April 30", null, null, "1998", null, null, null, null, null, null, null, "1998", null, "1", null, null, null, null, null, null, null, "0001", null, "3:00 pm Jan 1, 1997", null, null, null, null, null, null, null, "0003", "3:00 PM January 1, 1997" };
    final int PF_LENGTH = parseFormats.length;
    final int INPUT_LENGTH = inputStrings.length;
    dateParse.applyPattern("d MMMM, yyyy");
    dateParse.setTimeZone(TimeZone.getDefault());
    ss = "not parseable";
    // String thePat;
    logln("Trying to parse \"" + ss + "\" with " + dateParse.toPattern());
    try {
        date = dateParse.parse(ss);
    } catch (Exception ex) {
        logln("FAIL:" + ex);
    }
    for (int i = 0; i < INPUT_LENGTH; i += (PF_LENGTH + 1)) {
        ParsePosition parsePosition = new ParsePosition(0);
        String s = inputStrings[i];
        for (int index = 0; index < PF_LENGTH; ++index) {
            final String expected = inputStrings[i + 1 + index];
            dateParse.applyPattern(parseFormats[index]);
            dateParse.setTimeZone(TimeZone.getDefault());
            try {
                parsePosition.setIndex(0);
                date = dateParse.parse(s, parsePosition);
                if (parsePosition.getIndex() != 0) {
                    String s1, s2;
                    s1 = s.substring(0, parsePosition.getIndex());
                    s2 = s.substring(parsePosition.getIndex(), s.length());
                    if (date == null) {
                        errln("ERROR: null result fmt=\"" + parseFormats[index] + "\" pos=" + parsePosition.getIndex() + " " + s1 + "|" + s2);
                    } else {
                        String result = ((DateFormat) dateParse).format(date);
                        logln("Parsed \"" + s + "\" using \"" + dateParse.toPattern() + "\" to: " + result);
                        if (expected == null)
                            errln("FAIL: Expected parse failure for <" + result + ">");
                        else if (!result.equals(expected))
                            errln("FAIL: Expected " + expected);
                    }
                } else if (expected != null) {
                    errln("FAIL: Expected " + expected + " from \"" + s + "\" with \"" + dateParse.toPattern() + "\"");
                }
            } catch (Exception ex) {
                logln("FAIL:" + ex);
            }
        }
    }
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 70 with DateFormat

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

the class DateFormatTest method tryPattern.

// interl test subroutine, used by TestDayOfYearPattern195
public void tryPattern(SimpleDateFormat sdf, Date d, String pattern, Date expected) {
    if (pattern != null)
        sdf.applyPattern(pattern);
    logln("pattern: " + sdf.toPattern());
    String formatResult = ((DateFormat) sdf).format(d);
    logln(" format -> " + formatResult);
    try {
        Date d2 = sdf.parse(formatResult);
        logln(" parse(" + formatResult + ") -> " + d2);
        if (!d2.equals(expected))
            errln("FAIL: Expected " + expected);
        String format2 = ((DateFormat) sdf).format(d2);
        logln(" format -> " + format2);
        if (!formatResult.equals(format2))
            errln("FAIL: Round trip drift");
    } catch (Exception e) {
        errln(e.getMessage());
    }
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException)

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