Search in sources :

Example 16 with DateFormat

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

the class DateFormatRegressionTest method Test4100302.

/**
 * @bug 4100302
 */
@Test
public void Test4100302() {
    Locale[] locales = { Locale.CANADA, Locale.CANADA_FRENCH, Locale.CHINA, Locale.CHINESE, Locale.ENGLISH, Locale.FRANCE, Locale.FRENCH, Locale.GERMAN, Locale.GERMANY, Locale.ITALIAN, Locale.ITALY, Locale.JAPAN, Locale.JAPANESE, Locale.KOREA, Locale.KOREAN, Locale.PRC, Locale.SIMPLIFIED_CHINESE, Locale.TAIWAN, Locale.TRADITIONAL_CHINESE, Locale.UK, Locale.US };
    try {
        boolean pass = true;
        for (int i = 0; i < 21; i++) {
            Format format = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locales[i]);
            byte[] bytes;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(format);
            oos.flush();
            baos.close();
            bytes = baos.toByteArray();
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
            Object o = ois.readObject();
            if (!format.equals(o)) {
                pass = false;
                logln("DateFormat instance for locale " + locales[i] + " is incorrectly serialized/deserialized.");
            } else {
                logln("DateFormat instance for locale " + locales[i] + " is OKAY.");
            }
        }
        if (!pass)
            errln("Fail: DateFormat serialization/equality bug");
    } catch (OptionalDataException e) {
        errln("Fail: " + e);
    } catch (IOException e) {
        errln("Fail: " + e);
    } catch (ClassNotFoundException e) {
        errln("Fail: " + e);
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) OptionalDataException(java.io.OptionalDataException) Format(java.text.Format) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 17 with DateFormat

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

the class DateFormatRegressionTest method Test4106807.

/**
 * @bug 4106807
 */
