Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a localized date in Codename One

Tags:

codenameone

This question is not for generic Java, but only for Codename One.

I know that the L10NManager class provides the methods formatDateLongStyle, formatDateShortStyle, formatDateTime, formatDateTimeMedium, formatDateTimeShort, but their output is inconsistent between platforms (Simulator, Android, iOS, etc.). Moreover, even if their output could be consistent, it's not exactly as I need it.

I need to format the output localized string exactly as requested, that is: short localized day of week, day of month, long localized month, year (four digits), a minus sign with spaces (" - "), hours (24h, two digits), colon (":"), minutes. I don't want seconds, I need an output exactly in this format.

Is there any API for that in Codename One? Any hint? Thank you

like image 544
Francesco Galgani Avatar asked Dec 06 '25 05:12

Francesco Galgani


1 Answers

Examples of patterns compatible with the Codename One SimpleDateFormat class: https://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Full implementation example to localize in Italian the date formatted as I requested. Note that the first day to localize in the weekDays and shortWeekDays arrays is Sunday.

Form hi = new Form("Hi World", BoxLayout.y());

        String[] weekDays = {"Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato"};
        String[] shortWeekDays = {"Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab"};
        String[] months = {"Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre"};
        String[] shortMonths = {"Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago", "Set", "Ott", "Nov", "Dic"};
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern("EEE d MMMMMMMMMMMMMMMMMMMM yyyy - HH:mm");
        simpleDateFormat.getDateFormatSymbols().setWeekdays(weekDays);
        simpleDateFormat.getDateFormatSymbols().setShortWeekdays(shortWeekDays);
        simpleDateFormat.getDateFormatSymbols().setMonths(months);
        simpleDateFormat.getDateFormatSymbols().setShortMonths(shortMonths);
        String date = simpleDateFormat.format(new Date(System.currentTimeMillis()));

        hi.add(new Label(date));
        hi.show();

Example of output:

Mer 11 Settembre 2019 - 11:51
like image 199
Francesco Galgani Avatar answered Dec 11 '25 19:12

Francesco Galgani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!