Example usage for com.google.gwt.i18n.shared DateTimeFormat parse

List of usage examples for com.google.gwt.i18n.shared DateTimeFormat parse

Introduction

In this page you can find the example usage for com.google.gwt.i18n.shared DateTimeFormat parse.

Prototype

public Date parse(String text) throws IllegalArgumentException 

Source Link

Document

Parses text to produce a Date value.

Usage

From source file:com.vaadin.client.DateTimeService.java

License:Apache License

/**
 * Parses the given date string using the given format string and the locale
 * set in this com.vaadin.client.DateTimeService instance.
 *
 * @param dateString//from w  w w .  ja  va 2  s  .  com
 *            Date string e.g. "1 February 2010"
 * @param formatString
 *            Format string e.g. "d MMMM yyyy"
 * @param lenient
 *            true to use lenient parsing, false to use strict parsing
 * @return A Date object representing the dateString. Never returns null.
 * @throws IllegalArgumentException
 *             if the parsing fails
 *
 */
public Date parseDate(String dateString, String formatString, boolean lenient) throws IllegalArgumentException {

    /* DateTimeFormat uses the browser's locale */
    DateTimeFormat format = DateTimeFormat.getFormat(formatString);

    /*
     * Parse month names separately when locale for the com.vaadin.client.DateTimeService is
     * not the same as the browser locale
     */
    dateString = parseMonthName(dateString, formatString);

    Date date = null;

    if ("h".equalsIgnoreCase(dateString) || "t".equalsIgnoreCase(dateString)) {

        //todo: recordar fechas especiales, para conversin inversa

        date = new Date();

    } else {

        //todo: tener en cuenta los locales
        boolean inverso = false;
        String z = formatString.toLowerCase();
        if (z.indexOf("m") < z.indexOf("d"))
            inverso = true;

        Date h = new Date();

        if (dateString == null) {
        } else {
            if (dateString.contains(" ")) {
                String aux = dateString;
                String hourString = dateString.split(" ")[1].replaceAll("[^\\d]", "");
                dateString = dateString.split(" ")[0].replaceAll("[^\\d]", "");
                if (dateString.length() == 2) { // siempre es el da del mes actual
                    date = DateTimeFormat.getFormat((inverso) ? "yyyyMMddHHmm" : "ddMMyyyyHHmm")
                            .parse((inverso)
                                    ? "" + (1900 + h.getYear()) + ((h.getMonth() <= 10) ? "0" : "")
                                            + (h.getMonth() + 1) + dateString + hourString
                                    : dateString + ((h.getMonth() <= 10) ? "0" : "") + (h.getMonth() + 1)
                                            + (1900 + h.getYear()) + hourString);
                } else if (dateString.length() == 4) { // dia y mes del ao actual
                    date = DateTimeFormat.getFormat((inverso) ? "yyyyMMddHHmm" : "ddMMyyyyHHmm")
                            .parse((inverso) ? "" + (1900 + h.getYear()) + dateString + hourString
                                    : dateString + (1900 + h.getYear()) + hourString);
                } else if (dateString.length() == 6) { // dia, mes y ao del ao 20xx
                    date = DateTimeFormat.getFormat((inverso) ? "yyyyMMddHHmm" : "ddMMyyyyHHmm")
                            .parse((inverso) ? "20" + dateString + hourString
                                    : dateString.substring(0, 4) + "20" + dateString.substring(4) + hourString);
                } else if (dateString.length() == 8) { // dia mes y ao sin separadores
                    date = DateTimeFormat.getFormat((inverso) ? "yyyyMMddHHmm" : "ddMMyyyyHHmm")
                            .parse((inverso) ? dateString + hourString : dateString + hourString);
                } else if (lenient) {
                    date = format.parse(aux);
                } else {
                    date = format.parseStrict(aux);
                }
            } else {
                String aux = dateString;
                dateString = dateString.replaceAll("[^\\d]", "");
                if (dateString.length() == 2) { // siempre es el da del mes actual
                    date = DateTimeFormat.getFormat((inverso) ? "yyyyMMdd" : "ddMMyyyy")
                            .parse((inverso)
                                    ? "" + (1900 + h.getYear()) + ((h.getMonth() <= 10) ? "0" : "")
                                            + (h.getMonth() + 1) + dateString
                                    : dateString + ((h.getMonth() <= 10) ? "0" : "") + (h.getMonth() + 1)
                                            + (1900 + h.getYear()));
                } else if (dateString.length() == 4) { // dia y mes del ao actual
                    date = DateTimeFormat.getFormat((inverso) ? "yyyyMMdd" : "ddMMyyyy")
                            .parse((inverso) ? "" + (1900 + h.getYear()) + dateString
                                    : dateString + (1900 + h.getYear()));
                } else if (dateString.length() == 6) { // dia, mes y ao del ao 20xx
                    date = DateTimeFormat.getFormat((inverso) ? "yyyyMMdd" : "ddMMyyyy")
                            .parse((inverso) ? "20" + dateString
                                    : dateString.substring(0, 4) + "20" + dateString.substring(4));
                } else if (dateString.length() == 8) { // dia mes y ao sin separadores
                    date = DateTimeFormat.getFormat((inverso) ? "yyyyMMdd" : "ddMMyyyy")
                            .parse((inverso) ? dateString : dateString);
                } else if (lenient) {
                    date = format.parse(aux);
                } else {
                    date = format.parseStrict(aux);
                }
            }
        }

    }

    // Some version of Firefox sets the timestamp to 0 if parsing fails.
    if (date != null && date.getTime() == 0) {
        throw new IllegalArgumentException("Parsing of '" + dateString + "' failed");
    }

    return date;
}

From source file:gwt.material.design.addins.client.ui.MaterialTimePicker.java

License:Apache License

/**
 * Called after the lolliclock event <code>afterHide</code>.
 * /*from w ww  .  j  a  v  a2s  .  com*/
 * @param time
 *            The time given by lolliclock in 12-hours <code>hh:mm aa</code>
 *            format.
 */
protected void afterHide() {

    String timeString = this.getTime(this.input.getElement());

    Date parsedDate = null;

    if (timeString.equals("") == false && timeString != null) {
        try {
            if (this.hour24 == true) {
                DateTimeFormat hour24DateTimeFormat = DateTimeFormat.getFormat("HH:mm");
                parsedDate = hour24DateTimeFormat.parse(timeString);
            } else {
                DateTimeFormat hour12DateTimeFormat = DateTimeFormat.getFormat("hh:mm aa");
                parsedDate = hour12DateTimeFormat.parse(timeString);
            }
        } catch (Exception e) {
            // Silently catch parse errors
        }
    }

    this.setValue(parsedDate);

    // Remove class 'valid' after hide.
    this.validMixin.setOn(false);

    this.fireCloseEvent();
}

From source file:org.bonitasoft.web.toolkit.client.ui.ClientDateFormater.java

License:Open Source License

@Override
public Date _parse(final String value, final String format) {
    final DateTimeFormat formatter = DateTimeFormat.getFormat(format);
    return formatter.parse(value);

}