Example usage for java.text DateFormat setLenient

List of usage examples for java.text DateFormat setLenient

Introduction

In this page you can find the example usage for java.text DateFormat setLenient.

Prototype

public void setLenient(boolean lenient) 

Source Link

Document

Specify whether or not date/time parsing is to be lenient.

Usage

From source file:ch.qos.logback.core.pattern.parser2.PatternParser.java

private static DateFormat parseDateFormat(String option) {
    TimeZone tz = null;/*from w  w  w  . ja v  a2s.c o m*/

    // default to ISO8601 if no conversion pattern given
    if (option == null || option.isEmpty() || option.equalsIgnoreCase(CoreConstants.ISO8601_STR)) {
        option = CoreConstants.ISO8601_PATTERN;
    }

    // Parse the last option in the conversion pattern as a time zone.
    // Make sure the comma is not escaped/quoted.
    int idx = option.lastIndexOf(",");
    if ((idx > -1) && (idx + 1 < option.length() && !ParserUtil.isEscaped(option, idx)
            && !ParserUtil.isQuoted(option, idx))) {

        // make sure the string isn't the millisecond pattern, which
        // can appear after a comma
        String tzStr = option.substring(idx + 1).trim();
        if (!tzStr.startsWith("SSS")) {
            option = option.substring(0, idx);
            tz = TimeZone.getTimeZone(tzStr);
            if (!tz.getID().equalsIgnoreCase(tzStr)) {
                logger().warn("Time zone (\"{}\") defaulting to \"{}\".", tzStr, tz.getID());
            }
        }
    }

    // strip quotes from date format because SimpleDateFormat doesn't understand them
    if (option.length() > 1 && option.startsWith("\"") && option.endsWith("\"")) {
        option = option.substring(1, option.length() - 1);
    }

    DateFormat format = new SimpleDateFormat(option);
    format.setLenient(true);

    if (tz != null) {
        format.setTimeZone(tz);
    }

    return format;
}

From source file:com.jd.survey.service.security.UserService.java

public static boolean isDateValid(String date, String dateFormat) {
    try {/*from ww  w . ja v a  2s .  com*/
        DateFormat df = new SimpleDateFormat(dateFormat);
        df.setLenient(false);
        df.parse(date);
        return true;
    } catch (ParseException e) {
        return false;
    }
}

From source file:org.apache.oozie.extensions.OozieELExtensions.java

private static DateFormat getISO8601DateFormat(TimeZone tz) {
    DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
    // Stricter parsing to prevent dates such as 2011-12-50T01:00Z (December 50th) from matching
    dateFormat.setLenient(false);
    dateFormat.setTimeZone(tz);//from w  w w  .j  a v  a 2s . co m
    return dateFormat;
}

From source file:com.jeeframework.util.validate.GenericTypeValidator.java

/**
 *  <p>/*from   w w w .j  a v a  2  s. com*/
 *
 *  Checks if the field is a valid date. The <code>Locale</code> is used
 *  with <code>java.text.DateFormat</code>. The setLenient method is set to
 *  <code>false</code> for all.</p>
 *
 *@param  value   The value validation is being performed on.
 *@param  locale  The Locale to use to parse the date (system default if
 *      null)
 *@return the converted Date value.
 */
public static Date formatDate(String value, Locale locale) {
    Date date = null;

    if (value == null) {
        return null;
    }

    try {
        DateFormat formatter = null;
        if (locale != null) {
            formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
        } else {
            formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
        }

        formatter.setLenient(false);

        date = formatter.parse(value);
    } catch (ParseException e) {
        // Bad date so return null
        Log log = LogFactory.getLog(GenericTypeValidator.class);
        if (log.isDebugEnabled()) {
            log.debug("Date parse failed value=[" + value + "], " + "locale=[" + locale + "] " + e);
        }
    }

    return date;
}

From source file:org.lingcloud.molva.ocl.util.GenericTypeValidator.java

/**
 * <p>//  ww w .j  a va2s  . c  o m
 * 
 * Checks if the field is a valid date. The <code>Locale</code> is used with
 * <code>java.text.DateFormat</code>. The setLenient method is set to
 * <code>false</code> for all.
 * </p>
 * 
 * @param value
 *            The value validation is being performed on.
 * @param locale
 *            The Locale to use to parse the date (system default if null)
 * @return the converted Date value.
 */
public static Date formatDate(String value, Locale locale) {
    Date date = null;

    if (value == null) {
        return null;
    }

    try {
        DateFormat formatter = null;
        if (locale != null) {
            formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
        } else {
            formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
        }

        formatter.setLenient(false);

        date = formatter.parse(value);
    } catch (ParseException e) {
        // Bad date so return null
        if (log.isDebugEnabled()) {
            log.debug("Date parse failed value=[" + value + "], " + "locale=[" + locale + "] " + e);
        }
    }

    return date;
}

