Search in sources :

Example 76 with DateFormat

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

the class DateFormatRegressionTest method TestDangiFormat.

// Date formatting with Dangi calendar in en locale (#9987)
@Test
public void TestDangiFormat() {
    DateFormat fmt = DateFormat.getDateInstance(DateFormat.MEDIUM, new ULocale("en@calendar=dangi"));
    String calType = fmt.getCalendar().getType();
    assertEquals("Incorrect calendar type used by the date format instance", "dangi", calType);
    GregorianCalendar gcal = new GregorianCalendar();
    gcal.set(2013, Calendar.MARCH, 1, 0, 0, 0);
    Date d = gcal.getTime();
    String dangiDateStr = fmt.format(d);
    assertEquals("Bad date format", "Mo1 20, 2013", dangiDateStr);
}
Also used : ULocale(android.icu.util.ULocale) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 77 with DateFormat

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

the class DateFormatRegressionTest method Test4052408.

/**
 * @bug 4052408
 */
@Test
public void Test4052408() {
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US);
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(97 + 1900, Calendar.MAY, 3, 8, 55);
    Date dt = cal.getTime();
    String str = fmt.format(dt);
    logln(str);
    if (!str.equals("5/3/97, 8:55 AM"))
        errln("Fail: Test broken; Want 5/3/97, 8:55 AM Got " + str);
    String[] expected = { // "ERA_FIELD",
    "", // "YEAR_FIELD",
    "97", // "MONTH_FIELD",
    "5", // "DATE_FIELD",
    "3", // "HOUR_OF_DAY1_FIELD",
    "", // "HOUR_OF_DAY0_FIELD",
    "", // "MINUTE_FIELD",
    "55", // "SECOND_FIELD",
    "", // "MILLISECOND_FIELD",
    "", // "DAY_OF_WEEK_FIELD",
    "", // "DAY_OF_YEAR_FIELD",
    "", // "DAY_OF_WEEK_IN_MONTH_FIELD",
    "", // "WEEK_OF_YEAR_FIELD",
    "", // "WEEK_OF_MONTH_FIELD",
    "", // "AM_PM_FIELD",
    "AM", // "HOUR1_FIELD",
    "8", // "HOUR0_FIELD",
    "", // "TIMEZONE_FIELD"
    "" };
    String[] fieldNames = { "ERA_FIELD", "YEAR_FIELD", "MONTH_FIELD", "DATE_FIELD", "HOUR_OF_DAY1_FIELD", "HOUR_OF_DAY0_FIELD", "MINUTE_FIELD", "SECOND_FIELD", "MILLISECOND_FIELD", "DAY_OF_WEEK_FIELD", "DAY_OF_YEAR_FIELD", "DAY_OF_WEEK_IN_MONTH_FIELD", "WEEK_OF_YEAR_FIELD", "WEEK_OF_MONTH_FIELD", "AM_PM_FIELD", "HOUR1_FIELD", "HOUR0_FIELD", "TIMEZONE_FIELD" };
    boolean pass = true;
    for (int i = 0; i <= 17; ++i) {
        FieldPosition pos = new FieldPosition(i);
        StringBuffer buf = new StringBuffer("");
        fmt.format(dt, buf, pos);
        // char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()];
        String dst = buf.substring(pos.getBeginIndex(), pos.getEndIndex());
        str = dst;
        log(i + ": " + fieldNames[i] + ", \"" + str + "\", " + pos.getBeginIndex() + ", " + pos.getEndIndex());
        String exp = expected[i];
        if ((exp.length() == 0 && str.length() == 0) || str.equals(exp))
            logln(" ok");
        else {
            logln(" expected " + exp);
            pass = false;
        }
    }
    if (!pass)
        errln("Fail: FieldPosition not set right by DateFormat");
}
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) FieldPosition(java.text.FieldPosition) Date(java.util.Date) Test(org.junit.Test)

Example 78 with DateFormat

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

the class DateFormatRegressionTest method TestT5683.

// Ticket#5683
// Some ICU4J 3.6 data files contain garbage data which prevent the code to resolve another
// bundle as an alias.  zh_TW should be equivalent to zh_Hant_TW
@Test
public void TestT5683() {
    Locale[] aliasLocales = { new Locale("zh", "CN"), new Locale("zh", "TW"), new Locale("zh", "HK"), new Locale("zh", "SG"), new Locale("zh", "MO") };
    ULocale[] canonicalLocales = { new ULocale("zh_Hans_CN"), new ULocale("zh_Hant_TW"), new ULocale("zh_Hant_HK"), new ULocale("zh_Hans_SG"), new ULocale("zh_Hant_MO") };
    Date d = new Date(0);
    for (int i = 0; i < aliasLocales.length; i++) {
        DateFormat dfAlias = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, aliasLocales[i]);
        DateFormat dfCanonical = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, canonicalLocales[i]);
        String sAlias = dfAlias.format(d);
        String sCanonical = dfCanonical.format(d);
        if (!sAlias.equals(sCanonical)) {
            errln("Fail: The format result for locale " + aliasLocales[i] + " is different from the result for locale " + canonicalLocales[i] + ": " + sAlias + "[" + aliasLocales[i] + "] / " + sCanonical + "[" + canonicalLocales[i] + "]");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) ULocale(android.icu.util.ULocale) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 79 with DateFormat

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

the class DateFormatRegressionTest method Test714.

@Test
public void Test714() {
    // TimeZone Offset
    TimeZone defaultTZ = TimeZone.getDefault();
    TimeZone PST = TimeZone.getTimeZone("PST");
    int defaultOffset = defaultTZ.getRawOffset();
    int PSTOffset = PST.getRawOffset();
    Date d = new Date(978103543000l - (defaultOffset - PSTOffset));
    d = new Date(d.getTime() - (defaultTZ.inDaylightTime(d) ? 3600000 : 0));
    DateFormat fmt = DateFormat.getDateTimeInstance(-1, DateFormat.MEDIUM, Locale.US);
    String tests = "7:25:43 AM";
    String s = fmt.format(d);
    if (!s.equals(tests)) {
        errln("Fail: " + s + " != " + tests);
    } else {
        logln("OK: " + s + " == " + tests);
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 80 with DateFormat

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

the class DateFormatRegressionTest method Test4060212.

/**
 * @bug 4060212
 */
@Test
public void Test4060212() {
    String dateString = "1995-040.05:01:29";
    logln("dateString= " + dateString);
    logln("Using yyyy-DDD.hh:mm:ss");
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-DDD.hh:mm:ss");
    ParsePosition pos = new ParsePosition(0);
    Date myDate = formatter.parse(dateString, pos);
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.LONG);
    String myString = fmt.format(myDate);
    logln(myString);
    Calendar cal = new GregorianCalendar();
    cal.setTime(myDate);
    if ((cal.get(Calendar.DAY_OF_YEAR) != 40))
        errln("Fail: Got " + cal.get(Calendar.DAY_OF_YEAR) + " Want 40");
    logln("Using yyyy-ddd.hh:mm:ss");
    formatter = new SimpleDateFormat("yyyy-ddd.hh:mm:ss");
    pos.setIndex(0);
    myDate = formatter.parse(dateString, pos);
    myString = fmt.format(myDate);
    logln(myString);
    cal.setTime(myDate);
    if ((cal.get(Calendar.DAY_OF_YEAR) != 40))
        errln("Fail: Got " + cal.get(Calendar.DAY_OF_YEAR) + " Want 40");
}
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) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) 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