@Test
public void Test4106807() {
    Date dt;
    DateFormat df = DateFormat.getDateTimeInstance();
    SimpleDateFormat[] sdfs = { new SimpleDateFormat("yyyyMMddHHmmss"), new SimpleDateFormat("yyyyMMddHHmmss'Z'"), new SimpleDateFormat("yyyyMMddHHmmss''"), new SimpleDateFormat("yyyyMMddHHmmss'a''a'"), new SimpleDateFormat("yyyyMMddHHmmss %") };
    String[] strings = { "19980211140000", "19980211140000", "19980211140000", "19980211140000a", "19980211140000 " };
    GregorianCalendar gc = new GregorianCalendar();
    TimeZone timeZone = TimeZone.getDefault();
    TimeZone gmt = (TimeZone) timeZone.clone();
    gmt.setRawOffset(0);
    for (int i = 0; i < 5; i++) {
        SimpleDateFormat format = sdfs[i];
        String dateString = strings[i];
        try {
            format.setTimeZone(gmt);
            dt = format.parse(dateString);
            // {sfb} some of these parses will fail purposely
            StringBuffer fmtd = new StringBuffer("");
            FieldPosition pos = new FieldPosition(0);
            fmtd = df.format(dt, fmtd, pos);
            logln(fmtd.toString());
            // logln(df.format(dt));
            gc.setTime(dt);
            logln("" + gc.get(Calendar.ZONE_OFFSET));
            StringBuffer s = new StringBuffer("");
            s = format.format(dt, s, pos);
            logln(s.toString());
        } catch (ParseException e) {
            logln("No way Jose");
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) ParseException(java.text.ParseException) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 18 with DateFormat

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

the class DataDrivenFormatTest method testConvertDate.

private void testConvertDate(TestDataModule.TestData testData, DataMap settings, boolean fmt) {
    DateFormat basicFmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
    int n = 0;
    for (Iterator iter = testData.getDataIterator(); iter.hasNext(); ) {
        ++n;
        long now = System.currentTimeMillis();
        DataMap currentCase = (DataMap) iter.next();
        String caseString = "[" + testData.getName() + "#" + n + (fmt ? "format" : "parse") + "]";
        String locale = currentCase.getString("locale");
        String zone = currentCase.getString("zone");
        String spec = currentCase.getString("spec");
        String date = currentCase.getString("date");
        String str = currentCase.getString("str");
        Date fromDate = null;
        boolean useDate = false;
        ULocale loc = new ULocale(locale);
        String pattern = null;
        // boolean usePattern = false;
        DateFormat format = null;
        DateTimeStyleSet styleSet;
        CalendarFieldsSet fromSet = null;
        // parse 'spec'  - either 'PATTERN=yy mm dd' or 'DATE=x,TIME=y'
        if (spec.startsWith(kPATTERN)) {
            pattern = spec.substring(kPATTERN.length());
            // usePattern = true;
            format = new SimpleDateFormat(pattern, loc);
        } else {
            styleSet = new DateTimeStyleSet();
            styleSet.parseFrom(spec);
            format = DateFormat.getDateTimeInstance(styleSet.getDateStyle(), styleSet.getTimeStyle(), loc);
        }
        Calendar cal = Calendar.getInstance(loc);
        if (zone.length() > 0) {
            TimeZone tz = TimeZone.getFrozenTimeZone(zone);
            cal.setTimeZone(tz);
            format.setTimeZone(tz);
        }
        // parse 'date' - either 'MILLIS=12345' or  a CalendarFieldsSet
        if (date.startsWith(kMILLIS)) {
            useDate = true;
            fromDate = new Date(Long.parseLong(date.substring(kMILLIS.length())));
        } else if (date.startsWith(kRELATIVE_MILLIS)) {
            useDate = true;
            fromDate = new Date(now + Long.parseLong(date.substring(kRELATIVE_MILLIS.length())));
        } else if (date.startsWith(kRELATIVE_ADD)) {
            // "add" is a string indicating which fields to add
            String add = date.substring(kRELATIVE_ADD.length());
            CalendarFieldsSet addSet = new CalendarFieldsSet();
            addSet.parseFrom(add);
            useDate = true;
            cal.clear();
            cal.setTimeInMillis(now);
            // / perform op on 'to calendar'
            for (int q = 0; q < addSet.fieldCount(); q++) {
                if (addSet.isSet(q)) {
                    if (q == Calendar.DATE) {
                        cal.add(q, addSet.get(q));
                    } else {
                        cal.set(q, addSet.get(q));
                    }
                }
            }
            fromDate = cal.getTime();
        } else {
            fromSet = new CalendarFieldsSet();
            fromSet.parseFrom(date);
        }
        // run the test
        if (fmt) {
            StringBuffer output = new StringBuffer();
            cal.clear();
            FieldPosition pos = new FieldPosition(0);
            if (useDate) {
                output = format.format(fromDate, output, pos);
            } else {
                fromSet.setOnCalendar(cal);
                format.format(cal, output, pos);
            }
            if (output.toString().equals(str)) {
                logln(caseString + " Success - strings match: " + output);
            } else {
                errln(caseString + " FAIL: got " + output + " expected " + str);
            }
        } else {
            // parse
            cal.clear();
            ParsePosition pos = new ParsePosition(0);
            format.parse(str, cal, pos);
            if (useDate) {
                Date gotDate = cal.getTime();
                if (gotDate.equals(fromDate)) {
                    logln(caseString + " SUCCESS: got=parse=" + str);
                } else {
                    errln(caseString + " FAIL: parsed " + str + " but got " + basicFmt.format(gotDate) + " - " + gotDate + "  expected " + basicFmt.format(fromDate));
                }
            } else {
                CalendarFieldsSet diffSet = new CalendarFieldsSet();
                if (!fromSet.matches(cal, diffSet)) {
                    String diffs = diffSet.diffFrom(fromSet);
                    errln(caseString + " FAIL:  differences: " + diffs);
                } else {
                    logln(caseString + " SUCCESS: got=parse: " + str + " - " + fromSet.toString());
                }
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Calendar(android.icu.util.Calendar) CalendarFieldsSet(android.icu.dev.test.util.CalendarFieldsSet) FieldPosition(java.text.FieldPosition) Date(java.util.Date) DataMap(android.icu.dev.test.TestDataModule.DataMap) TimeZone(android.icu.util.TimeZone) DateTimeStyleSet(android.icu.dev.test.util.DateTimeStyleSet) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Iterator(java.util.Iterator) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition)

Example 19 with DateFormat

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

the class DateFormatRegressionTest method Test4065240.

/**
 * @bug 4065240
 */
@Test
public void Test4065240() {
    Date curDate;
    DateFormat shortdate, fulldate;
    String strShortDate, strFullDate;
    Locale saveLocale = Locale.getDefault();
    TimeZone saveZone = TimeZone.getDefault();
    try {
        Locale curLocale = new Locale("de", "DE");
        Locale.setDefault(curLocale);
        // {sfb} adoptDefault instead of setDefault
        // TimeZone.setDefault(TimeZone.createTimeZone("EST"));
        TimeZone.setDefault(TimeZone.getTimeZone("EST"));
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(98 + 1900, 0, 1);
        curDate = cal.getTime();
        shortdate = DateFormat.getDateInstance(DateFormat.SHORT);
        fulldate = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
        strShortDate = "The current date (short form) is ";
        String temp;
        temp = shortdate.format(curDate);
        strShortDate += temp;
        strFullDate = "The current date (long form) is ";
        String temp2 = fulldate.format(curDate);
        strFullDate += temp2;
        logln(strShortDate);
        logln(strFullDate);
    // {sfb} What to do with resource bundle stuff?????
    // Check to see if the resource is present; if not, we can't test
    // ResourceBundle bundle = //The variable is never used
    // ICULocaleData.getBundle("DateFormatZoneData", curLocale);
    // {sfb} API change to ResourceBundle -- add getLocale()
    /*if (bundle.getLocale().getLanguage().equals("de")) {
                // UPDATE THIS AS ZONE NAME RESOURCE FOR <EST> in de_DE is updated
                if (!strFullDate.endsWith("GMT-05:00"))
                    errln("Fail: Want GMT-05:00");
            } else {
                logln("*** TEST COULD NOT BE COMPLETED BECAUSE DateFormatZoneData ***");
                logln("*** FOR LOCALE de OR de_DE IS MISSING ***");
            }*/
    } catch (Exception e) {
        logln(e.getMessage());
    } finally {
        Locale.setDefault(saveLocale);
        TimeZone.setDefault(saveZone);
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) OptionalDataException(java.io.OptionalDataException) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 20 with DateFormat

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

the class DateFormatRegressionTest method Test4071441.

/*
      DateFormat.equals is too narrowly defined.  As a result, MessageFormat
      does not work correctly.  DateFormat.equals needs to be written so
      that the Calendar sub-object is not compared using Calendar.equals,
      but rather compared for equivalency.  This may necessitate adding a
      (package private) method to Calendar to test for equivalency.
      
      Currently this bug breaks MessageFormat.toPattern
      */
/**
 * @bug 4071441
 */
@Test
public void Test4071441() {
    DateFormat fmtA = DateFormat.getInstance();
    DateFormat fmtB = DateFormat.getInstance();
    // {sfb} Is it OK to cast away const here?
    Calendar calA = fmtA.getCalendar();
    Calendar calB = fmtB.getCalendar();
    calA.clear();
    calA.set(1900, 0, 0);
    calB.clear();
    calB.set(1900, 0, 0);
    if (!calA.equals(calB))
        errln("Fail: Can't complete test; Calendar instances unequal");
    if (!fmtA.equals(fmtB))
        errln("Fail: DateFormat unequal when Calendars equal");
    calB.clear();
    calB.set(1961, Calendar.DECEMBER, 25);
    if (calA.equals(calB))
        errln("Fail: Can't complete test; Calendar instances equal");
    if (!fmtA.equals(fmtB))
        errln("Fail: DateFormat unequal when Calendars equivalent");
    logln("DateFormat.equals ok");
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) 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