Search in sources :

Example 51 with DateFormat

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

the class GlobalizationPreferences method getDateFormat.

/**
 * Gets a date format according to the current settings. If there
 * is an explicit (non-null) date/time format set, a copy of that
 * is returned. Otherwise, the language priority list is used.
 * DF_NONE should be used for the style, where only the date or
 * time format individually is being gotten.
 *
 * @param dateStyle DF_FULL, DF_LONG, DF_MEDIUM, DF_SHORT or DF_NONE
 * @param timeStyle DF_FULL, DF_LONG, DF_MEDIUM, DF_SHORT or DF_NONE
 * @return a DateFormat, according to the above description
 * @hide draft / provisional / internal are hidden on Android
 */
public DateFormat getDateFormat(int dateStyle, int timeStyle) {
    if (dateStyle == DF_NONE && timeStyle == DF_NONE || dateStyle < 0 || dateStyle >= DF_LIMIT || timeStyle < 0 || timeStyle >= DF_LIMIT) {
        throw new IllegalArgumentException("Illegal date format style arguments");
    }
    DateFormat result = null;
    if (dateFormats != null) {
        result = dateFormats[dateStyle][timeStyle];
    }
    if (result != null) {
        // clone for safety
        result = (DateFormat) result.clone();
        // Not sure overriding configuration is what we really want...
        result.setTimeZone(getTimeZone());
    } else {
        result = guessDateFormat(dateStyle, timeStyle);
    }
    return result;
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat)

Example 52 with DateFormat

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

the class CalendarRegressionTest method Test4197699.

/**
 * Week of year is wrong at the start and end of the year.
 */
@Test
public void Test4197699() {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setFirstDayOfWeek(Calendar.MONDAY);
    cal.setMinimalDaysInFirstWeek(4);
    DateFormat fmt = new SimpleDateFormat("E dd MMM yyyy  'DOY='D 'WOY='w");
    fmt.setCalendar(cal);
    int[] DATA = { 2000, Calendar.JANUARY, 1, 52, 2001, Calendar.DECEMBER, 31, 1 };
    for (int i = 0; i < DATA.length; ) {
        cal.set(DATA[i++], DATA[i++], DATA[i++]);
        int expWOY = DATA[i++];
        int actWOY = cal.get(Calendar.WEEK_OF_YEAR);
        if (expWOY == actWOY) {
            logln("Ok: " + fmt.format(cal.getTime()));
        } else {
            errln("FAIL: " + fmt.format(cal.getTime()) + ", expected WOY=" + expWOY);
            cal.add(Calendar.DATE, -8);
            for (int j = 0; j < 14; ++j) {
                cal.add(Calendar.DATE, 1);
                logln(fmt.format(cal.getTime()));
            }
        }
    }
}
Also used : DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 53 with DateFormat

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

the class IndianTest method TestYearEdge.

/**
 * Problem reported by Bruno Haible <bruno.haible@de.ibm.com>
 *  -- see ticket 8419 -- http://bugs.icu-project.org/trac/ticket/8419
 * Problem with months out of range 0-11
 */
@Test
public void TestYearEdge() {
    // Display dates in ISO 8601 format.
    DateFormat fmt = new SimpleDateFormat("YYYY-MM-dd", ULocale.US);
    // Instantiate an Indian calendar.
    ULocale locale = ULocale.US.setKeywordValue("calendar", "indian");
    Calendar cal = Calendar.getInstance(locale);
    // Try add() repeatedly.
    cal.setTimeInMillis(1295568000000L);
    if (!fmt.format(cal.getTime()).equals("2011-01-20")) {
        errln("Incorrect calendar value for year edge test");
    }
    cal.add(Calendar.MONTH, 1);
    if (!fmt.format(cal.getTime()).equals("2011-02-19")) {
        errln("Incorrect calendar value for year edge test");
    }
    cal.add(Calendar.MONTH, 1);
    if (!fmt.format(cal.getTime()).equals("2011-03-21")) {
        errln("Incorrect calendar value for year edge test");
    }
    cal.add(Calendar.MONTH, 1);
    if (!fmt.format(cal.getTime()).equals("2011-04-20")) {
        errln("Incorrect calendar value for year edge test");
    }
}
Also used : ULocale(android.icu.util.ULocale) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) IndianCalendar(android.icu.util.IndianCalendar) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 54 with DateFormat

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

the class IndianTest method TestYear.

@Test
public void TestYear() {
    // Gregorian Calendar
    Calendar gCal = new GregorianCalendar();
    Date gToday = gCal.getTime();
    gCal.add(GregorianCalendar.MONTH, 2);
    Date gFuture = gCal.getTime();
    DateFormat gDF = DateFormat.getDateInstance(gCal, DateFormat.FULL);
    logln("gregorian calendar: " + gDF.format(gToday) + " + 2 months = " + gDF.format(gFuture));
    // Indian Calendar
    IndianCalendar iCal = new IndianCalendar();
    Date iToday = iCal.getTime();
    iCal.add(IndianCalendar.MONTH, 2);
    Date iFuture = iCal.getTime();
    DateFormat iDF = DateFormat.getDateInstance(iCal, DateFormat.FULL);
    logln("Indian calendar: " + iDF.format(iToday) + " + 2 months = " + iDF.format(iFuture));
}
Also used : GregorianCalendar(android.icu.util.GregorianCalendar) IndianCalendar(android.icu.util.IndianCalendar) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) IndianCalendar(android.icu.util.IndianCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 55 with DateFormat

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

the class CalendarRegressionTest method TestDateFormatFactoryJ26.

/**
 * 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);
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) DateFormat(android.icu.text.DateFormat) 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