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

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

Introduction

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

Prototype

public Date parseStrict(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 www  .j  av a  2 s .  c  om
 *            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;
}