Search in sources :

Example 61 with SimpleDateFormat

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

the class DateIntervalFormatTest method stress.

private void stress(String[] data, int data_length, Locale loc, String locName) {
    String[] skeleton = { "EEEEdMMMMy", "dMMMMy", "dMMMM", "MMMMy", "EEEEdMMMM", "EEEdMMMy", "dMMMy", "dMMM", "MMMy", "EEEdMMM", "EEEdMy", "dMy", "dM", "My", "EEEdM", "d", "EEEd", "y", "M", "MMM", "MMMM", "hm", "hmv", "hmz", "h", "hv", "hz", // following could be normalized
    "EEddMMyyyy", "EddMMy", "hhmm", "hhmmzz", // following could not be normalized
    "hms", "dMMMMMy", "EEEEEdM" };
    int i = 0;
    SimpleDateFormat ref = new SimpleDateFormat(data[i++], loc);
    while (i < data_length) {
        // 'f'
        String datestr = data[i++];
        String datestr_2 = data[i++];
        Date date;
        Date date_2;
        try {
            date = ref.parse(datestr);
            date_2 = ref.parse(datestr_2);
        } catch (ParseException e) {
            errln("parse exception" + e);
            continue;
        }
        DateInterval dtitv = new DateInterval(date.getTime(), date_2.getTime());
        for (int skeletonIndex = 0; skeletonIndex < skeleton.length; ++skeletonIndex) {
            String oneSkeleton = skeleton[skeletonIndex];
            // need special handle of "Thai" since the default calendar
            // of "Thai" is "Budd", not "Gregorian".
            DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance(oneSkeleton, loc);
            /*
                if ( locName.equals("th") ) {
                    // reset calendar to be Gregorian
                    GregorianCalendar gregCal = new GregorianCalendar(loc);
                    DateFormat dformat = dtitvfmt.getDateFormat();
                    DateFormat newOne = (DateFormat)dformat.clone();
                    newOne.setCalendar(gregCal);
                    dtitvfmt.setDateFormat(newOne);
                }
                 */
            dtitvfmt.format(dtitv);
        }
        // test interval format by algorithm
        for (int style = DateFormat.FULL; style < 4; ++style) {
            SimpleDateFormat dtfmt = (SimpleDateFormat) DateFormat.getDateInstance(style, loc);
            FieldPosition pos = new FieldPosition(0);
            StringBuffer str = new StringBuffer("");
            Calendar fromCalendar = (Calendar) dtfmt.getCalendar().clone();
            Calendar toCalendar = (Calendar) dtfmt.getCalendar().clone();
            fromCalendar.setTimeInMillis(dtitv.getFromDate());
            toCalendar.setTimeInMillis(dtitv.getToDate());
            dtfmt.intervalFormatByAlgorithm(fromCalendar, toCalendar, str, pos);
        }
    }
}
Also used : Calendar(android.icu.util.Calendar) DateIntervalFormat(android.icu.text.DateIntervalFormat) ParseException(java.text.ParseException) DateInterval(android.icu.util.DateInterval) FieldPosition(java.text.FieldPosition) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date)

Example 62 with SimpleDateFormat

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

the class GlobalizationPreferencesTest method TestDateFormat.

