Search in sources :

Example 1 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestGetSkeletons.

/* Tests the method
     *        public Map<String, String> getSkeletons(Map<String, String> result)
     */
@Test
public void TestGetSkeletons() {
    DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance();
    // Tests when "if (result == null)" is true
    try {
        dtpg.getSkeletons(null);
    } catch (Exception e) {
        errln("DateTimePatternGenerator.getSkeletons(Map) was suppose to " + "return a new LinkedHashMap for a null parameter.");
    }
    // Tests when "if (result == null)" is false
    Map<String, String> mm = new LinkedHashMap<String, String>();
    try {
        dtpg.getSkeletons(mm);
    } catch (Exception e) {
        errln("DateTimePatternGenerator.getSkeletons(Map) was suppose to " + "return a new LinkedHashMap for a LinkedHashMap parameter.");
    }
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestGetFields.

/* Tests the method
     *    public String getFields(String pattern)
     */
@Test
public void TestGetFields() {
    DateTimePatternGenerator dt = DateTimePatternGenerator.getInstance();
    String[] cases = { "MMDDYY", "HHMMSS", "", "MM/DD/YYYY HH:MM:SS", "MMDDYY HHMMSS", "HHMMSS MMDDYYYY", "HMS MDY" };
    String[] results = { "{Month:N}{Day_Of_Year:N}{Year:N}", "{Hour:N}{Month:N}{Fractional_Second:N}", "", "{Month:N}/{Day_Of_Year:N}/{Year:N} {Hour:N}:{Month:N}:{Fractional_Second:N}", "{Month:N}{Day_Of_Year:N}{Year:N} {Hour:N}{Month:N}{Fractional_Second:N}", "{Hour:N}{Month:N}{Fractional_Second:N} {Month:N}{Day_Of_Year:N}{Year:N}", "{Hour:N}{Month:N}{Fractional_Second:N} {Month:N}{Day_Of_Year:N}{Year:N}" };
    for (int i = 0; i < cases.length; i++) {
        if (!dt.getFields(cases[i]).equals(results[i])) {
            errln("DateTimePatternGenerator.getFields(String) did not " + "not return an expected result when passing " + cases[i] + ". Got " + dt.getFields(cases[i]) + " but expected " + results[i]);
        }
    }
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) Test(org.junit.Test)

Example 3 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestEmptyInstance.

@Test
public void TestEmptyInstance() {
    DateTimePatternGenerator dtpg = DateTimePatternGenerator.getEmptyInstance();
    String skeleton = "GrMMd";
    String message = "DTPG getEmptyInstance should not throw exceptions on basic operations and should conform to " + "the example in setAppendItemFormat";
    assertEquals(message, "G ├'F7': d┤ ├'F3': MM┤ ├'F1': y┤", dtpg.getBestPattern(skeleton));
    dtpg.addPattern("d-MM-yyyy", false, new DateTimePatternGenerator.PatternInfo());
    assertEquals(message, "d-MM-y ├'F0': G┤", dtpg.getBestPattern(skeleton));
    dtpg.setAppendItemFormat(DateTimePatternGenerator.ERA, "{0}, {1}");
    assertEquals(message, "d-MM-y, G", dtpg.getBestPattern(skeleton));
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) Test(org.junit.Test)

Example 4 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestReplacingZoneString.

@Test
public void TestReplacingZoneString() {
    Date testDate = new Date();
    TimeZone testTimeZone = TimeZone.getTimeZone("America/New_York");
    TimeZone bogusTimeZone = new SimpleTimeZone(1234, "Etc/Unknown");
    Calendar calendar = Calendar.getInstance();
    ParsePosition parsePosition = new ParsePosition(0);
    ULocale[] locales = ULocale.getAvailableLocales();
    int count = 0;
    for (int i = 0; i < locales.length; ++i) {
        // skip the country locales unless we are doing exhaustive tests
        if (getExhaustiveness() < 6) {
            if (locales[i].getCountry().length() > 0) {
                continue;
            }
        }
        count++;
        // ticket#6503
        if (getExhaustiveness() <= 5 && count % 3 != 0) {
            continue;
        }
        logln(locales[i].toString());
        DateTimePatternGenerator dtpgen = DateTimePatternGenerator.getInstance(locales[i]);
        for (int style1 = DateFormat.FULL; style1 <= DateFormat.SHORT; ++style1) {
            final SimpleDateFormat oldFormat = (SimpleDateFormat) DateFormat.getTimeInstance(style1, locales[i]);
            String pattern = oldFormat.toPattern();
            // replaceZoneString(pattern, "VVVV");
            String newPattern = dtpgen.replaceFieldTypes(pattern, "VVVV");
            if (newPattern.equals(pattern)) {
                continue;
            }
            // verify that it roundtrips parsing
            SimpleDateFormat newFormat = new SimpleDateFormat(newPattern, locales[i]);
            newFormat.setTimeZone(testTimeZone);
            String formatted = newFormat.format(testDate);
            calendar.setTimeZone(bogusTimeZone);
            parsePosition.setIndex(0);
            newFormat.parse(formatted, calendar, parsePosition);
            if (parsePosition.getErrorIndex() >= 0) {
                errln("Failed parse with VVVV:\t" + locales[i] + ",\t\"" + pattern + "\",\t\"" + newPattern + "\",\t\"" + formatted.substring(0, parsePosition.getErrorIndex()) + "{}" + formatted.substring(parsePosition.getErrorIndex()) + "\"");
            } else if (!calendar.getTimeZone().getID().equals(testTimeZone.getID())) {
                errln("Failed timezone roundtrip with VVVV:\t" + locales[i] + ",\t\"" + pattern + "\",\t\"" + newPattern + "\",\t\"" + formatted + "\",\t" + calendar.getTimeZone().getID() + " != " + testTimeZone.getID());
            } else {
                logln(locales[i] + ":\t\"" + pattern + "\" => \t\"" + newPattern + "\"\t" + formatted);
            }
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 5 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestSimple.

@Test
public void TestSimple() {
    // some simple use cases
    ULocale locale = ULocale.GERMANY;
    TimeZone zone = TimeZone.getTimeZone("Europe/Paris");
    // make from locale
    DateTimePatternGenerator gen = DateTimePatternGenerator.getInstance(locale);
    SimpleDateFormat format = new SimpleDateFormat(gen.getBestPattern("MMMddHmm"), locale);
    format.setTimeZone(zone);
    assertEquals("simple format: MMMddHmm", "14. Okt., 08:58", format.format(sampleDate));
    // (a generator can be built from scratch, but that is not a typical use case)
    // modify the generator by adding patterns
    DateTimePatternGenerator.PatternInfo returnInfo = new DateTimePatternGenerator.PatternInfo();
    gen.addPattern("d'. von' MMMM", true, returnInfo);
    // the returnInfo is mostly useful for debugging problem cases
    format.applyPattern(gen.getBestPattern("MMMMdHmm"));
    assertEquals("modified format: MMMdHmm", "14. von Oktober, 08:58", format.format(sampleDate));
    // get a pattern and modify it
    format = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
    format.setTimeZone(zone);
    String pattern = format.toPattern();
    assertEquals("full-date", "Donnerstag, 14. Oktober 1999 um 08:58:59 Mitteleurop\u00E4ische Sommerzeit", format.format(sampleDate));
    // modify it to change the zone.
    String newPattern = gen.replaceFieldTypes(pattern, "vvvv");
    format.applyPattern(newPattern);
    assertEquals("full-date: modified zone", "Donnerstag, 14. Oktober 1999 um 08:58:59 Mitteleurop\u00E4ische Zeit", format.format(sampleDate));
    // add test of basic cases
    // lang  YYYYMMM MMMd    MMMdhmm hmm hhmm    Full Date-Time
    // en  Mar 2007    Mar 4   6:05 PM Mar 4   6:05 PM 06:05 PM    Sunday, March 4, 2007 6:05:05 PM PT
    DateTimePatternGenerator enGen = DateTimePatternGenerator.getInstance(ULocale.ENGLISH);
    TimeZone enZone = TimeZone.getTimeZone("Etc/GMT");
    SimpleDateFormat enFormat = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, ULocale.ENGLISH);
    enFormat.setTimeZone(enZone);
    String[][] tests = { { "yyyyMMMdd", "Oct 14, 1999" }, { "yyyyqqqq", "4th quarter 1999" }, { "yMMMdd", "Oct 14, 1999" }, { "EyyyyMMMdd", "Thu, Oct 14, 1999" }, { "yyyyMMdd", "10/14/1999" }, { "yyyyMMM", "Oct 1999" }, { "yyyyMM", "10/1999" }, { "yyMM", "10/99" }, // narrow format
    { "yMMMMMd", "O 14, 1999" }, // narrow format
    { "EEEEEMMMMMd", "T, O 14" }, { "MMMd", "Oct 14" }, { "MMMdhmm", "Oct 14, 6:58 AM" }, { "EMMMdhmms", "Thu, Oct 14, 6:58:59 AM" }, { "MMdhmm", "10/14, 6:58 AM" }, { "EEEEMMMdhmms", "Thursday, Oct 14, 6:58:59 AM" }, // (fixed expected result per ticket 6872<-7180)
    { "yyyyMMMddhhmmss", "Oct 14, 1999, 6:58:59 AM" }, // (fixed expected result per ticket 6872<-7180)
    { "EyyyyMMMddhhmmss", "Thu, Oct 14, 1999, 6:58:59 AM" }, { "hmm", "6:58 AM" }, // (fixed expected result per ticket 6872<-7180)
    { "hhmm", "6:58 AM" }, // (fixed expected result per ticket 6872<-7180)
    { "hhmmVVVV", "6:58 AM GMT" } };
    for (int i = 0; i < tests.length; ++i) {
        final String testSkeleton = tests[i][0];
        String pat = enGen.getBestPattern(testSkeleton);
        enFormat.applyPattern(pat);
        String formattedDate = enFormat.format(sampleDate);
        assertEquals("Testing skeleton '" + testSkeleton + "' with  " + sampleDate, tests[i][1], formattedDate);
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) ULocale(android.icu.util.ULocale) 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