Search in sources :

Example 41 with DateFormat

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

the class DateFormatTest method TestQuotePattern161.

/**
 * Test the handling of single quotes in patterns.
 */
@Test
public void TestQuotePattern161() {
    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy 'at' hh:mm:ss a zzz", Locale.US);
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(1997, Calendar.AUGUST, 13, 10, 42, 28);
    Date currentTime_1 = cal.getTime();
    String dateString = ((DateFormat) formatter).format(currentTime_1);
    String exp = "08/13/1997 at 10:42:28 AM ";
    logln("format(" + currentTime_1 + ") = " + dateString);
    if (!dateString.substring(0, exp.length()).equals(exp))
        errln("FAIL: Expected " + exp);
}
Also used : 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) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 42 with DateFormat

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

the class DateFormatTest method TestErrorChecking.

@Test
public void TestErrorChecking() {
    try {
        DateFormat.getDateTimeInstance(-1, -1, Locale.US);
        errln("Expected exception for getDateTimeInstance(-1, -1, Locale)");
    } catch (IllegalArgumentException e) {
        logln("one ok");
    } catch (Exception e) {
        warnln("Expected IllegalArgumentException, got: " + e);
    }
    try {
        DateFormat df = new SimpleDateFormat("aaNNccc");
        df.format(new Date());
        errln("Expected exception for format with bad pattern");
    } catch (IllegalArgumentException ex) {
        logln("two ok");
    } catch (Exception e) {
        warnln("Expected IllegalArgumentException, got: " + e);
    }
    {
        // opposite of text
        SimpleDateFormat fmt = new SimpleDateFormat("dd/MM/yy");
        fmt.set2DigitYearStart(getDate(2003, Calendar.DECEMBER, 25));
        String text = "12/25/03";
        Calendar xcal = new GregorianCalendar();
        xcal.setLenient(false);
        ParsePosition pp = new ParsePosition(0);
        // should get parse error on second field, not lenient
        fmt.parse(text, xcal, pp);
        if (pp.getErrorIndex() == -1) {
            errln("Expected parse error");
        } else {
            logln("three ok");
        }
    }
}
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) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParseException(java.text.ParseException) IOException(java.io.IOException) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 43 with DateFormat

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

the class DateFormatTest method TestRelativeDateFormat.