@Test
public void TestDateFormat() {
    GlobalizationPreferences gp = new GlobalizationPreferences();
    String pattern;
    DateFormat df;
    // Set unsupported locale - ach
    logln("Set locale - ach");
    gp.setLocale(new ULocale("ach"));
    // Date - short
    df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_NONE);
    pattern = ((SimpleDateFormat) df).toPattern();
    // root pattern must be used
    if (!pattern.equals("y-MM-dd")) {
        errln("FAIL: SHORT date pattern is " + pattern + " Expected: y-MM-dd");
    }
    // Set locale - fr, fr_CA, fr_FR
    ArrayList lcls = new ArrayList(3);
    lcls.add(new ULocale("fr"));
    lcls.add(new ULocale("fr_CA"));
    lcls.add(new ULocale("fr_FR"));
    logln("Set locales - fr, fr_CA, fr_FR");
    gp.setLocales(lcls);
    // Date - short
    df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_NONE);
    pattern = ((SimpleDateFormat) df).toPattern();
    // fr_CA pattern must be used
    if (!pattern.equals("yy-MM-dd")) {
        errln("FAIL: SHORT date pattern is " + pattern + " Expected: yy-MM-dd");
    }
    // Set locale - en_GB
    logln("Set locale - en_GB");
    gp.setLocale(new ULocale("en_GB"));
    // Date - full
    df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE);
    pattern = ((SimpleDateFormat) df).toPattern();
    if (!pattern.equals("EEEE, d MMMM y")) {
        errln("FAIL: FULL date pattern is " + pattern + " Expected: EEEE, d MMMM y");
    }
    // Date - long
    df = gp.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE);
    pattern = ((SimpleDateFormat) df).toPattern();
    if (!pattern.equals("d MMMM y")) {
        errln("FAIL: LONG date pattern is " + pattern + " Expected: d MMMM y");
    }
    // Date - medium
    df = gp.getDateFormat(GlobalizationPreferences.DF_MEDIUM, GlobalizationPreferences.DF_NONE);
    pattern = ((SimpleDateFormat) df).toPattern();
    if (!pattern.equals("d MMM y")) {
        errln("FAIL: MEDIUM date pattern is " + pattern + " Expected: d MMM y");
    }
    // Date - short
    df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_NONE);
    pattern = ((SimpleDateFormat) df).toPattern();
    if (!pattern.equals("dd/MM/y")) {
        errln("FAIL: SHORT date pattern is " + pattern + " Expected: dd/MM/y");
    }
    // Time - full
    df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_FULL);
    pattern = ((SimpleDateFormat) df).toPattern();
    if (!pattern.equals("HH:mm:ss zzzz")) {
        errln("FAIL: FULL time pattern is " + pattern + " Expected: HH:mm:ss zzzz");
    }
    // Time - long
    df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_LONG);
    pattern = ((SimpleDateFormat) df).toPattern();
    if (!pattern.equals("HH:mm:ss z")) {
        errln("FAIL: LONG time pattern is " + pattern + " Expected: HH:mm:ss z");
    }
    // Time - medium
    df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_MEDIUM);
    pattern = ((SimpleDateFormat) df).toPattern();
    if (!pattern.equals("HH:mm:ss")) {
        errln("FAIL: MEDIUM time pattern is " + pattern + " Expected: HH:mm:ss");
    }
    // Time - short
    df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_SHORT);
    pattern = ((SimpleDateFormat) df).toPattern();
    if (!pattern.equals("HH:mm")) {
        errln("FAIL: SHORT time pattern is " + pattern + " Expected: HH:mm");
    }
    // Date/Time - full
    df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_FULL);
    pattern = ((SimpleDateFormat) df).toPattern();
    if (!pattern.equals("EEEE, d MMMM y 'at' HH:mm:ss zzzz")) {
        errln("FAIL: FULL date/time pattern is " + pattern + " Expected: EEEE, d MMMM y 'at' HH:mm:ss zzzz");
    }
    // Invalid style
    boolean illegalArg = false;
    try {
        df = gp.getDateFormat(-1, GlobalizationPreferences.DF_NONE);
    } catch (IllegalArgumentException iae) {
        logln("Illegal date style -1");
        illegalArg = true;
    }
    if (!illegalArg) {
        errln("FAIL: getDateFormat() must throw IllegalArgumentException for dateStyle -1");
    }
    illegalArg = false;
    try {
        df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_NONE);
    } catch (IllegalArgumentException iae) {
        logln("Illegal style - dateStyle:DF_NONE / timeStyle:DF_NONE");
        illegalArg = true;
    }
    if (!illegalArg) {
        errln("FAIL: getDateFormat() must throw IllegalArgumentException for dateStyle:DF_NONE/timeStyle:DF_NONE");
    }
    // Set explicit time zone
    logln("Set timezone - America/Sao_Paulo");
    TimeZone tz = TimeZone.getTimeZone("America/Sao_Paulo");
    gp.setTimeZone(tz);
    df = gp.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_MEDIUM);
    String tzid = df.getTimeZone().getID();
    if (!tzid.equals("America/Sao_Paulo")) {
        errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
    }
    // Set explicit calendar
    logln("Set calendar - japanese");
    Calendar jcal = new JapaneseCalendar();
    jcal.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
    gp.setCalendar(jcal);
    df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_SHORT);
    Calendar dfCal = df.getCalendar();
    if (!(dfCal instanceof JapaneseCalendar)) {
        errln("FAIL: The DateFormat instance must use Japanese calendar");
    }
    // TimeZone must be still America/Sao_Paulo
    tzid = df.getTimeZone().getID();
    if (!tzid.equals("America/Sao_Paulo")) {
        errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
    }
    // Set explicit DateFormat
    logln("Set explicit date format - full date");
    DateFormat customFD = DateFormat.getDateInstance(new IslamicCalendar(), DateFormat.FULL, new ULocale("ar_SA"));
    customFD.setTimeZone(TimeZone.getTimeZone("Asia/Riyadh"));
    gp.setDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE, customFD);
    df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE);
    dfCal = df.getCalendar();
    if (!(dfCal instanceof IslamicCalendar)) {
        errln("FAIL: The DateFormat instance must use Islamic calendar");
    }
    // TimeZone in the custom DateFormat is overridden by GP's timezone setting
    tzid = df.getTimeZone().getID();
    if (!tzid.equals("America/Sao_Paulo")) {
        errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
    }
    // Freeze
    logln("Freeze this object");
    gp.freeze();
    DateFormat customLD = DateFormat.getDateInstance(new BuddhistCalendar(), DateFormat.LONG, new ULocale("th"));
    customLD.setTimeZone(TimeZone.getTimeZone("Asia/Bangkok"));
    boolean isFrozen = false;
    try {
        gp.setDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE, customLD);
    } catch (UnsupportedOperationException uoe) {
        logln("setDateFormat is blocked");
        isFrozen = true;
    }
    if (!isFrozen) {
        errln("FAIL: setDateFormat must be blocked after frozen");
    }
    // Modifiable clone
    logln("cloneAsThawed");
    GlobalizationPreferences gp1 = (GlobalizationPreferences) gp.cloneAsThawed();
    gp1.setDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE, customLD);
    df = gp1.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_SHORT);
    dfCal = df.getCalendar();
    if (!(dfCal instanceof JapaneseCalendar)) {
        errln("FAIL: The DateFormat instance must use Japanese calendar");
    }
    // TimeZone must be still America/Sao_Paulo
    tzid = df.getTimeZone().getID();
    if (!tzid.equals("America/Sao_Paulo")) {
        errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
    }
    df = gp1.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE);
    dfCal = df.getCalendar();
    if (!(dfCal instanceof BuddhistCalendar)) {
        errln("FAIL: The DateFormat instance must use Buddhist calendar");
    }
    // TimeZone must be still America/Sao_Paulo
    tzid = df.getTimeZone().getID();
    if (!tzid.equals("America/Sao_Paulo")) {
        errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
    }
}
Also used : GlobalizationPreferences(android.icu.util.GlobalizationPreferences) ULocale(android.icu.util.ULocale) IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) BuddhistCalendar(android.icu.util.BuddhistCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) ArrayList(java.util.ArrayList) JapaneseCalendar(android.icu.util.JapaneseCalendar) TimeZone(android.icu.util.TimeZone) BuddhistCalendar(android.icu.util.BuddhistCalendar) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) IslamicCalendar(android.icu.util.IslamicCalendar) Test(org.junit.Test)

Example 63 with SimpleDateFormat

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

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

the class DateFormatTest method TestTwoDigitWOY.

@Test
public void TestTwoDigitWOY() {
    // See ICU Ticket #8514
    String dateText = new String("98MON01");
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYEEEww");
    simpleDateFormat.set2DigitYearStart(new GregorianCalendar(1999, 0, 1).getTime());
    Calendar cal = new GregorianCalendar();
    cal.clear();
    cal.setFirstDayOfWeek(Calendar.SUNDAY);
    cal.setMinimalDaysInFirstWeek(4);
    ParsePosition pp = new ParsePosition(0);
    simpleDateFormat.parse(dateText, cal, pp);
    if (pp.getErrorIndex() >= 0) {
        errln("FAIL: Error in parsing two digit WOY");
    }
    simpleDateFormat.applyPattern("Y");
    String result = simpleDateFormat.format(cal.getTime());
    if (!result.equals("2098")) {
        errln("FAIL: Unexpected result in two digit WOY parse.  Expected 2098, got " + result);
    }
}
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) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 65 with SimpleDateFormat

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

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