Search in sources :

Example 76 with DateFormatSymbols

use of java.text.DateFormatSymbols in project jdk8u_jdk by JetBrains.

the class Calendar method getDisplayName.

/**
     * Returns the string representation of the calendar
     * <code>field</code> value in the given <code>style</code> and
     * <code>locale</code>.  If no string representation is
     * applicable, <code>null</code> is returned. This method calls
     * {@link Calendar#get(int) get(field)} to get the calendar
     * <code>field</code> value if the string representation is
     * applicable to the given calendar <code>field</code>.
     *
     * <p>For example, if this <code>Calendar</code> is a
     * <code>GregorianCalendar</code> and its date is 2005-01-01, then
     * the string representation of the {@link #MONTH} field would be
     * "January" in the long style in an English locale or "Jan" in
     * the short style. However, no string representation would be
     * available for the {@link #DAY_OF_MONTH} field, and this method
     * would return <code>null</code>.
     *
     * <p>The default implementation supports the calendar fields for
     * which a {@link DateFormatSymbols} has names in the given
     * <code>locale</code>.
     *
     * @param field
     *        the calendar field for which the string representation
     *        is returned
     * @param style
     *        the style applied to the string representation; one of {@link
     *        #SHORT_FORMAT} ({@link #SHORT}), {@link #SHORT_STANDALONE},
     *        {@link #LONG_FORMAT} ({@link #LONG}), {@link #LONG_STANDALONE},
     *        {@link #NARROW_FORMAT}, or {@link #NARROW_STANDALONE}.
     * @param locale
     *        the locale for the string representation
     *        (any calendar types specified by {@code locale} are ignored)
     * @return the string representation of the given
     *        {@code field} in the given {@code style}, or
     *        {@code null} if no string representation is
     *        applicable.
     * @exception IllegalArgumentException
     *        if {@code field} or {@code style} is invalid,
     *        or if this {@code Calendar} is non-lenient and any
     *        of the calendar fields have invalid values
     * @exception NullPointerException
     *        if {@code locale} is null
     * @since 1.6
     */
public String getDisplayName(int field, int style, Locale locale) {
    if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale, ERA_MASK | MONTH_MASK | DAY_OF_WEEK_MASK | AM_PM_MASK)) {
        return null;
    }
    String calendarType = getCalendarType();
    int fieldValue = get(field);
    // the standalone and narrow styles are supported only through CalendarDataProviders.
    if (isStandaloneStyle(style) || isNarrowFormatStyle(style)) {
        String val = CalendarDataUtility.retrieveFieldValueName(calendarType, field, fieldValue, style, locale);
        // Perform fallback here to follow the CLDR rules
        if (val == null) {
            if (isNarrowFormatStyle(style)) {
                val = CalendarDataUtility.retrieveFieldValueName(calendarType, field, fieldValue, toStandaloneStyle(style), locale);
            } else if (isStandaloneStyle(style)) {
                val = CalendarDataUtility.retrieveFieldValueName(calendarType, field, fieldValue, getBaseStyle(style), locale);
            }
        }
        return val;
    }
    DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
    String[] strings = getFieldStrings(field, style, symbols);
    if (strings != null) {
        if (fieldValue < strings.length) {
            return strings[fieldValue];
        }
    }
    return null;
}
Also used : DateFormatSymbols(java.text.DateFormatSymbols)

Example 77 with DateFormatSymbols

use of java.text.DateFormatSymbols in project jdk8u_jdk by JetBrains.

the class HostLocaleProviderAdapterImpl method getDateFormatSymbolsProvider.

public static DateFormatSymbolsProvider getDateFormatSymbolsProvider() {
    return new DateFormatSymbolsProvider() {

        @Override
        public Locale[] getAvailableLocales() {
            return getSupportedCalendarLocales();
        }

        @Override
        public boolean isSupportedLocale(Locale locale) {
            return isSupportedCalendarLocale(locale);
        }

        @Override
        public DateFormatSymbols getInstance(Locale locale) {
            DateFormatSymbols dfs;
            SoftReference<DateFormatSymbols> ref = dateFormatSymbolsCache.get(locale);
            if (ref == null || (dfs = ref.get()) == null) {
                dfs = new DateFormatSymbols(locale);
                String langTag = removeExtensions(locale).toLanguageTag();
                dfs.setAmPmStrings(getAmPmStrings(langTag, dfs.getAmPmStrings()));
                dfs.setEras(getEras(langTag, dfs.getEras()));
                dfs.setMonths(getMonths(langTag, dfs.getMonths()));
                dfs.setShortMonths(getShortMonths(langTag, dfs.getShortMonths()));
                dfs.setWeekdays(getWeekdays(langTag, dfs.getWeekdays()));
                dfs.setShortWeekdays(getShortWeekdays(langTag, dfs.getShortWeekdays()));
                ref = new SoftReference<>(dfs);
                dateFormatSymbolsCache.put(locale, ref);
            }
            return (DateFormatSymbols) dfs.clone();
        }
    };
}
Also used : Locale(java.util.Locale) DateFormatSymbolsProvider(java.text.spi.DateFormatSymbolsProvider) DateFormatSymbols(java.text.DateFormatSymbols)

