Search in sources :

Example 21 with SimpleDateFormat

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

the class ChineseTest method Test6510.

@Test
public void Test6510() {
    Calendar gregorianCalendar;
    ChineseCalendar chineseCalendar, chineseCalendar2;
    ChineseDateFormat dateFormat;
    SimpleDateFormat simpleDateFormat;
    simpleDateFormat = new android.icu.text.SimpleDateFormat("MM/dd/yyyy G 'at' HH:mm:ss vvvv", Locale.US);
    dateFormat = new android.icu.text.ChineseDateFormat("MM/dd/yyyy(G) HH:mm:ss", Locale.CHINA);
    // lunar to gregorian
    chineseCalendar = new ChineseCalendar(77, 26, Calendar.JANUARY, 0, 6, 0, 0, 0);
    // coverage
    assertEquals("equivalent ChineseCalendar() constructors", chineseCalendar, new ChineseCalendar(77, 26, Calendar.JANUARY, 0, 6));
    gregorianCalendar = Calendar.getInstance(Locale.US);
    gregorianCalendar.setTime(chineseCalendar.getTime());
    // gregorian to lunar
    chineseCalendar2 = new ChineseCalendar();
    chineseCalendar2.setTimeInMillis(gregorianCalendar.getTimeInMillis());
    // validate roundtrip
    if (chineseCalendar.getTimeInMillis() != chineseCalendar2.getTimeInMillis()) {
        errln("time1: " + chineseCalendar.getTimeInMillis());
        errln("time2: " + chineseCalendar2.getTimeInMillis());
        errln("Lunar [MM/dd/y(G) HH:mm:ss] " + dateFormat.format(chineseCalendar));
        errln("**PROBLEM Grego [MM/dd/y(G) HH:mm:ss] " + simpleDateFormat.format(gregorianCalendar));
        errln("Grego [MM/dd/y(G) HH:mm:ss] " + simpleDateFormat.format(gregorianCalendar));
        errln("Lunar [MM/dd/y(G) HH:mm:ss] " + dateFormat.format(chineseCalendar2));
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ChineseDateFormat(android.icu.text.ChineseDateFormat) ChineseCalendar(android.icu.util.ChineseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 22 with SimpleDateFormat

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

the class JapaneseTest method Test3860.

@Test
public void Test3860() {
    ULocale loc = new ULocale("ja_JP@calendar=japanese");
    Calendar cal = new JapaneseCalendar(loc);
    DateFormat enjformat = cal.getDateTimeFormat(0, 0, new ULocale("en_JP@calendar=japanese"));
    DateFormat format = cal.getDateTimeFormat(0, 0, loc);
    // Note: just 'y' doesn't work here.
    ((SimpleDateFormat) format).applyPattern("y.M.d");
    ParsePosition pos = new ParsePosition(0);
    // after the start of heisei accession.  Jan 1, 1H wouldn't work  because it is actually showa 64
    Date aDate = format.parse("1.1.9", pos);
    String inEn = enjformat.format(aDate);
    cal.clear();
    cal.setTime(aDate);
    int gotYear = cal.get(Calendar.YEAR);
    int gotEra = cal.get(Calendar.ERA);
    int expectYear = 1;
    int expectEra = JapaneseCalendar.CURRENT_ERA;
    if ((gotYear != expectYear) || (gotEra != expectEra)) {
        errln("Expected year " + expectYear + ", era " + expectEra + ", but got year " + gotYear + " and era " + gotEra + ", == " + inEn);
    } else {
        logln("Got year " + gotYear + " and era " + gotEra + ", == " + inEn);
    }
    // Test parse with missing era (should default to current era, heisei)
    // Test parse with incomplete information
    logln("Testing parse w/ just year...");
    Calendar cal2 = new JapaneseCalendar(loc);
    SimpleDateFormat fmt = new SimpleDateFormat("y", loc);
    SimpleDateFormat fmt2 = new SimpleDateFormat("HH:mm:ss.S MMMM d, yyyy G", new ULocale("en_US@calendar=gregorian"));
    cal2.clear();
    String samplestr = "1";
    logln("Test Year: " + samplestr);
    try {
        aDate = fmt.parse(samplestr);
    } catch (ParseException pe) {
        errln("Error parsing " + samplestr);
    }
    ParsePosition pp = new ParsePosition(0);
    fmt.parse(samplestr, cal2, pp);
    logln("cal2 after 1 parse:");
    String str = fmt2.format(aDate);
    logln("as Gregorian Calendar: " + str);
    cal2.setTime(aDate);
    gotYear = cal2.get(Calendar.YEAR);
    gotEra = cal2.get(Calendar.ERA);
    expectYear = 1;
    expectEra = JapaneseCalendar.CURRENT_ERA;
    if ((gotYear != 1) || (gotEra != expectEra)) {
        errln("parse " + samplestr + " of 'y' as Japanese Calendar, expected year " + expectYear + " and era " + expectEra + ", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str + ")");
    } else {
        logln(" year: " + gotYear + ", era: " + gotEra);
    }
}
Also used : ULocale(android.icu.util.ULocale) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 23 with SimpleDateFormat

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

the class JapaneseTest method Test5345parse.

@Test
public void Test5345parse() {
    // Test parse with incomplete information
    // DateFormat.LONG, Locale.US);
    DateFormat fmt2 = DateFormat.getDateInstance();
    JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault(), new ULocale("en_US"));
    SimpleDateFormat fmt = (SimpleDateFormat) c.getDateTimeFormat(1, 1, new ULocale("en_US@calendar=japanese"));
    fmt.applyPattern("G y");
    logln("fmt's locale = " + fmt.getLocale(ULocale.ACTUAL_LOCALE));
    // SimpleDateFormat fmt = new SimpleDateFormat("G y", new Locale("en_US@calendar=japanese"));
    // 1868-09-08 00:00 Pacific Time (GMT-07:52:58)
    long aDateLong = -3197117222000L;
    if (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) {
        // Java time zone implementation does not support LMTs
        // 1868-09-08 00:00 Pacific Time (GMT-08:00)
        aDateLong = -3197116800000L;
    }
    Date aDate = new Date(aDateLong);
    logln("aDate: " + aDate.toString() + ", from " + aDateLong);
    String str;
    str = fmt2.format(aDate);
    logln("Test Date: " + str);
    str = fmt.format(aDate);
    logln("as Japanese Calendar: " + str);
    String expected = "Meiji 1";
    if (!str.equals(expected)) {
        errln("FAIL: Expected " + expected + " but got " + str);
    }
    Date otherDate;
    try {
        otherDate = fmt.parse(expected);
        if (!otherDate.equals(aDate)) {
            String str3;
            // ParsePosition pp;
            Date dd = fmt.parse(expected);
            str3 = fmt.format(otherDate);
            long oLong = otherDate.getTime();
            long aLong = otherDate.getTime();
            errln("FAIL: Parse incorrect of " + expected + ":  wanted " + aDate + " (" + aLong + "), but got " + " " + otherDate + " (" + oLong + ") = " + str3 + " not " + dd.toString());
        } else {
            logln("Parsed OK: " + expected);
        }
    } catch (java.text.ParseException pe) {
        errln("FAIL: ParseException: " + pe.toString());
        pe.printStackTrace();
    }
}
Also used : ULocale(android.icu.util.ULocale) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 24 with SimpleDateFormat

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

the class DateFormatRegressionTest method Test4104522.

/**
 * @bug 4104522
 * CANNOT REPRODUCE
 * According to the bug report, this test should throw a
 * StringIndexOutOfBoundsException during the second parse.  However,
 * this is not seen.
 */
@Test
public void Test4104522() {
    SimpleDateFormat sdf = new SimpleDateFormat();
    String pattern = "'time' hh:mm";
    sdf.applyPattern(pattern);
    logln("pattern: \"" + pattern + "\"");
    // works correctly
    ParsePosition pp = new ParsePosition(0);
    String text = "time ";
    Date dt = sdf.parse(text, pp);
    logln(" text: \"" + text + "\"" + " date: " + dt);
    // works wrong
    pp.setIndex(0);
    text = "time";
    dt = sdf.parse(text, pp);
    logln(" text: \"" + text + "\"" + " date: " + dt);
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 25 with SimpleDateFormat

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

the class DateFormatRegressionTest method Test4182066.

/**
 * DateFormat shouldn't parse year "-1" as a two-digit year (e.g., "-1" . 1999).
 */
@Test
public void Test4182066() {
    SimpleDateFormat fmt = new SimpleDateFormat("MM/dd/yy", Locale.US);
    SimpleDateFormat dispFmt = new SimpleDateFormat("MMM dd yyyy HH:mm:ss GG", Locale.US);
    /* We expect 2-digit year formats to put 2-digit years in the right
         * window.  Out of range years, that is, anything less than "00" or
         * greater than "99", are treated as literal years.  So "1/2/3456"
         * becomes 3456 AD.  Likewise, "1/2/-3" becomes -3 AD == 2 BC.
         */
    final String[] STRINGS = { "02/29/00", "01/23/01", "04/05/-1", "01/23/-9", "11/12/1314", "10/31/1", "09/12/+1", "09/12/001" };
    int STRINGS_COUNT = STRINGS.length;
    Calendar cal = Calendar.getInstance();
    Date FAIL_DATE = cal.getTime();
    cal.clear();
    cal.set(2000, Calendar.FEBRUARY, 29);
    Date d0 = cal.getTime();
    cal.clear();
    cal.set(2001, Calendar.JANUARY, 23);
    Date d1 = cal.getTime();
    cal.clear();
    cal.set(-1, Calendar.APRIL, 5);
    Date d2 = cal.getTime();
    cal.clear();
    cal.set(-9, Calendar.JANUARY, 23);
    Date d3 = cal.getTime();
    cal.clear();
    cal.set(1314, Calendar.NOVEMBER, 12);
    Date d4 = cal.getTime();
    cal.clear();
    cal.set(1, Calendar.OCTOBER, 31);
    Date d5 = cal.getTime();
    cal.clear();
    cal.set(1, Calendar.SEPTEMBER, 12);
    Date d7 = cal.getTime();
    Date[] DATES = { d0, d1, d2, d3, d4, d5, FAIL_DATE, d7 };
    String out = "";
    boolean pass = true;
    for (int i = 0; i < STRINGS_COUNT; ++i) {
        String str = STRINGS[i];
        Date expected = DATES[i];
        Date actual = null;
        try {
            actual = fmt.parse(str);
        } catch (ParseException e) {
            actual = FAIL_DATE;
        }
        String actStr = "";
        if ((actual.getTime()) == FAIL_DATE.getTime()) {
            actStr += "null";
        } else {
            // Yuck: See j25
            actStr = ((DateFormat) dispFmt).format(actual);
        }
        if (expected.getTime() == (actual.getTime())) {
            out += str + " => " + actStr + "\n";
        } else {
            String expStr = "";
            if (expected.getTime() == FAIL_DATE.getTime()) {
                expStr += "null";
            } else {
                // Yuck: See j25
                expStr = ((DateFormat) dispFmt).format(expected);
            }
            out += "FAIL: " + str + " => " + actStr + ", expected " + expStr + "\n";
            pass = false;
        }
    }
    if (pass) {
        log(out);
    } else {
        err(out);
    }
}
Also used : IslamicCalendar(android.icu.util.IslamicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) 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