Java Code Examples for android.icu.text.DateFormat#getTimeInstance()

The following examples show how to use android.icu.text.DateFormat#getTimeInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GlobalizationPreferences.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * This function can be overridden by subclasses to use different heuristics.
 * <b>It MUST return a 'safe' value,
 * one whose modification will not affect this object.</b>
 *
 * @param dateStyle
 * @param timeStyle
 * @hide draft / provisional / internal are hidden on Android
 */
protected DateFormat guessDateFormat(int dateStyle, int timeStyle) {
    DateFormat result;
    ULocale dfLocale = getAvailableLocale(TYPE_DATEFORMAT);
    if (dfLocale == null) {
        dfLocale = ULocale.ROOT;
    }
    if (timeStyle == DF_NONE) {
        result = DateFormat.getDateInstance(getCalendar(), dateStyle, dfLocale);
    } else if (dateStyle == DF_NONE) {
        result = DateFormat.getTimeInstance(getCalendar(), timeStyle, dfLocale);
    } else {
        result = DateFormat.getDateTimeInstance(getCalendar(), dateStyle, timeStyle, dfLocale);
    }
    return result;
}
 
Example 2
Source File: TestCLDRVsICU.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private SimpleDateFormat getDateFormat(ULocale locale, int dateFormat, int timeFormat) {
    if (DEBUG)
        logln("Getting date/time format for " + locale);
    if (DEBUG && "ar_EG".equals(locale.toString())) {
        logln("debug here");
    }
    DateFormat dt;
    if (dateFormat == 0) {
        dt = DateFormat.getTimeInstance(DateFormatValues[timeFormat], locale);
        if (DEBUG)
            System.out.print("getTimeInstance");
    } else if (timeFormat == 0) {
        dt = DateFormat.getDateInstance(DateFormatValues[dateFormat], locale);
        if (DEBUG)
            System.out.print("getDateInstance");
    } else {
        dt = DateFormat.getDateTimeInstance(DateFormatValues[dateFormat], DateFormatValues[timeFormat],
                locale);
        if (DEBUG)
            System.out.print("getDateTimeInstance");
    }
    if (DEBUG)
        logln("\tinput:\t" + dateFormat + ", " + timeFormat + " => " + ((SimpleDateFormat) dt).toPattern());
    return (SimpleDateFormat) dt;
}
 
Example 3
Source File: DateFormatRegressionTestJ.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@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");
    }
}
 
Example 4
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * DateFormat class mistakes date style and time style as follows: -
 * DateFormat.getDateTimeInstance takes date style as time style, and time
 * style as date style - If a Calendar is passed to
 * DateFormat.getDateInstance, it returns time instance - If a Calendar is
 * passed to DateFormat.getTimeInstance, it returns date instance
 */
@Test
public void TestDateFormatFactoryJ26() {
    TimeZone zone = TimeZone.getDefault();
    try {
        Locale loc = Locale.US;
        TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
        java.util.Calendar tempcal = java.util.Calendar.getInstance();
        tempcal.set(2001, Calendar.APRIL, 5, 17, 43, 53);
        Date date = tempcal.getTime();
        Calendar cal = Calendar.getInstance(loc);
        Object[] DATA = {
            DateFormat.getDateInstance(DateFormat.SHORT, loc),
            "DateFormat.getDateInstance(DateFormat.SHORT, loc)",
            "4/5/01",

            DateFormat.getTimeInstance(DateFormat.SHORT, loc),
            "DateFormat.getTimeInstance(DateFormat.SHORT, loc)",
            "5:43 PM",

            DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc),
            "DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, loc)",
            "Thursday, April 5, 2001 at 5:43 PM",

            DateFormat.getDateInstance(cal, DateFormat.SHORT, loc),
            "DateFormat.getDateInstance(cal, DateFormat.SHORT, loc)",
            "4/5/01",

            DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc),
            "DateFormat.getTimeInstance(cal, DateFormat.SHORT, loc)",
            "5:43 PM",

            DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc),
            "DateFormat.getDateTimeInstance(cal, DateFormat.FULL, DateFormat.SHORT, loc)",
            "Thursday, April 5, 2001 at 5:43 PM",
        
            cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc),
            "cal.getDateTimeFormat(DateFormat.SHORT, DateFormat.FULL, loc)",
            "4/5/01, 5:43:53 PM Pacific Daylight Time",

            cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc),
            "cal.getDateTimeFormat(DateFormat.FULL, DateFormat.SHORT, loc)",
            "Thursday, April 5, 2001 at 5:43 PM",
        };
        for (int i=0; i<DATA.length; i+=3) {
            DateFormat df = (DateFormat) DATA[i];
            String desc = (String) DATA[i+1];
            String exp = (String) DATA[i+2];
            String got = df.format(date);
            if (got.equals(exp)) {
                logln("Ok: " + desc + " => " + got);
            } else {
                errln("FAIL: " + desc + " => " + got + ", expected " + exp);
            }
        }
    } finally {
        TimeZone.setDefault(zone);
    }
}
 
Example 5
Source File: DateTimeGeneratorTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@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++;
        // Skipping some test case in the non-exhaustive mode to reduce the test time
        //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();
            String newPattern = dtpgen.replaceFieldTypes(pattern, "VVVV"); // replaceZoneString(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);
            }
        }
    }
}