Search in sources :

Example 21 with DateFormat

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

the class DateFormatRegressionTest method Test4073003.

/* The java.text.DateFormat.parse(String) method expects for the
      US locale a string formatted according to mm/dd/yy and parses it
      correctly.
    
      When given a string mm/dd/yyyy it only parses up to the first
      two y's, typically resulting in a date in the year 1919.
      
      Please extend the parsing method(s) to handle strings with
      four-digit year values (probably also applicable to various
      other locales.  */
/**
 * @bug 4073003
 */
@Test
public void Test4073003() {
    try {
        DateFormat fmt = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
        String[] tests = { "12/25/61", "12/25/1961", "4/3/2010", "4/3/10" };
        for (int i = 0; i < 4; i += 2) {
            Date d = fmt.parse(tests[i]);
            Date dd = fmt.parse(tests[i + 1]);
            String s;
            s = fmt.format(d);
            String ss;
            ss = fmt.format(dd);
            if (d.getTime() != dd.getTime())
                errln("Fail: " + d + " != " + dd);
            if (!s.equals(ss))
                errln("Fail: " + s + " != " + ss);
            logln("Ok: " + s + " " + d);
        }
    } catch (ParseException e) {
        errln("Fail: " + e);
        e.printStackTrace();
    }
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParseException(java.text.ParseException) Date(java.util.Date) Test(org.junit.Test)

Example 22 with DateFormat

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

the class DateFormatRegressionTestJ method Test4213086.

// DateFormat getDateTimeInstance(int, int), invalid styles no exception
@Test
public void Test4213086() {
    Date someDate = new Date();
    String d = null;
    try {
        DateFormat df2 = DateFormat.getDateTimeInstance(2, -2);
        d = df2.format(someDate);
        errln("we should catch an exception here");
    } catch (Exception e) {
        logln("dateStyle = 2" + "\t timeStyle = -2");
        logln("Exception caught!");
    }
    try {
        DateFormat df3 = DateFormat.getDateTimeInstance(4, 2);
        d = df3.format(someDate);
        errln("we should catch an exception here");
    } catch (Exception e) {
        logln("dateStyle = 4" + "\t timeStyle = 2");
        logln("Exception caught!");
        logln("********************************************");
    }
    try {
        DateFormat df4 = DateFormat.getDateTimeInstance(-12, -12);
        d = df4.format(someDate);
        errln("we should catch an exception here");
    } catch (Exception e) {
        logln("dateStyle = -12" + "\t timeStyle = -12");
        logln("Exception caught!");
        logln("********************************************");
    }
    try {
        DateFormat df5 = DateFormat.getDateTimeInstance(2, 123);
        d = df5.format(someDate);
        errln("we should catch an exception here");
    } catch (Exception e) {
        logln("dateStyle = 2" + "\t timeStyle = 123");
        logln("Exception caught!");
        logln("********************************************");
    }
    // read the value in d to get rid of the warning
    if (d != null) {
        logln("The value of d: " + d);
    }
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) Date(java.util.Date) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 23 with DateFormat

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

the class DateFormatRegressionTestJ method Test4250359.

// DateFormat.format works wrongly?
@Test
public void Test4250359() {
    Locale.setDefault(Locale.US);
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(101 + 1900, 9, 9, 17, 53);
    Date d = cal.getTime();
    DateFormat tf = DateFormat.getTimeInstance(DateFormat.SHORT);
    String act_result = tf.format(d);
    String exp_result = "5:53 PM";
    if (!act_result.equals(exp_result)) {
        errln("The result is not expected");
    }
}
Also used : Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 24 with DateFormat

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

the class TimeZoneOffsetLocalTest method TestGetOffsetAroundTransition.

/*
     * Testing getOffset APIs around rule transition by local standard/wall time.
     */
@Test
public void TestGetOffsetAroundTransition() {
    final int HOUR = 60 * 60 * 1000;
    final int MINUTE = 60 * 1000;
    int[][] DATES = { { 2006, Calendar.APRIL, 2, 1, 30, 1 * HOUR + 30 * MINUTE }, { 2006, Calendar.APRIL, 2, 2, 00, 2 * HOUR }, { 2006, Calendar.APRIL, 2, 2, 30, 2 * HOUR + 30 * MINUTE }, { 2006, Calendar.APRIL, 2, 3, 00, 3 * HOUR }, { 2006, Calendar.APRIL, 2, 3, 30, 3 * HOUR + 30 * MINUTE }, { 2006, Calendar.OCTOBER, 29, 0, 30, 0 * HOUR + 30 * MINUTE }, { 2006, Calendar.OCTOBER, 29, 1, 00, 1 * HOUR }, { 2006, Calendar.OCTOBER, 29, 1, 30, 1 * HOUR + 30 * MINUTE }, { 2006, Calendar.OCTOBER, 29, 2, 00, 2 * HOUR }, { 2006, Calendar.OCTOBER, 29, 2, 30, 2 * HOUR + 30 * MINUTE } };
    // Expected offsets by getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds)
    int[] OFFSETS1 = { // April 2, 2006
    -8 * HOUR, -7 * HOUR, -7 * HOUR, -7 * HOUR, -7 * HOUR, // October 29, 2006
    -7 * HOUR, -8 * HOUR, -8 * HOUR, -8 * HOUR, -8 * HOUR };
    // Expected offsets by getOffset(long time, boolean local, int[] offsets) with local = true
    // or getOffsetFromLocal(long time, int nonExistingTimeOpt, int duplicatedTimeOpt, int[] offsets)
    // with nonExistingTimeOpt = LOCAL_STD/duplicatedTimeOpt = LOCAL_STD
    int[][] OFFSETS2 = { // April 2, 2006
    { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, // Oct 29, 2006
    { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 } };
    // Expected offsets by getOffsetFromLocal(long time, int nonExistingTimeOpt, int duplicatedTimeOpt, int[] offsets)
    // with nonExistingTimeOpt = LOCAL_DST/duplicatedTimeOpt = LOCAL_DST
    int[][] OFFSETS3 = { // April 2, 2006
    { -8 * HOUR, 0 }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, // October 29, 2006
    { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 1 * HOUR }, { -8 * HOUR, 0 }, { -8 * HOUR, 0 } };
    int[] offsets = new int[2];
    TimeZone utc = TimeZone.getTimeZone("UTC");
    Calendar cal = Calendar.getInstance(utc);
    cal.clear();
    // Set up TimeZone objects - OlsonTimeZone, SimpleTimeZone and RuleBasedTimeZone
    BasicTimeZone[] TESTZONES = new BasicTimeZone[3];
    TESTZONES[0] = (BasicTimeZone) TimeZone.getTimeZone("America/Los_Angeles", TimeZone.TIMEZONE_ICU);
    TESTZONES[1] = new SimpleTimeZone(-8 * HOUR, "Simple Pacific Time", Calendar.APRIL, 1, Calendar.SUNDAY, 2 * HOUR, Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * HOUR);
    InitialTimeZoneRule ir = new InitialTimeZoneRule(// Initial time Name
    "Pacific Standard Time", // Raw offset
    -8 * HOUR, // DST saving amount
    0 * HOUR);
    RuleBasedTimeZone rbPT = new RuleBasedTimeZone("Rule based Pacific Time", ir);
    DateTimeRule dtr;
    AnnualTimeZoneRule atzr;
    final int STARTYEAR = 2000;
    dtr = new DateTimeRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * HOUR, // 1st Sunday in April, at 2AM wall time
    DateTimeRule.WALL_TIME);
    atzr = new AnnualTimeZoneRule("Pacific Daylight Time", -8 * HOUR, /* rawOffset */
    1 * HOUR, /* dstSavings */
    dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
    rbPT.addTransitionRule(atzr);
    dtr = new DateTimeRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * HOUR, // last Sunday in October, at 2AM wall time
    DateTimeRule.WALL_TIME);
    atzr = new AnnualTimeZoneRule("Pacific Standard Time", -8 * HOUR, /* rawOffset */
    0, /* dstSavings */
    dtr, STARTYEAR, AnnualTimeZoneRule.MAX_YEAR);
    rbPT.addTransitionRule(atzr);
    TESTZONES[2] = rbPT;
    // Calculate millis
    long[] MILLIS = new long[DATES.length];
    for (int i = 0; i < DATES.length; i++) {
        cal.clear();
        cal.set(DATES[i][0], DATES[i][1], DATES[i][2], DATES[i][3], DATES[i][4]);
        MILLIS[i] = cal.getTimeInMillis();
    }
    DateFormat df = DateFormat.getInstance();
    df.setTimeZone(utc);
    // Test getOffset(int era, int year, int month, int day, int dayOfWeek, int millis)
    for (int i = 0; i < TESTZONES.length; i++) {
        for (int d = 0; d < DATES.length; d++) {
            int offset = TESTZONES[i].getOffset(GregorianCalendar.AD, DATES[d][0], DATES[d][1], DATES[d][2], Calendar.SUNDAY, DATES[d][5]);
            if (offset != OFFSETS1[d]) {
                errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[d])) + "(standard) - Got: " + offset + " Expected: " + OFFSETS1[d]);
            }
        }
    }
    // Test getOffset(long time, boolean local, int[] offsets) with local=true
    for (int i = 0; i < TESTZONES.length; i++) {
        for (int m = 0; m < MILLIS.length; m++) {
            TESTZONES[i].getOffset(MILLIS[m], true, offsets);
            if (offsets[0] != OFFSETS2[m][0] || offsets[1] != OFFSETS2[m][1]) {
                errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS2[m][0] + "/" + OFFSETS2[m][1]);
            }
        }
    }
    // with nonExistingTimeOpt = LOCAL_STD/duplicatedTimeOpt = LOCAL_STD
    for (int i = 0; i < TESTZONES.length; i++) {
        for (int m = 0; m < MILLIS.length; m++) {
            TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_STD, BasicTimeZone.LOCAL_STD, offsets);
            if (offsets[0] != OFFSETS2[m][0] || offsets[1] != OFFSETS2[m][1]) {
                errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/STD/STD) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS2[m][0] + "/" + OFFSETS2[m][1]);
            }
        }
    }
    // with nonExistingTimeOpt = LOCAL_DST/duplicatedTimeOpt = LOCAL_DST
    for (int i = 0; i < TESTZONES.length; i++) {
        for (int m = 0; m < MILLIS.length; m++) {
            TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_DST, BasicTimeZone.LOCAL_DST, offsets);
            if (offsets[0] != OFFSETS3[m][0] || offsets[1] != OFFSETS3[m][1]) {
                errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/DST/DST) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS3[m][0] + "/" + OFFSETS3[m][1]);
            }
        }
    }
    // with nonExistingTimeOpt = LOCAL_FORMER/duplicatedTimeOpt = LOCAL_LATTER
    for (int i = 0; i < TESTZONES.length; i++) {
        for (int m = 0; m < MILLIS.length; m++) {
            TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_FORMER, BasicTimeZone.LOCAL_LATTER, offsets);
            if (offsets[0] != OFFSETS2[m][0] || offsets[1] != OFFSETS2[m][1]) {
                errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/FORMER/LATTER) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS2[m][0] + "/" + OFFSETS2[m][1]);
            }
        }
    }
    // with nonExistingTimeOpt = LOCAL_LATTER/duplicatedTimeOpt = LOCAL_FORMER
    for (int i = 0; i < TESTZONES.length; i++) {
        for (int m = 0; m < MILLIS.length; m++) {
            TESTZONES[i].getOffsetFromLocal(MILLIS[m], BasicTimeZone.LOCAL_LATTER, BasicTimeZone.LOCAL_FORMER, offsets);
            if (offsets[0] != OFFSETS3[m][0] || offsets[1] != OFFSETS3[m][1]) {
                errln("Bad offset returned by " + TESTZONES[i].getID() + " at " + df.format(new Date(MILLIS[m])) + "(wall/LATTER/FORMER) - Got: " + offsets[0] + "/" + offsets[1] + " Expected: " + OFFSETS3[m][0] + "/" + OFFSETS3[m][1]);
            }
        }
    }
}
Also used : InitialTimeZoneRule(android.icu.util.InitialTimeZoneRule) AnnualTimeZoneRule(android.icu.util.AnnualTimeZoneRule) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) Date(java.util.Date) DateTimeRule(android.icu.util.DateTimeRule) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) RuleBasedTimeZone(android.icu.util.RuleBasedTimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) DateFormat(android.icu.text.DateFormat) Test(org.junit.Test)

