Search in sources :

Example 1 with DateFormat

use of com.codename1.l10n.DateFormat in project CodeRAD by shannah.

the class ElementSelector method getDate.

/**
 * Gets a date value on this selector.  This uses a notation that allows you to target
 * the current result, or a subselection.
 * @param key The key.  E.g. "@attname" for attribute.  "tagname" for subtag.  "subselector/@attname", "subselector/tagname"
 * @param formats DateFormats to attempt to parse date with.
 * @return
 */
public Date getDate(String key, DateFormat... formats) {
    String o = getString(key, null);
    if (o == null) {
        return null;
    }
    String strval = String.valueOf(o);
    if (strval.isEmpty()) {
        return null;
    }
    for (DateFormat fmt : formats) {
        try {
            return fmt.parse(strval);
        } catch (Throwable t) {
        }
    }
    throw new RuntimeException("Failed to parse key " + key + " value " + o + " using any of the provided date formatters.");
}
Also used : DateFormat(com.codename1.l10n.DateFormat)

Example 2 with DateFormat

use of com.codename1.l10n.DateFormat in project CodenameOne by codenameone.

the class TimeChart method drawXLabels.

/**
 * The graphical representation of the labels on the X axis.
 *
 * @param xLabels the X labels values
 * @param xTextLabelLocations the X text label locations
 * @param canvas the canvas to paint to
 * @param paint the paint to be used for drawing
 * @param left the left value of the labels area
 * @param top the top value of the labels area
 * @param bottom the bottom value of the labels area
 * @param xPixelsPerUnit the amount of pixels per one unit in the chart labels
 * @param minX the minimum value on the X axis in the chart
 * @param maxX the maximum value on the X axis in the chart
 */
@Override
protected void drawXLabels(List<Double> xLabels, Double[] xTextLabelLocations, Canvas canvas, Paint paint, int left, int top, int bottom, double xPixelsPerUnit, double minX, double maxX) {
    int length = xLabels.size();
    if (length > 0) {
        boolean showLabels = mRenderer.isShowLabels();
        boolean showGridY = mRenderer.isShowGridY();
        boolean showTickMarks = mRenderer.isShowTickMarks();
        DateFormat format = getDateFormat(xLabels.get(0), xLabels.get(length - 1));
        for (int i = 0; i < length; i++) {
            long label = Math.round(xLabels.get(i));
            float xLabel = (float) (left + xPixelsPerUnit * (label - minX));
            if (showLabels) {
                paint.setColor(mRenderer.getXLabelsColor());
                if (showTickMarks) {
                    canvas.drawLine(xLabel, bottom, xLabel, bottom + mRenderer.getLabelsTextSize() / 3, paint);
                }
                drawText(canvas, format.format(new Date(label)), xLabel, bottom + mRenderer.getLabelsTextSize() * 4 / 3 + mRenderer.getXLabelsPadding(), paint, mRenderer.getXLabelsAngle());
            }
            if (showGridY) {
                paint.setColor(mRenderer.getGridColor(0));
                canvas.drawLine(xLabel, bottom, xLabel, top, paint);
            }
        }
    }
    drawXTextLabels(xTextLabelLocations, canvas, paint, true, left, top, bottom, xPixelsPerUnit, minX, maxX);
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Paint(com.codename1.charts.compat.Paint) Date(java.util.Date)

Example 3 with DateFormat

use of com.codename1.l10n.DateFormat in project CodenameOne by codenameone.

the class JavaSEPort method getLocalizationManager.

/**
 * @inheritDoc
 */
public L10NManager getLocalizationManager() {
    if (l10n == null) {
        final Locale l = Locale.getDefault();
        l10n = new L10NManager(l.getLanguage(), l.getCountry()) {

            public double parseDouble(String localeFormattedDecimal) {
                try {
                    return NumberFormat.getNumberInstance().parse(localeFormattedDecimal).doubleValue();
                } catch (ParseException err) {
                    return Double.parseDouble(localeFormattedDecimal);
                }
            }

            @Override
            public String getLongMonthName(Date date) {
                java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("MMMM", l);
                return fmt.format(date);
            }

            @Override
            public String getShortMonthName(Date date) {
                java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("MMM", l);
                return fmt.format(date);
            }

            public String format(int number) {
                return NumberFormat.getNumberInstance().format(number);
            }

            public String format(double number) {
                return NumberFormat.getNumberInstance().format(number);
            }

            public String formatCurrency(double currency) {
                return NumberFormat.getCurrencyInstance().format(currency);
            }

            public String formatDateLongStyle(Date d) {
                return DateFormat.getDateInstance(DateFormat.LONG).format(d);
            }

            public String formatDateShortStyle(Date d) {
                return DateFormat.getDateInstance(DateFormat.SHORT).format(d);
            }

            public String formatDateTime(Date d) {
                return DateFormat.getDateTimeInstance().format(d);
            }

            public String formatDateTimeMedium(Date d) {
                DateFormat dd = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
                return dd.format(d);
            }

            public String formatDateTimeShort(Date d) {
                DateFormat dd = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
                return dd.format(d);
            }

            public String getCurrencySymbol() {
                return NumberFormat.getInstance().getCurrency().getSymbol();
            }

            public void setLocale(String locale, String language) {
                super.setLocale(locale, language);
                Locale l = new Locale(language, locale);
                Locale.setDefault(l);
            }
        };
    }
    return l10n;
}
Also used : Locale(java.util.Locale) AttributedString(java.text.AttributedString) Point(java.awt.Point) L10NManager(com.codename1.l10n.L10NManager) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) FontRenderContext(java.awt.font.FontRenderContext)

Example 4 with DateFormat

