Search in sources :

Example 26 with SimpleDateFormat

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

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

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

the class DateFormatRegressionTest method Test4101483.

/**
 * @bug 4101483
 */
@Test
public void Test4101483() {
    SimpleDateFormat sdf = new SimpleDateFormat("z", Locale.US);
    FieldPosition fp = new FieldPosition(DateFormat.TIMEZONE_FIELD);
    Date d = new Date(9234567890L);
    StringBuffer buf = new StringBuffer("");
    sdf.format(d, buf, fp);
    logln(sdf.format(d, buf, fp).toString());
    logln("beginIndex = " + fp.getBeginIndex());
    logln("endIndex = " + fp.getEndIndex());
    if (fp.getBeginIndex() == fp.getEndIndex())
        errln("Fail: Empty field");
}
Also used : FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 29 with SimpleDateFormat

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

the class DateFormatRegressionTest method Test4089106.

/**
 * @bug 4089106
 */
@Test
public void Test4089106() {
    TimeZone def = TimeZone.getDefault();
    try {
        TimeZone z = new SimpleTimeZone((int) (1.25 * 3600000), "FAKEZONE");
        TimeZone.setDefault(z);
        // Android patch (http://b/28949992) start.
        // ICU TimeZone.setDefault() not supported on Android.
        z = TimeZone.getDefault();
        // Android patch (http://b/28949992) end.
        SimpleDateFormat f = new SimpleDateFormat();
        if (!f.getTimeZone().equals(z))
            errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
    } finally {
        TimeZone.setDefault(def);
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 30 with SimpleDateFormat

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

the class DateFormatRegressionTest method Test4151706.

/**
 * @bug 4151706
 * 'z' at end of date format throws index exception in SimpleDateFormat
 * CANNOT REPRODUCE THIS BUG ON 1.2FCS
 */
@Test
public void Test4151706() {
    String dateString = "Thursday, 31-Dec-98 23:00:00 GMT";
    SimpleDateFormat fmt = new SimpleDateFormat("EEEE, dd-MMM-yy HH:mm:ss z", Locale.US);
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.US);
    cal.clear();
    cal.set(1998, Calendar.DECEMBER, 31, 23, 0, 0);
    Date d = new Date();
    try {
        d = fmt.parse(dateString);
        // {sfb} what about next two lines?
        if (d.getTime() != cal.getTime().getTime())
            errln("Incorrect value: " + d);
    } catch (Exception e) {
        errln("Fail: " + e);
    }
    StringBuffer temp = new StringBuffer("");
    FieldPosition pos = new FieldPosition(0);
    logln(dateString + " . " + fmt.format(d, temp, pos));
}
Also used : IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) OptionalDataException(java.io.OptionalDataException) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Aggregations

SimpleDateFormat (android.icu.text.SimpleDateFormat)153 Test (org.junit.Test)113 Date (java.util.Date)103 GregorianCalendar (android.icu.util.GregorianCalendar)59 Calendar (android.icu.util.Calendar)53 ParseException (java.text.ParseException)42 DateFormat (android.icu.text.DateFormat)41 JapaneseCalendar (android.icu.util.JapaneseCalendar)41 ULocale (android.icu.util.ULocale)40 IslamicCalendar (android.icu.util.IslamicCalendar)37 ParsePosition (java.text.ParsePosition)36 FieldPosition (java.text.FieldPosition)29 ChineseCalendar (android.icu.util.ChineseCalendar)27 TimeZone (android.icu.util.TimeZone)27 BuddhistCalendar (android.icu.util.BuddhistCalendar)25 ChineseDateFormat (android.icu.text.ChineseDateFormat)21 HebrewCalendar (android.icu.util.HebrewCalendar)21 IOException (java.io.IOException)19 SimpleTimeZone (android.icu.util.SimpleTimeZone)16 Locale (java.util.Locale)14