Example 25 with DateFormat

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

the class IBMCalendarTest method TestJB4541.

// Tests for jb 4541
@Test
public void TestJB4541() {
    ULocale loc = new ULocale("en_US");
    // !!! Shouldn't we have an api like this?
    // !!! Question: should this reflect those actually available in this copy of ICU, or
    // the list of types we assume is available?
    // String[] calTypes = Calendar.getAvailableTypes();
    final String[] calTypes = { "buddhist", "chinese", "coptic", "ethiopic", "gregorian", "hebrew", "islamic", "islamic-civil", "japanese", "roc" };
    // constructing a DateFormat with a locale indicating a calendar type should construct a
    // date format appropriate to that calendar
    final Date time = new Date();
    for (int i = 0; i < calTypes.length; ++i) {
        ULocale aLoc = loc.setKeywordValue("calendar", calTypes[i]);
        logln("locale: " + aLoc);
        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, aLoc);
        logln("df type: " + df.getClass().getName() + " loc: " + df.getLocale(ULocale.VALID_LOCALE));
        Calendar cal = df.getCalendar();
        assertEquals("calendar types", cal.getType(), calTypes[i]);
        DateFormat df2 = cal.getDateTimeFormat(DateFormat.FULL, DateFormat.FULL, ULocale.US);
        logln("df2 type: " + df2.getClass().getName() + " loc: " + df2.getLocale(ULocale.VALID_LOCALE));
        assertEquals("format results", df.format(time), df2.format(time));
    }
    // dateFormat.setCalendar should throw exception if wrong format for calendar
    if (false) {
        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, new ULocale("en_US@calendar=chinese"));
        logln("dateformat type: " + df.getClass().getName());
        Calendar cal = Calendar.getInstance(new ULocale("en_US@calendar=chinese"));
        logln("calendar type: " + cal.getClass().getName());
    }
}
Also used : ULocale(android.icu.util.ULocale) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) ChineseCalendar(android.icu.util.ChineseCalendar) TaiwanCalendar(android.icu.util.TaiwanCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) BuddhistCalendar(android.icu.util.BuddhistCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) 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