Search in sources :

Example 96 with DateFormat

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

the class ChineseTest method TestFormat.

/**
 * Test formatting.
 * Q: Why is this in Calendar tests instead of Format tests?
 * Note: This test assumes that Chinese calendar formatted dates can be parsed
 * unambiguously to recover the original Date that was formatted. This is not
 * currently true since Chinese calendar formatted dates do not include an era.
 * To address this will require formatting/parsing of fields from some other
 * associated calendar, as per ICU ticket #9043. This test should be timebombed
 * until that ticket is addressed.
 */
@Test
public void TestFormat() {
    ChineseCalendar cal = new ChineseCalendar();
    DateFormat fmt = DateFormat.getDateTimeInstance(cal, DateFormat.DEFAULT, DateFormat.DEFAULT);
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    Date[] DATA = new Date[2];
    tempcal.set(2001, Calendar.MAY, 22);
    DATA[0] = tempcal.getTime();
    tempcal.set(2001, Calendar.MAY, 23);
    DATA[1] = tempcal.getTime();
    for (int i = 0; i < DATA.length; ++i) {
        String s = fmt.format(DATA[i]);
        try {
            Date e = fmt.parse(s);
            if (e.equals(DATA[i])) {
                logln("Ok: " + DATA[i] + " -> " + s + " -> " + e);
            } else {
                errln("FAIL: " + DATA[i] + " -> " + s + " -> " + e);
            }
        } catch (java.text.ParseException e) {
            errln("Fail: " + s + " -> parse failure at " + e.getErrorOffset());
            errln(e.toString());
        }
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 97 with DateFormat

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

the class TestMessageFormat method testNumericFormatWithMap.

@Test
public void testNumericFormatWithMap() {
    MessageFormat mf = new MessageFormat("X:{2} Y:{1}");
    if (mf.usesNamedArguments()) {
        errln("should not use named arguments");
    }
    Map map12 = new HashMap();
    map12.put("1", "one");
    map12.put("2", "two");
    String target = "X:two Y:one";
    String result = mf.format(map12);
    if (!target.equals(result)) {
        errln("expected '" + target + "' but got '" + result + "'");
    }
    try {
        Map mapResult = mf.parseToMap(target);
        if (!map12.equals(mapResult)) {
            errln("expected " + map12 + " but got " + mapResult);
        }
    } catch (ParseException e) {
        errln("unexpected exception: " + e.getMessage());
    }
    Map map10 = new HashMap();
    map10.put("1", "one");
    map10.put("0", "zero");
    target = "X:{2} Y:one";
    result = mf.format(map10);
    if (!target.equals(result)) {
        errln("expected '" + target + "' but got '" + result + "'");
    }
    DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
    DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
    Map fmtMap = new HashMap();
    fmtMap.put("1", dateFormat);
    fmtMap.put("2", timeFormat);
    mf.setFormatsByArgumentName(fmtMap);
    Date date = new Date(661439820000L);
    try {
        // should fail, wrong argument type
        result = mf.format(map12);
        fail("expected exception but got '" + result + "'");
    } catch (IllegalArgumentException e) {
    // expect this
    }
    Map argMap = new HashMap();
    argMap.put("1", date);
    argMap.put("2", date);
    target = "X:5:17:00 AM Y:Dec 17, 1990";
    result = mf.format(argMap);
    if (!target.equals(result)) {
        errln("expected '" + target + "' but got '" + result + "'");
    }
}
Also used : MessageFormat(android.icu.text.MessageFormat) HashMap(java.util.HashMap) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) AttributedString(java.text.AttributedString) ParseException(java.text.ParseException) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) 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