Search in sources :

Example 36 with DateFormat

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

the class DateFormatTest method TestGetPatternInstance.

/* Tests the method public final static DateFormat getPatternInstance */
@Test
public void TestGetPatternInstance() {
    // public final static DateFormat getPatternInstance(String pattern)
    try {
        @SuppressWarnings("unused") DateFormat df = DateFormat.getPatternInstance("");
        df = DateFormat.getPatternInstance("", new Locale("en_US"));
        df = DateFormat.getPatternInstance(null, "", new Locale("en_US"));
    } catch (Exception e) {
        errln("DateFormat.getPatternInstance is not suppose to return an exception.");
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 37 with DateFormat

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

the class DateFormatTest method TestRunTogetherPattern985.

/**
 * Verify the behavior of patterns in which digits for different fields run together
 * without intervening separators.
 */
@Test
public void TestRunTogetherPattern985() {
    String format = "yyyyMMddHHmmssSSS";
    String now, then;
    // UBool flag;
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    Date date1 = new Date();
    now = ((DateFormat) formatter).format(date1);
    logln(now);
    ParsePosition pos = new ParsePosition(0);
    Date date2 = formatter.parse(now, pos);
    if (date2 == null)
        then = "Parse stopped at " + pos.getIndex();
    else
        then = ((DateFormat) formatter).format(date2);
    logln(then);
    if (date2 == null || !date2.equals(date1))
        errln("FAIL");
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 38 with DateFormat

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

the class DateFormatTest method tryPat994.

// internal test subroutine, used by TestPartialParse994
public void tryPat994(SimpleDateFormat format, String pat, String str, Date expected) {
    Date Null = null;
    logln("Pattern \"" + pat + "\"   String \"" + str + "\"");
    try {
        format.applyPattern(pat);
        Date date = format.parse(str);
        String f = ((DateFormat) format).format(date);
        logln(" parse(" + str + ") -> " + date);
        logln(" format -> " + f);
        if (expected.equals(Null) || !date.equals(expected))
            // " + expected);
            errln("FAIL: Expected null");
        if (!f.equals(str))
            errln("FAIL: Expected " + str);
    } catch (ParseException e) {
        logln("ParseException: " + e.getMessage());
        if (!(expected == Null))
            errln("FAIL: Expected " + expected);
    } catch (Exception e) {
        errln("*** Exception:");
        e.printStackTrace();
    }
}
Also used : DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParseException(java.text.ParseException) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 39 with DateFormat

use of android.icu.text.DateFormat 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 40 with DateFormat

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

the class IntlTestDateFormatAPI method TestAPI.

// This test checks various generic API methods in DateFormat to achieve 100% API coverage.
@Test
public void TestAPI() {
    logln("DateFormat API test---");
    logln("");
    Locale.setDefault(Locale.ENGLISH);
    // ======= Test constructors
    logln("Testing DateFormat constructors");
    DateFormat def = DateFormat.getInstance();
    DateFormat fr = DateFormat.getTimeInstance(DateFormat.FULL, Locale.FRENCH);
    DateFormat it = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALIAN);
    DateFormat de = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.GERMAN);
    // ======= Test equality
    logln("Testing equality operator");
    if (fr.equals(it)) {
        errln("ERROR: equals failed");
    }
    // ======= Test various format() methods
    logln("Testing various format() methods");
    Date d = new Date((long) 837039928046.0);
    StringBuffer res1 = new StringBuffer();
    StringBuffer res2 = new StringBuffer();
    String res3 = new String();
    FieldPosition pos1 = new FieldPosition(0);
    FieldPosition pos2 = new FieldPosition(0);
    res1 = fr.format(d, res1, pos1);
    logln("" + d.getTime() + " formatted to " + res1);
    res2 = it.format(d, res2, pos2);
    logln("" + d.getTime() + " formatted to " + res2);
    res3 = de.format(d);
    logln("" + d.getTime() + " formatted to " + res3);
    // ======= Test parse()
    logln("Testing parse()");
    String text = new String("02/03/76, 2:50 AM, CST");
    Object result1 = new Date();
    Date result2 = new Date();
    Date result3 = new Date();
    ParsePosition pos = new ParsePosition(0);
    ParsePosition pos01 = new ParsePosition(0);
    result1 = def.parseObject(text, pos);
    if (result1 == null) {
        errln("ERROR: parseObject() failed for " + text);
    }
    logln(text + " parsed into " + ((Date) result1).getTime());
    try {
        result2 = def.parse(text);
    } catch (ParseException e) {
        errln("ERROR: parse() failed");
    }
    logln(text + " parsed into " + result2.getTime());
    result3 = def.parse(text, pos01);
    if (result3 == null) {
        errln("ERROR: parse() failed for " + text);
    }
    logln(text + " parsed into " + result3.getTime());
    // ======= Test getters and setters
    logln("Testing getters and setters");
    final Locale[] locales = DateFormat.getAvailableLocales();
    long count = locales.length;
    logln("Got " + count + " locales");
    // Ticket #6280, #8078 and #11674
    // These locales should be included in the result
    boolean missingLocaleNotFatal = TestUtil.getJavaVendor() == JavaVendor.Android || TestUtil.getJavaVersion() >= 7;
    final Locale[] samples = { new Locale("zh", "CN"), new Locale("zh", "TW"), new Locale("zh", "HK"), new Locale("sr", "RS") };
    boolean[] available = new boolean[samples.length];
    for (int i = 0; i < count; i++) {
        String name;
        name = locales[i].getDisplayName();
        logln(name);
        for (int j = 0; j < samples.length; j++) {
            if (locales[i].equals(samples[j])) {
                available[j] = true;
                break;
            }
        }
    }
    for (int i = 0; i < available.length; i++) {
        if (!available[i]) {
            if (missingLocaleNotFatal) {
                // Java 7 supports script field, so zh_Hans_CN is included
                // in the available locale list.
                logln("INFO: missing Locale: " + samples[i]);
            } else {
                errln("ERROR: missing Locale: " + samples[i]);
            }
        }
    }
    fr.setLenient(it.isLenient());
    if (fr.isLenient() != it.isLenient()) {
        errln("ERROR: setLenient() failed");
    }
    final Calendar cal = def.getCalendar();
    Calendar newCal = (Calendar) cal.clone();
    de.setCalendar(newCal);
    it.setCalendar(newCal);
    if (!de.getCalendar().equals(it.getCalendar())) {
        errln("ERROR: set Calendar() failed");
    }
    final NumberFormat nf = def.getNumberFormat();
    NumberFormat newNf = (NumberFormat) nf.clone();
    de.setNumberFormat(newNf);
    it.setNumberFormat(newNf);
    if (!de.getNumberFormat().equals(it.getNumberFormat())) {
        errln("ERROR: set NumberFormat() failed");
    }
    final TimeZone tz = def.getTimeZone();
    TimeZone newTz = (TimeZone) tz.clone();
    de.setTimeZone(newTz);
    it.setTimeZone(newTz);
    if (!de.getTimeZone().equals(it.getTimeZone())) {
        errln("ERROR: set TimeZone() failed");
    }
// ======= Test getStaticClassID()
// logln("Testing instanceof()");
// try {
// DateFormat test = new SimpleDateFormat();
// if (! (test instanceof SimpleDateFormat)) {
// errln("ERROR: instanceof failed");
// }
// }
// catch (Exception e) {
// errln("ERROR: Couldn't create a DateFormat");
// }
}
Also used : Locale(java.util.Locale) Calendar(android.icu.util.Calendar) FieldPosition(java.text.FieldPosition) Date(java.util.Date) TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) ParseException(java.text.ParseException) ParsePosition(java.text.ParsePosition) NumberFormat(android.icu.text.NumberFormat) 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