// based on TestRelativeDateFormat() in icu/trunk/source/test/cintltst/cdattst.c
@Test
public void TestRelativeDateFormat() {
    ULocale loc = ULocale.US;
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar cal = new GregorianCalendar(tz, loc);
    Date now = new Date();
    cal.setTime(now);
    cal.set(Calendar.HOUR_OF_DAY, 18);
    cal.set(Calendar.MINUTE, 49);
    cal.set(Calendar.SECOND, 0);
    Date today = cal.getTime();
    // minutes string to search for in formatted result
    String minutesStr = "49";
    int[] dateStylesList = { DateFormat.RELATIVE_FULL, DateFormat.RELATIVE_LONG, DateFormat.RELATIVE_MEDIUM, DateFormat.RELATIVE_SHORT };
    for (int i = 0; i < dateStylesList.length; i++) {
        int dateStyle = dateStylesList[i];
        DateFormat fmtRelDateTime = DateFormat.getDateTimeInstance(dateStyle, DateFormat.SHORT, loc);
        DateFormat fmtRelDate = DateFormat.getDateInstance(dateStyle, loc);
        DateFormat fmtTime = DateFormat.getTimeInstance(DateFormat.SHORT, loc);
        for (int dayOffset = -2; dayOffset <= 2; dayOffset++) {
            StringBuffer dateTimeStr = new StringBuffer(64);
            StringBuffer dateStr = new StringBuffer(64);
            StringBuffer timeStr = new StringBuffer(64);
            FieldPosition fp = new FieldPosition(DateFormat.MINUTE_FIELD);
            cal.setTime(today);
            cal.add(Calendar.DATE, dayOffset);
            fmtRelDateTime.format(cal, dateTimeStr, fp);
            fmtRelDate.format(cal, dateStr, new FieldPosition(0));
            fmtTime.format(cal, timeStr, new FieldPosition(0));
            logln(dayOffset + ", " + dateStyle + ", " + dateTimeStr);
            logln(dayOffset + ", " + dateStyle + ", " + dateStr);
            logln(dayOffset + ", " + dateStyle + ", " + timeStr);
            // check that dateStr is in dateTimeStr
            if (dateTimeStr.toString().indexOf(dateStr.toString()) < 0) {
                errln("relative date string not found in datetime format with timeStyle SHORT, dateStyle " + dateStyle + " for dayOffset " + dayOffset);
                errln("datetime format is " + dateTimeStr.toString() + ", date string is " + dateStr.toString());
            }
            // check that timeStr is in dateTimeStr
            if (dateTimeStr.toString().indexOf(timeStr.toString()) < 0) {
                errln("short time string not found in datetime format with timeStyle SHORT, dateStyle " + dateStyle + " for dayOffset " + dayOffset);
                errln("datetime format is " + dateTimeStr.toString() + ", time string is " + timeStr.toString());
            }
            // check index of minutesStr
            int minutesStrIndex = dateTimeStr.toString().indexOf(minutesStr);
            if (fp.getBeginIndex() != minutesStrIndex) {
                errln("FieldPosition beginIndex " + fp.getBeginIndex() + " instead of " + minutesStrIndex + " for datetime format with timeStyle SHORT, dateStyle " + dateStyle + " for dayOffset " + dayOffset);
                errln("datetime format is " + dateTimeStr.toString());
            }
        }
    }
}
Also used : TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) 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) GregorianCalendar(android.icu.util.GregorianCalendar) FieldPosition(java.text.FieldPosition) Date(java.util.Date) Test(org.junit.Test)

Example 44 with DateFormat

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

the class DateFormatTest method TestEquals.

@Test
public void TestEquals() {
    DateFormat fmtA = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL);
    DateFormat fmtB = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.FULL);
    if (!fmtA.equals(fmtB))
        errln("FAIL");
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 45 with DateFormat

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

the class Calendar method formatHelper.

private static DateFormat formatHelper(Calendar cal, ULocale loc, int dateStyle, int timeStyle) {
    if (timeStyle < DateFormat.NONE || timeStyle > DateFormat.SHORT) {
        throw new IllegalArgumentException("Illegal time style " + timeStyle);
    }
    if (dateStyle < DateFormat.NONE || dateStyle > DateFormat.SHORT) {
        throw new IllegalArgumentException("Illegal date style " + dateStyle);
    }
    PatternData patternData = PatternData.make(cal, loc);
    String override = null;
    // Resolve a pattern for the date/time style
    String pattern = null;
    if ((timeStyle >= 0) && (dateStyle >= 0)) {
        pattern = SimpleFormatterImpl.formatRawPattern(patternData.getDateTimePattern(dateStyle), 2, 2, patternData.patterns[timeStyle], patternData.patterns[dateStyle + 4]);
        // time style.
        if (patternData.overrides != null) {
            String dateOverride = patternData.overrides[dateStyle + 4];
            String timeOverride = patternData.overrides[timeStyle];
            override = mergeOverrideStrings(patternData.patterns[dateStyle + 4], patternData.patterns[timeStyle], dateOverride, timeOverride);
        }
    } else if (timeStyle >= 0) {
        pattern = patternData.patterns[timeStyle];
        if (patternData.overrides != null) {
            override = patternData.overrides[timeStyle];
        }
    } else if (dateStyle >= 0) {
        pattern = patternData.patterns[dateStyle + 4];
        if (patternData.overrides != null) {
            override = patternData.overrides[dateStyle + 4];
        }
    } else {
        throw new IllegalArgumentException("No date or time style specified");
    }
    DateFormat result = cal.handleGetDateFormat(pattern, override, loc);
    result.setCalendar(cal);
    return result;
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat)

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