Search in sources :

Example 6 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestEmpty.

@Test
public void TestEmpty() {
    // now nothing
    DateTimePatternGenerator nullGen = DateTimePatternGenerator.getEmptyInstance();
    SimpleDateFormat format = new SimpleDateFormat(nullGen.getBestPattern("yMdHms"), ULocale.ROOT);
    TimeZone rootZone = TimeZone.getTimeZone("Etc/GMT");
    format.setTimeZone(rootZone);
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 7 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestBasic.

@Test
public void TestBasic() {
    ULocale uLocale = null;
    DateTimePatternGenerator dtfg = null;
    Date date = null;
    for (int i = 0; i < dateTestData.length; ++i) {
        if (dateTestData[i] instanceof ULocale) {
            uLocale = (ULocale) dateTestData[i];
            dtfg = DateTimePatternGenerator.getInstance(uLocale);
            if (GENERATE_TEST_DATA)
                logln("new ULocale(\"" + uLocale.toString() + "\"),");
        } else if (dateTestData[i] instanceof Date) {
            date = (Date) dateTestData[i];
            if (GENERATE_TEST_DATA)
                logln("new Date(" + date.getTime() + "L),");
        } else if (dateTestData[i] instanceof String) {
            String testSkeleton = (String) dateTestData[i];
            String pattern = dtfg.getBestPattern(testSkeleton);
            SimpleDateFormat sdf = new SimpleDateFormat(pattern, uLocale);
            String formatted = sdf.format(date);
            if (GENERATE_TEST_DATA)
                logln("new String[] {\"" + testSkeleton + "\", \"" + Utility.escape(formatted) + "\"},");
        // logln(uLocale + "\t" + testSkeleton + "\t" + pattern + "\t" + sdf.format(date));
        } else {
            String[] testPair = (String[]) dateTestData[i];
            String testSkeleton = testPair[0];
            String testFormatted = testPair[1];
            String pattern = dtfg.getBestPattern(testSkeleton);
            SimpleDateFormat sdf = new SimpleDateFormat(pattern, uLocale);
            String formatted = sdf.format(date);
            if (GENERATE_TEST_DATA) {
                logln("new String[] {\"" + testSkeleton + "\", \"" + Utility.escape(formatted) + "\"},");
            } else if (!formatted.equals(testFormatted)) {
                errln(uLocale + "\tformatted string doesn't match test case: " + testSkeleton + "\t generated: " + pattern + "\t expected: " + testFormatted + "\t got: " + formatted);
                if (true) {
                    // debug
                    pattern = dtfg.getBestPattern(testSkeleton);
                    sdf = new SimpleDateFormat(pattern, uLocale);
                    formatted = sdf.format(date);
                }
            }
        // logln(uLocale + "\t" + testSkeleton + "\t" + pattern + "\t" + sdf.format(date));
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 8 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestGetAppendItemFormat.

/* Tests the method
     *        public String getAppendItemFormat(int field)
     */
@Test
public void TestGetAppendItemFormat() {
    DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance(ULocale.ENGLISH);
    int[] fields = { DateTimePatternGenerator.ERA, DateTimePatternGenerator.DAY, DateTimePatternGenerator.SECOND };
    String[] results = { "{0} {1}", "{0} ({2}: {1})", "{0} ({2}: {1})" };
    for (int i = 0; i < fields.length; i++) {
        if (!dtpg.getAppendItemFormat(fields[i]).equals(results[i])) {
            errln("DateTimePatternGenerator.getAppendItemFormat(int field) " + "did not return as expected. For field " + fields[i] + ", was expecting " + results[i] + " but got back " + dtpg.getAppendItemFormat(fields[i]));
        }
    }
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) Test(org.junit.Test)

Example 9 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestT7169.

/*
     * Test case for DateFormatPatternGenerator threading problem #7169
     */
@Test
public void TestT7169() {
    Thread[] workers = new Thread[10];
    for (int i = 0; i < workers.length; i++) {
        workers[i] = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    for (int i = 0; i < 50; i++) {
                        DateTimePatternGenerator patternGenerator = DateTimePatternGenerator.getFrozenInstance(ULocale.US);
                        patternGenerator.getBestPattern("MMMMd");
                    }
                } catch (Exception e) {
                    errln("FAIL: Caught an exception (frozen)" + e);
                }
                try {
                    for (int i = 0; i < 50; i++) {
                        DateTimePatternGenerator patternGenerator = DateTimePatternGenerator.getInstance(ULocale.US);
                        patternGenerator.getBestPattern("MMMMd");
                    }
                } catch (Exception e) {
                    errln("FAIL: Caught an exception " + e);
                }
            }
        });
    }
    for (Thread wk : workers) {
        wk.start();
    }
    for (Thread wk : workers) {
        try {
            wk.join();
        } catch (InterruptedException ie) {
        }
    }
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) Test(org.junit.Test)

Example 10 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestRoot.

@Test
public void TestRoot() {
    DateTimePatternGenerator rootGen = DateTimePatternGenerator.getInstance(ULocale.ROOT);
    SimpleDateFormat rootFormat = new SimpleDateFormat(rootGen.getBestPattern("yMdHms"), ULocale.ROOT);
    rootFormat.setTimeZone(gmt);
    // *** expected result should be "1999-10-14 6:58:59" with current data, changed test temporarily to match current result, needs investigation
    assertEquals("root format: yMdHms", "1999-10-14 06:58:59", rootFormat.format(sampleDate));
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Aggregations

DateTimePatternGenerator (android.icu.text.DateTimePatternGenerator)28 Test (org.junit.Test)27 SimpleDateFormat (android.icu.text.SimpleDateFormat)6 ULocale (android.icu.util.ULocale)6 SimpleTimeZone (android.icu.util.SimpleTimeZone)3 TimeZone (android.icu.util.TimeZone)3 Date (java.util.Date)3 LinkedHashSet (java.util.LinkedHashSet)2 Calendar (android.icu.util.Calendar)1 GregorianCalendar (android.icu.util.GregorianCalendar)1 ParsePosition (java.text.ParsePosition)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1