use of com.codename1.l10n.DateFormat in project CodenameOne by codenameone.

the class AndroidImplementation method getLocalizationManager.

/**
 * @inheritDoc
 */
public L10NManager getLocalizationManager() {
    if (l10n == null) {
        final Locale l = Locale.getDefault();
        l10n = new L10NManager(l.getLanguage(), l.getCountry()) {

            public double parseDouble(String localeFormattedDecimal) {
                try {
                    return NumberFormat.getNumberInstance().parse(localeFormattedDecimal).doubleValue();
                } catch (ParseException err) {
                    return Double.parseDouble(localeFormattedDecimal);
                }
            }

            @Override
            public String getLongMonthName(Date date) {
                java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("MMMM", l);
                return fmt.format(date);
            }

            @Override
            public String getShortMonthName(Date date) {
                java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("MMM", l);
                return fmt.format(date);
            }

            public String format(int number) {
                return NumberFormat.getNumberInstance().format(number);
            }

            public String format(double number) {
                return NumberFormat.getNumberInstance().format(number);
            }

            public String formatCurrency(double currency) {
                return NumberFormat.getCurrencyInstance().format(currency);
            }

            public String formatDateLongStyle(Date d) {
                return DateFormat.getDateInstance(DateFormat.LONG).format(d);
            }

            public String formatDateShortStyle(Date d) {
                return DateFormat.getDateInstance(DateFormat.SHORT).format(d);
            }

            public String formatDateTime(Date d) {
                return DateFormat.getDateTimeInstance().format(d);
            }

            public String formatDateTimeMedium(Date d) {
                DateFormat dd = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
                return dd.format(d);
            }

            public String formatDateTimeShort(Date d) {
                DateFormat dd = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
                return dd.format(d);
            }

            public String getCurrencySymbol() {
                return NumberFormat.getInstance().getCurrency().getSymbol();
            }

            public void setLocale(String locale, String language) {
                super.setLocale(locale, language);
                Locale l = new Locale(language, locale);
                Locale.setDefault(l);
            }
        };
    }
    return l10n;
}
Also used : Locale(java.util.Locale) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Paint(android.graphics.Paint) L10NManager(com.codename1.l10n.L10NManager) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with DateFormat

use of com.codename1.l10n.DateFormat in project CodenameOne by codenameone.

the class SimpleDateFormatTest method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    final Form hi = new Form("Hi World", BoxLayout.y());
    TextField dateStringIn = new TextField();
    TextField dateFormat = new TextField();
    Label shortMonth = new Label();
    Label longMonth = new Label();
    Label shortDate = new Label();
    Label longDate = new Label();
    Label dateTime = new Label();
    Label formattedString = new Label();
    Container result = new Container(BoxLayout.y());
    Button parse = new Button("Parse Date");
    Container resultWrapper = new Container(BoxLayout.y());
    SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm a");
    dateFormatter.format(new Date());
    parse.addActionListener(evt -> {
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat(dateFormat.getText());
            Date dt = dateStringIn.getText().length() == 0 ? new Date() : inputFormat.parse(dateStringIn.getText());
            SimpleDateFormat shortMonthFormat = new SimpleDateFormat("MMM");
            shortMonth.setText(shortMonthFormat.format(dt));
            SimpleDateFormat longMonthFormat = new SimpleDateFormat("MMMM");
            longMonth.setText(longMonthFormat.format(dt));
            longDate.setText(L10NManager.getInstance().formatDateLongStyle(dt));
            shortDate.setText(L10NManager.getInstance().formatDateShortStyle(dt));
            dateTime.setText(L10NManager.getInstance().formatDateTime(dt));
            formattedString.setText(inputFormat.format(dt));
            resultWrapper.removeAll();
            resultWrapper.add(result);
            hi.revalidateWithAnimationSafety();
        } catch (ParseException ex) {
            Log.e(ex);
            ToastBar.showErrorMessage("Parse failed: " + ex.getMessage());
        }
    });
    result.addAll(new Label("Short month name:"), shortMonth, new Label("Long month name:"), longMonth, new Label("Short Date:"), shortDate, new Label("Long Date:"), longDate, new Label("Time: "), dateTime, new Label("Formatted String:"), formattedString);
    hi.addAll(new Label("Date:"), dateStringIn, new Label("Date Format:"), dateFormat, parse, resultWrapper);
    hi.show();
}
Also used : Container(com.codename1.ui.Container) Form(com.codename1.ui.Form) Button(com.codename1.ui.Button) Label(com.codename1.ui.Label) TextField(com.codename1.ui.TextField) ParseException(com.codename1.l10n.ParseException) SimpleDateFormat(com.codename1.l10n.SimpleDateFormat) Date(java.util.Date)

Aggregations

Date (java.util.Date)4 DateFormat (com.codename1.l10n.DateFormat)3 SimpleDateFormat (com.codename1.l10n.SimpleDateFormat)3 DateFormat (java.text.DateFormat)3 L10NManager (com.codename1.l10n.L10NManager)2 Button (com.codename1.ui.Button)2 Container (com.codename1.ui.Container)2 Form (com.codename1.ui.Form)2 Label (com.codename1.ui.Label)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Locale (java.util.Locale)2 Paint (android.graphics.Paint)1 Paint (com.codename1.charts.compat.Paint)1 FloatingActionButton (com.codename1.components.FloatingActionButton)1 SpanLabel (com.codename1.components.SpanLabel)1 ParseException (com.codename1.l10n.ParseException)1 TextField (com.codename1.ui.TextField)1 BorderLayout (com.codename1.ui.layouts.BorderLayout)1 Picker (com.codename1.ui.spinner.Picker)1