Example 78 with DateFormatSymbols

use of java.text.DateFormatSymbols in project CompactCalendarView by SundeepK.

the class CompactCalendarControllerTest method testItAbbreviatesDayNames.

@Test
public void testItAbbreviatesDayNames() {
    //simulate Feb month
    when(calendar.get(Calendar.DAY_OF_WEEK)).thenReturn(1);
    when(calendar.get(Calendar.MONTH)).thenReturn(1);
    when(calendar.getActualMaximum(Calendar.DAY_OF_MONTH)).thenReturn(28);
    //set grow progress so that it simulates the calendar being open
    underTest.setGrowProgress(1000);
    underTest.setLocale(TimeZone.getTimeZone("Europe/Paris"), Locale.FRANCE);
    //reset because invalidate is called
    reset(canvas);
    underTest.setUseWeekDayAbbreviation(true);
    //reset because invalidate is called
    reset(canvas);
    underTest.drawMonth(canvas, calendar, 0);
    DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(Locale.FRANCE);
    String[] dayNames = dateFormatSymbols.getShortWeekdays();
    InOrder inOrder = inOrder(canvas);
    inOrder.verify(canvas).drawText(eq(dayNames[2]), anyInt(), anyInt(), eq(paint));
    inOrder.verify(canvas).drawText(eq(dayNames[3]), anyInt(), anyInt(), eq(paint));
    inOrder.verify(canvas).drawText(eq(dayNames[4]), anyInt(), anyInt(), eq(paint));
    inOrder.verify(canvas).drawText(eq(dayNames[5]), anyInt(), anyInt(), eq(paint));
    inOrder.verify(canvas).drawText(eq(dayNames[6]), anyInt(), anyInt(), eq(paint));
    inOrder.verify(canvas).drawText(eq(dayNames[7]), anyInt(), anyInt(), eq(paint));
    inOrder.verify(canvas).drawText(eq(dayNames[1]), anyInt(), anyInt(), eq(paint));
}
Also used : InOrder(org.mockito.InOrder) DateFormatSymbols(java.text.DateFormatSymbols) Test(org.junit.Test)

Example 79 with DateFormatSymbols

use of java.text.DateFormatSymbols in project poi by apache.

the class TestText method testTextWithDateFormatSecondArg.

@Test
public void testTextWithDateFormatSecondArg() {
    TimeZone userTZ = LocaleUtil.getUserTimeZone();
    LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET"));
    try {
        // Test with Java style M=Month
        ValueEval numArg = new NumberEval(321.321);
        ValueEval formatArg = new StringEval("dd:MM:yyyy hh:mm:ss");
        ValueEval[] args = { numArg, formatArg };
        ValueEval result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
        ValueEval testResult = new StringEval("16:11:1900 07:42:14");
        assertEquals(testResult.toString(), result.toString());
        // Excel also supports "m before h is month"
        formatArg = new StringEval("dd:mm:yyyy hh:mm:ss");
        args[1] = formatArg;
        result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
        testResult = new StringEval("16:11:1900 07:42:14");
        assertEquals(testResult.toString(), result.toString());
        // this line is intended to compute how "November" would look like in the current locale
        // update: now the locale will be (if not set otherwise) always Locale.getDefault() (see LocaleUtil)
        DateFormatSymbols dfs = DateFormatSymbols.getInstance(LocaleUtil.getUserLocale());
        SimpleDateFormat sdf = new SimpleDateFormat("MMMM", dfs);
        sdf.setTimeZone(LocaleUtil.getUserTimeZone());
        String november = sdf.format(LocaleUtil.getLocaleCalendar(2015, 10, 1).getTime());
        // Again with Java style
        formatArg = new StringEval("MMMM dd, yyyy");
        args[1] = formatArg;
        result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
        testResult = new StringEval(november + " 16, 1900");
        assertEquals(testResult.toString(), result.toString());
        // And Excel style
        formatArg = new StringEval("mmmm dd, yyyy");
        args[1] = formatArg;
        result = TextFunction.TEXT.evaluate(args, -1, (short) -1);
        testResult = new StringEval(november + " 16, 1900");
        assertEquals(testResult.toString(), result.toString());
    } finally {
        LocaleUtil.setUserTimeZone(userTZ);
    }
}
Also used : TimeZone(java.util.TimeZone) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringEval(org.apache.poi.ss.formula.eval.StringEval) DateFormatSymbols(java.text.DateFormatSymbols) SimpleDateFormat(java.text.SimpleDateFormat) NumberEval(org.apache.poi.ss.formula.eval.NumberEval) Test(org.junit.Test)

Example 80 with DateFormatSymbols

