Search in sources :

Example 31 with DateFormat

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

the class DateFormatTest method Test6880.

@Test
public void Test6880() {
    Date d1, d2, dp1, dp2, dexp1, dexp2;
    String s1, s2;
    TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
    GregorianCalendar gcal = new GregorianCalendar(tz);
    gcal.clear();
    // offset 8:05:43
    gcal.set(1900, Calendar.JANUARY, 1, 12, 00);
    d1 = gcal.getTime();
    gcal.clear();
    // offset 8:00
    gcal.set(1950, Calendar.JANUARY, 1, 12, 00);
    d2 = gcal.getTime();
    gcal.clear();
    gcal.set(1970, Calendar.JANUARY, 1, 12, 00);
    dexp2 = gcal.getTime();
    // subtract 5m43s
    dexp1 = new Date(dexp2.getTime() - (5 * 60 + 43) * 1000);
    DateFormat fmt = DateFormat.getTimeInstance(DateFormat.FULL, new ULocale("zh"));
    fmt.setTimeZone(tz);
    s1 = fmt.format(d1);
    s2 = fmt.format(d2);
    try {
        dp1 = fmt.parse(s1);
        dp2 = fmt.parse(s2);
        if (!dp1.equals(dexp1)) {
            errln("FAIL: Failed to parse " + s1 + " parsed: " + dp1 + " expected: " + dexp1);
        }
        if (!dp2.equals(dexp2)) {
            errln("FAIL: Failed to parse " + s2 + " parsed: " + dp2 + " expected: " + dexp2);
        }
    } catch (ParseException pe) {
        errln("FAIL: Parse failure");
    }
}
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) GregorianCalendar(android.icu.util.GregorianCalendar) ParseException(java.text.ParseException) Date(java.util.Date) Test(org.junit.Test)

Example 32 with DateFormat

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

the class DateFormatTest method TestDateFormatNone.

/**
 * Test the formatting of dates with the 'NONE' keyword.
 */
@Test
public void TestDateFormatNone() {
    // Mon Sep 15 00:00:00 PDT 1997
    Date testDate = new Date(874306800000L);
    DateFormat dfFrench = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.NONE, Locale.FRENCH);
    // Set TimeZone = PDT
    TimeZone tz = TimeZone.getTimeZone("PST");
    dfFrench.setTimeZone(tz);
    String expectedFRENCH_JDK12 = "lundi 15 septembre 1997";
    // String expectedFRENCH = "lundi 15 septembre 1997 00 h 00 PDT";
    logln("Date set to : " + testDate);
    String out = dfFrench.format(testDate);
    logln("Date Formated with French Locale " + out);
    if (!out.equals(expectedFRENCH_JDK12))
        errln("FAIL: Expected " + expectedFRENCH_JDK12 + " Got " + out);
}
Also used : TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 33 with DateFormat

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

the class DateFormatTest method TestFormatToCharacterIteratorCoverage.

/*
     * API coverage test case for formatToCharacterIterator
     */
@Test
public void TestFormatToCharacterIteratorCoverage() {
    // Calling formatToCharacterIterator, using various argument types
    DateFormat df = DateFormat.getDateTimeInstance();
    AttributedCharacterIterator acit = null;
    Calendar cal = Calendar.getInstance();
    try {
        acit = df.formatToCharacterIterator(cal);
        if (acit == null) {
            errln("FAIL: null AttributedCharacterIterator returned by formatToCharacterIterator(Calendar)");
        }
    } catch (IllegalArgumentException iae) {
        errln("FAIL: Calendar must be accepted by formatToCharacterIterator");
    }
    Date d = cal.getTime();
    try {
        acit = df.formatToCharacterIterator(d);
        if (acit == null) {
            errln("FAIL: null AttributedCharacterIterator returned by formatToCharacterIterator(Date)");
        }
    } catch (IllegalArgumentException iae) {
        errln("FAIL: Date must be accepted by formatToCharacterIterator");
    }
    Number num = new Long(d.getTime());
    try {
        acit = df.formatToCharacterIterator(num);
        if (acit == null) {
            errln("FAIL: null AttributedCharacterIterator returned by formatToCharacterIterator(Number)");
        }
    } catch (IllegalArgumentException iae) {
        errln("FAIL: Number must be accepted by formatToCharacterIterator");
    }
    boolean isException = false;
    String str = df.format(d);
    try {
        acit = df.formatToCharacterIterator(str);
        if (acit == null) {
            errln("FAIL: null AttributedCharacterIterator returned by formatToCharacterIterator(String)");
        }
    } catch (IllegalArgumentException iae) {
        logln("IllegalArgumentException is thrown by formatToCharacterIterator");
        isException = true;
    }
    if (!isException) {
        errln("FAIL: String must not be accepted by formatToCharacterIterator");
    }
    // DateFormat.Field#ofCalendarField and getCalendarField
    for (int i = 0; i < DATEFORMAT_FIELDS.length; i++) {
        int calField = DATEFORMAT_FIELDS[i].getCalendarField();
        if (calField != -1) {
            DateFormat.Field field = DateFormat.Field.ofCalendarField(calField);
            if (field != DATEFORMAT_FIELDS[i]) {
                errln("FAIL: " + field + " is returned for a Calendar field " + calField + " - Expected: " + DATEFORMAT_FIELDS[i]);
            }
        }
    }
    // IllegalArgument for ofCalendarField
    isException = false;
    try {
        DateFormat.Field.ofCalendarField(-1);
    } catch (IllegalArgumentException iae) {
        logln("IllegalArgumentException is thrown by ofCalendarField");
        isException = true;
    }
    if (!isException) {
        errln("FAIL: IllegalArgumentException must be thrown by ofCalendarField for calendar field value -1");
    }
    // ChineseDateFormat.Field#ofCalendarField and getCalendarField
    int ccalField = ChineseDateFormat.Field.IS_LEAP_MONTH.getCalendarField();
    if (ccalField != Calendar.IS_LEAP_MONTH) {
        errln("FAIL: ChineseCalendar field " + ccalField + " is returned for ChineseDateFormat.Field.IS_LEAP_MONTH.getCalendarField()");
    } else {
        DateFormat.Field cfield = ChineseDateFormat.Field.ofCalendarField(ccalField);
        if (cfield != ChineseDateFormat.Field.IS_LEAP_MONTH) {
            errln("FAIL: " + cfield + " is returned for a ChineseCalendar field " + ccalField + " - Expected: " + ChineseDateFormat.Field.IS_LEAP_MONTH);
        }
    }
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) 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) Field(android.icu.text.ChineseDateFormat.Field) Date(java.util.Date) AttributedCharacterIterator(java.text.AttributedCharacterIterator) Test(org.junit.Test)

