Search in sources :

Example 81 with DateFormat

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

the class DateFormatRoundTripTest method _test.

private void _test(Locale loc) {
    if (!INFINITE) {
        logln("Locale: " + loc.getDisplayName());
    }
    // Total possibilities = 24
    // 4 date
    // 4 time
    // 16 date-time
    boolean[] TEST_TABLE = new boolean[24];
    int i = 0;
    for (i = 0; i < 24; ++i) TEST_TABLE[i] = true;
    // test time by eliminating some tests, up to 23.
    for (i = 0; i < SPARSENESS; i++) {
        int random = (int) (ran.nextDouble() * 24);
        if (random >= 0 && random < 24 && TEST_TABLE[i]) {
            TEST_TABLE[random] = false;
        }
    }
    int itable = 0;
    int style = 0;
    for (style = DateFormat.FULL; style <= DateFormat.SHORT; ++style) {
        if (TEST_TABLE[itable++]) {
            logln("Testing style " + styleName(style));
            DateFormat df = DateFormat.getDateInstance(style, loc);
            _test(df, false);
        }
    }
    for (style = DateFormat.FULL; style <= DateFormat.SHORT; ++style) {
        if (TEST_TABLE[itable++]) {
            logln("Testing style " + styleName(style));
            DateFormat df = DateFormat.getTimeInstance(style, loc);
            _test(df, true);
        }
    }
    for (int dstyle = DateFormat.FULL; dstyle <= DateFormat.SHORT; ++dstyle) {
        for (int tstyle = DateFormat.FULL; tstyle <= DateFormat.SHORT; ++tstyle) {
            if (TEST_TABLE[itable++]) {
                logln("Testing dstyle " + styleName(dstyle) + ", tstyle " + styleName(tstyle));
                DateFormat df = DateFormat.getDateTimeInstance(dstyle, tstyle, loc);
                _test(df, false);
            }
        }
    }
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat)

Example 82 with DateFormat

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

the class TimeZoneRegressionTest method Test4176686.

/**
 * getDisplayName doesn't work with unusual savings/offsets.
 */