From source file:controller.InputSanitizer.java

/**
 * Ueberprueft ob ein Datum day.month.year ein gueltiges datum ergibt
 * @param day//from w w  w .  j av  a 2s  .  com
 * @param month
 * @param year
 * @return Date.getTime () if valid
 * else error message
 */
public static String checkDate(String day, String month, String year) {
    String result = null;

    DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
    Calendar cal = new GregorianCalendar();

    Date projectStartDate = null;
    String projectStart = day + "." + month + "." + year;

    int monthInt = Integer.parseInt(month) - 1;
    int dayInt = Integer.parseInt(day);

    cal.set(Calendar.MONTH, monthInt);
    int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    if (dayInt <= maxDay) {
        try {
            formatter.setLenient(false);
            projectStartDate = formatter.parse(projectStart);
            result = Long.toString(projectStartDate.getTime());
        } catch (ParseException e) {
            result = "Ihre Eingabe ist kein g&uuml;ltiges Datum.";
        }
    } else {
        result = "Ihre Eingabe ist kein g&uuml;ltiges Datum.";
    }

    return result;
}

From source file:org.jdal.vaadin.data.converter.DateConverter.java

@Override
protected DateFormat getFormat(Locale locale) {
    if (locale == null)
        locale = LocaleContextHolder.getLocale();

    DateFormat f = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
    f.setLenient(false);

    return f;//from w ww  .ja v a  2s.c  om
}

From source file:grails.plugins.crm.core.CustomDateEditor.java

@Override
public void setAsText(final String text) throws IllegalArgumentException {
    if (!StringUtils.hasText(text)) {
        setValue(null); // Treat empty String as null value.
    } else {//from w ww  .ja va 2  s .  co m
        Exception error = null;
        java.util.Date date = null;
        for (int i = 0; i < DATE_FORMATS.length; i++) {
            try {
                DateFormat df = new SimpleDateFormat(DATE_FORMATS[i]);
                df.setLenient(false);
                date = df.parse(text);
            } catch (ParseException ex) {
                if (error == null) {
                    error = ex;
                }
            }
            if (date != null) {
                setValue(sql ? new java.sql.Date(date.getTime()) : date);
                error = null;
                break; // We've found a valid date.
            }
        }
        if (error != null) {
            throw new IllegalArgumentException("Could not parse date: " + error.getMessage());
        }
    }
}

From source file:com.googlecode.jsfFlex.component.ext.AbstractFlexUIDateField.java

@Override
public void decode(FacesContext context) {
    super.decode(context);

    java.util.Map<String, String> requestMap = context.getExternalContext().getRequestParameterMap();

    String selectedDateId = getId() + SELECTED_DATE_ID_APPENDED;
    String selectedDateUpdateVal = requestMap.get(selectedDateId);

    if (selectedDateUpdateVal != null && selectedDateUpdateVal.length() > 0) {
        /*//from www .  ja  va2  s . co m
         * HACK: Since ActionScript returns date in format of "Thu Aug 23 00:00:00 GMT-0700 2009"
         * and "EEE MMM dd HH:mm:ss zZ yyyy" pattern doesn't seem to match it within SimpleDateFormat,
         * place a space between z + Z
         */
        int dashIndex = selectedDateUpdateVal.indexOf("-");
        if (dashIndex != -1) {
            selectedDateUpdateVal = selectedDateUpdateVal.substring(0, dashIndex) + " "
                    + selectedDateUpdateVal.substring(dashIndex);
        }
        Calendar instance = Calendar.getInstance();
        try {
            DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_DEFAULT);
            dateFormat.setLenient(true);
            instance.setTime(dateFormat.parse(selectedDateUpdateVal));
            setSelectedDate(instance);
        } catch (ParseException parsingException) {
            setValid(false);
            context.addMessage(getId(),
                    new FacesMessage("Parsing exception for value : " + selectedDateUpdateVal));
            _log.error("Parsing exception for value : " + selectedDateUpdateVal, parsingException);
        }
    }
}

From source file:com.haulmont.chile.core.datatypes.impl.TimeDatatype.java

@Override
public String format(Object value, Locale locale) {
    if (value == null) {
        return "";
    }//from   ww w .  j  ava2s .c  o m

    FormatStrings formatStrings = AppBeans.get(FormatStringsRegistry.class).getFormatStrings(locale);
    if (formatStrings == null) {
        return format(value);
    }

    DateFormat format = new SimpleDateFormat(formatStrings.getTimeFormat());
    format.setLenient(false);

    return format.format(value);
}