Example 34 with DateFormat

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

the class DateFormatTest method TestBadInput135.

/**
 * Verify the correct behavior when handling invalid input strings.
 */
@Test
public void TestBadInput135() {
    int[] looks = { DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL };
    int looks_length = looks.length;
    final String[] strings = { "Mar 15", "Mar 15 1997", "asdf", "3/1/97 1:23:", "3/1/00 1:23:45 AM" };
    int strings_length = strings.length;
    DateFormat full = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);
    String expected = "March 1, 2000 at 1:23:45 AM ";
    for (int i = 0; i < strings_length; ++i) {
        final String text = strings[i];
        for (int j = 0; j < looks_length; ++j) {
            int dateLook = looks[j];
            for (int k = 0; k < looks_length; ++k) {
                int timeLook = looks[k];
                DateFormat df = DateFormat.getDateTimeInstance(dateLook, timeLook, Locale.US);
                String prefix = text + ", " + dateLook + "/" + timeLook + ": ";
                try {
                    Date when = df.parse(text);
                    if (when == null) {
                        errln(prefix + "SHOULD NOT HAPPEN: parse returned null.");
                        continue;
                    }
                    if (when != null) {
                        String format;
                        format = full.format(when);
                        logln(prefix + "OK: " + format);
                        if (!format.substring(0, expected.length()).equals(expected)) {
                            errln("FAIL: Expected <" + expected + ">, but got <" + format.substring(0, expected.length()) + ">");
                        }
                    }
                } catch (java.text.ParseException e) {
                    logln(e.getMessage());
                }
            }
        }
    }
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DisplayContext(android.icu.text.DisplayContext) ParseException(java.text.ParseException) Date(java.util.Date) Test(org.junit.Test)

Example 35 with DateFormat

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

the class DateFormatTest method TestTwoDigitYearDSTParse.

/**
 * Test the parsing of 2-digit years.
 */
@Test
public void TestTwoDigitYearDSTParse() {
    SimpleDateFormat fullFmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss.SSS zzz yyyy G");
    SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yy h:mm:ss 'o''clock' a z", Locale.ENGLISH);
    String s = "03-Apr-04 2:20:47 o'clock AM PST";
    /*
         * SimpleDateFormat(pattern, locale) Construct a SimpleDateDateFormat using
         * the given pattern, the locale and using the TimeZone.getDefault();
         * So it need to add the timezone offset on hour field.
         * ps. the Method Calendar.getTime() used by SimpleDateFormat.parse() always
         * return Date value with TimeZone.getDefault() [Richard/GCL]
         */
    TimeZone defaultTZ = TimeZone.getDefault();
    TimeZone PST = TimeZone.getTimeZone("PST");
    int defaultOffset = defaultTZ.getRawOffset();
    int PSTOffset = PST.getRawOffset();
    int hour = 2 + (defaultOffset - PSTOffset) / (60 * 60 * 1000);
    // hour is the expected hour of day, in units of seconds
    hour = ((hour < 0) ? hour + 24 : hour) * 60 * 60;
    try {
        Date d = fmt.parse(s);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        // DSTOffset
        hour += defaultTZ.inDaylightTime(d) ? 1 : 0;
        logln(s + " P> " + ((DateFormat) fullFmt).format(d));
        // hr is the actual hour of day, in units of seconds
        // adjust for DST
        int hr = cal.get(Calendar.HOUR_OF_DAY) * 60 * 60 - cal.get(Calendar.DST_OFFSET) / 1000;
        if (hr != hour)
            errln("FAIL: Hour (-DST) = " + hr / (60 * 60.0) + "; expected " + hour / (60 * 60.0));
    } catch (ParseException e) {
        errln("Parse Error:" + e.getMessage());
    }
}
Also used : TimeZone(android.icu.util.TimeZone) 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) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) 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