use of java.text.DateFormatSymbols in project poi by apache.

the class TestExcelStyleDateFormatter method test60369.

/**
     * [Bug 60369] Month format 'MMMMM' issue with TEXT-formula and Java 8
     */
@Test
public void test60369() throws ParseException {
    // Setting up the locale to be tested together with a list of asserted unicode-formatted results and put them in a map.
    Locale germanLocale = Locale.GERMAN;
    List<String> germanResultList = Arrays.asList("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D");
    Locale russianLocale = new Locale("ru", "RU");
    List<String> russianResultList = Arrays.asList("я", "ф", "м", "а", "м", "и", "и", "а", "с", "о", "н", "д");
    Locale austrianLocale = new Locale("de", "AT");
    List<String> austrianResultList = Arrays.asList("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D");
    Locale englishLocale = Locale.UK;
    List<String> englishResultList = Arrays.asList("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D");
    Locale frenchLocale = Locale.FRENCH;
    List<String> frenchResultList = Arrays.asList("j", "f", "m", "a", "m", "j", "j", "a", "s", "o", "n", "d");
    Locale chineseLocale = Locale.CHINESE;
    List<String> chineseResultList = Arrays.asList("一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十", "十");
    Locale turkishLocale = new Locale("tr", "TR");
    List<String> turkishResultList = Arrays.asList("O", "Ş", "M", "N", "M", "H", "T", "A", "E", "E", "K", "A");
    Locale hungarianLocale = new Locale("hu", "HU");
    List<String> hungarianResultList = Arrays.asList("j", "f", "m", "á", "m", "j", "j", "a", "s", "o", "n", "d");
    Locale indianLocale = new Locale("en", "IN");
    List<String> indianResultList = Arrays.asList("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D");
    Locale indonesianLocale = new Locale("in", "ID");
    List<String> indonesianResultList = Arrays.asList("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D");
    Map<Locale, List<String>> testMap = new HashMap<Locale, List<String>>();
    testMap.put(germanLocale, germanResultList);
    testMap.put(russianLocale, russianResultList);
    testMap.put(austrianLocale, austrianResultList);
    testMap.put(englishLocale, englishResultList);
    testMap.put(frenchLocale, frenchResultList);
    testMap.put(chineseLocale, chineseResultList);
    testMap.put(turkishLocale, turkishResultList);
    testMap.put(hungarianLocale, hungarianResultList);
    testMap.put(indianLocale, indianResultList);
    testMap.put(indonesianLocale, indonesianResultList);
    // We have to set up dates as well.
    SimpleDateFormat testDateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.ROOT);
    List<Date> testDates = Arrays.asList(testDateFormat.parse("12.01.1980"), testDateFormat.parse("11.02.1995"), testDateFormat.parse("10.03.2045"), testDateFormat.parse("09.04.2016"), testDateFormat.parse("08.05.2017"), testDateFormat.parse("07.06.1945"), testDateFormat.parse("06.07.1998"), testDateFormat.parse("05.08.2099"), testDateFormat.parse("04.09.1988"), testDateFormat.parse("03.10.2023"), testDateFormat.parse("02.11.1978"), testDateFormat.parse("01.12.1890"));
    // Let's iterate over the test setup.
    for (Locale locale : testMap.keySet()) {
        //System.err.println("Locale: " + locale);
        ExcelStyleDateFormatter formatter = new ExcelStyleDateFormatter(EXCEL_DATE_FORMAT, new DateFormatSymbols(locale));
        for (int i = 0; i < 12; i++) {
            // Call the method to be tested!
            String result = formatter.format(testDates.get(i), new StringBuffer(), new FieldPosition(java.text.DateFormat.MONTH_FIELD)).toString();
            //System.err.println(result +  " - " + getUnicode(result.charAt(0)));
            assertEquals(getUnicode(testMap.get(locale).get(i).charAt(0)), getUnicode(result.charAt(0)));
        }
    }
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) FieldPosition(java.text.FieldPosition) Date(java.util.Date) List(java.util.List) DateFormatSymbols(java.text.DateFormatSymbols) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Aggregations

DateFormatSymbols (java.text.DateFormatSymbols)87 SimpleDateFormat (java.text.SimpleDateFormat)29 Date (java.util.Date)23 Locale (java.util.Locale)15 Resources (android.content.res.Resources)12 ArrayList (java.util.ArrayList)10 View (android.view.View)9 TextView (android.widget.TextView)8 OnClickListener (android.view.View.OnClickListener)6 Calendar (java.util.Calendar)6 GregorianCalendar (java.util.GregorianCalendar)6 RelativeLayout (android.widget.RelativeLayout)5 Test (org.junit.Test)5 LayoutParams (android.app.ActionBar.LayoutParams)4 Typeface (android.graphics.Typeface)4 List (java.util.List)4 TypedArray (android.content.res.TypedArray)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ObjectInputStream (java.io.ObjectInputStream)3