@Test
public void Test4176686() {
    // Construct a zone that does not observe DST but
    // that does have a DST savings (which should be ignored).
    // 1:30
    int offset = 90 * 60000;
    SimpleTimeZone z1 = new SimpleTimeZone(offset, "_std_zone_");
    // 0:45
    z1.setDSTSavings(45 * 60000);
    // Construct a zone that observes DST for the first 6 months.
    SimpleTimeZone z2 = new SimpleTimeZone(offset, "_dst_zone_");
    // 0:45
    z2.setDSTSavings(45 * 60000);
    z2.setStartRule(Calendar.JANUARY, 1, 0);
    z2.setEndRule(Calendar.JULY, 1, 0);
    // Also check DateFormat
    DateFormat fmt1 = new SimpleDateFormat("z");
    // Format uses standard zone
    fmt1.setTimeZone(z1);
    DateFormat fmt2 = new SimpleDateFormat("z");
    // Format uses DST zone
    fmt2.setTimeZone(z2);
    java.util.Calendar tempcal = java.util.Calendar.getInstance();
    tempcal.clear();
    tempcal.set(1970, Calendar.FEBRUARY, 1);
    // Time in DST
    Date dst = tempcal.getTime();
    tempcal.set(1970, Calendar.AUGUST, 1);
    // Time in standard
    Date std = tempcal.getTime();
    // Description, Result, Expected Result
    String[] DATA = { "getDisplayName(false, SHORT)/std zone", z1.getDisplayName(false, TimeZone.SHORT), "GMT+1:30", "getDisplayName(false, LONG)/std zone", z1.getDisplayName(false, TimeZone.LONG), "GMT+01:30", "getDisplayName(true, SHORT)/std zone", z1.getDisplayName(true, TimeZone.SHORT), "GMT+1:30", "getDisplayName(true, LONG)/std zone", z1.getDisplayName(true, TimeZone.LONG), "GMT+01:30", "getDisplayName(false, SHORT)/dst zone", z2.getDisplayName(false, TimeZone.SHORT), "GMT+1:30", "getDisplayName(false, LONG)/dst zone", z2.getDisplayName(false, TimeZone.LONG), "GMT+01:30", "getDisplayName(true, SHORT)/dst zone", z2.getDisplayName(true, TimeZone.SHORT), "GMT+2:15", "getDisplayName(true, LONG)/dst zone", z2.getDisplayName(true, TimeZone.LONG), "GMT+02:15", "DateFormat.format(std)/std zone", fmt1.format(std), "GMT+1:30", "DateFormat.format(dst)/std zone", fmt1.format(dst), "GMT+1:30", "DateFormat.format(std)/dst zone", fmt2.format(std), "GMT+1:30", "DateFormat.format(dst)/dst zone", fmt2.format(dst), "GMT+2:15" };
    for (int i = 0; i < DATA.length; i += 3) {
        if (!DATA[i + 1].equals(DATA[i + 2])) {
            errln("FAIL: " + DATA[i] + " -> " + DATA[i + 1] + ", exp " + DATA[i + 2]);
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 83 with DateFormat

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

the class TimeZoneBoundaryTest method findDaylightBoundaryUsingTimeZone.

void findDaylightBoundaryUsingTimeZone(Date d, boolean startsInDST, long expectedBoundary, TimeZone tz) {
    // Given a date with a year start, find the Daylight onset
    // and end.  The given date should be 1/1/xx in some year.
    // Use a binary search, assuming that we have a Standard
    // time at the midpoint.
    long min = d.getTime();
    long max = min + SIX_MONTHS;
    if (tz.inDaylightTime(d) != startsInDST) {
        errln("FAIL: " + tz.getID() + " inDaylightTime(" + d + ") != " + startsInDST);
        // Flip over; find the apparent value
        startsInDST = !startsInDST;
    }
    if (tz.inDaylightTime(new Date(max)) == startsInDST) {
        errln("FAIL: " + tz.getID() + " inDaylightTime(" + (new Date(max)) + ") != " + (!startsInDST));
        return;
    }
    while ((max - min) > INTERVAL) {
        long mid = (min + max) >> 1;
        boolean isIn = tz.inDaylightTime(new Date(mid));
        if (isIn == startsInDST) {
            min = mid;
        } else {
            max = mid;
        }
    }
    logln(tz.getID() + " Before: " + showDate(min, tz));
    logln(tz.getID() + " After:  " + showDate(max, tz));
    long mindelta = expectedBoundary - min;
    // not used long maxdelta = max - expectedBoundary;
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
    fmt.setTimeZone(tz);
    if (mindelta >= 0 && mindelta <= INTERVAL && mindelta >= 0 && mindelta <= INTERVAL)
        logln("PASS: Expected boundary at " + expectedBoundary + " = " + fmt.format(new Date(expectedBoundary)));
    else
        errln("FAIL: Expected boundary at " + expectedBoundary + " = " + fmt.format(new Date(expectedBoundary)));
}
Also used : DateFormat(android.icu.text.DateFormat) Date(java.util.Date)

Example 84 with DateFormat

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

the class TimeZoneBoundaryTest method showDate.

private static String showDate(Date d, TimeZone zone) {
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
    fmt.setTimeZone(zone);
    java.util.Calendar cal = java.util.Calendar.getInstance();
    cal.setTime(d);
    return "" + (cal.get(Calendar.YEAR) - 1900) + "/" + showNN(cal.get(Calendar.MONTH) + 1) + "/" + showNN(cal.get(Calendar.DAY_OF_MONTH)) + " " + showNN(cal.get(Calendar.HOUR_OF_DAY)) + ":" + showNN(cal.get(Calendar.MINUTE)) + " \"" + d + "\" = " + fmt.format(d) + " = " + d.getTime();
}
Also used : DateFormat(android.icu.text.DateFormat)

Example 85 with DateFormat

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

the class DateFormatTest method TestLocaleDateFormat.

/**
 * Test the formatting of dates in different locales.
 */
@Test
public void TestLocaleDateFormat() {
    // Mon Sep 15 00:00:00 PDT 1997
    Date testDate = new Date(874306800000L);
    DateFormat dfFrench = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.FRENCH);
    DateFormat dfUS = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.US);
    // Set TimeZone = PDT
    TimeZone tz = TimeZone.getTimeZone("PST");
    dfFrench.setTimeZone(tz);
    dfUS.setTimeZone(tz);
    String expectedFRENCH_JDK12 = "lundi 15 septembre 1997 \u00E0 00:00:00 heure d\u2019\u00E9t\u00E9 du Pacifique";
    // String expectedFRENCH = "lundi 15 septembre 1997 00 h 00 PDT";
    String expectedUS = "Monday, September 15, 1997 at 12:00:00 AM Pacific Daylight Time";
    logln("Date set to : " + testDate);
    String out = dfFrench.format(testDate);
    logln("Date Formated with French Locale " + out);
    /* our own data only has GMT-xxxx information here
        String javaVersion = System.getProperty("java.version");
        if (javaVersion.startsWith("1.2")) {
            if (!out.equals(expectedFRENCH_JDK12))
                errln("FAIL: Expected " + expectedFRENCH_JDK12+" Got "+out);
        } else {
            if (!out.equals(expectedFRENCH))
                errln("FAIL: Expected " + expectedFRENCH);
        }
        */
    if (!out.equals(expectedFRENCH_JDK12))
        errln("FAIL: Expected " + expectedFRENCH_JDK12 + " Got " + out);
    out = dfUS.format(testDate);
    logln("Date Formated with US Locale " + out);
    if (!out.equals(expectedUS))
        errln("FAIL: Expected " + expectedUS + " Got " + out);
}
Also used : TimeZone(android.icu.util.TimeZone) DateFormat(android.icu.text.DateFormat) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) 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