Java Code Examples for java.text.DateFormat.setLenient()

The following are Jave code examples for showing how to use setLenient() of the java.text.DateFormat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: NotifyTools   File: DateTimeConverter.java   Source Code and License Vote up 6 votes
/**
 * Parse a String into a <code>Calendar</code> object
 * using the specified <code>DateFormat</code>.
 *
 * @param sourceType The type of the value being converted
 * @param targetType The type to convert the value to
 * @param value The String date value.
 * @param format The DateFormat to parse the String value.
 *
 * @return The converted Calendar object.
 * @throws ConversionException if the String cannot be converted.
 */
private Calendar parse(final Class<?> sourceType, final Class<?> targetType, final String value, final DateFormat format) {
    logFormat("Parsing", format);
    format.setLenient(false);
    final ParsePosition pos = new ParsePosition(0);
    final Date parsedDate = format.parse(value, pos); // ignore the result (use the Calendar)
    if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedDate == null) {
        String msg = "Error converting '" + toString(sourceType) + "' to '" + toString(targetType) + "'";
        if (format instanceof SimpleDateFormat) {
            msg += " using pattern '" + ((SimpleDateFormat)format).toPattern() + "'";
        }
        if (log().isDebugEnabled()) {
            log().debug("    " + msg);
        }
        throw new ConversionException(msg);
    }
    final Calendar calendar = format.getCalendar();
    return calendar;
}
 
Example 2
Project: validators   File: DateTimeValidator.java   Source Code and License Vote up 6 votes
@Override
public boolean isValid(CharSequence sequence, ConstraintValidatorContext context) {
    if (sequence == null || sequence.length() == 0) {
        return true;
    }

    final String value = sequence.toString();

    for (String pattern : annotation.patterns()) {
        final DateFormat format = new SimpleDateFormat(pattern);
        format.setLenient(annotation.lenient());

        try {
            format.parse(value);
            return true;
        } catch (@SuppressWarnings("unused") ParseException e) {
            continue;
        }

    }
    return false;
}
 
Example 3
Project: calcite-avatica   File: DateTimeUtils.java   Source Code and License Vote up 6 votes
/**
 * Parses a string using {@link SimpleDateFormat} and a given pattern. This
 * method parses a string at the specified parse position and if successful,
 * updates the parse position to the index after the last character used.
 * The parsing is strict and requires months to be less than 12, days to be
 * less than 31, etc.
 *
 * @param s       string to be parsed
 * @param dateFormat Date format
 * @param tz      time zone in which to interpret string. Defaults to the Java
 *                default time zone
 * @param pp      position to start parsing from
 * @return a Calendar initialized with the parsed value, or null if parsing
 * failed. If returned, the Calendar is configured to the GMT time zone.
 */
private static Calendar parseDateFormat(String s, DateFormat dateFormat,
    TimeZone tz, ParsePosition pp) {
  if (tz == null) {
    tz = DEFAULT_ZONE;
  }
  Calendar ret = Calendar.getInstance(tz, Locale.ROOT);
  dateFormat.setCalendar(ret);
  dateFormat.setLenient(false);

  final Date d = dateFormat.parse(s, pp);
  if (null == d) {
    return null;
  }
  ret.setTime(d);
  ret.setTimeZone(UTC_ZONE);
  return ret;
}
 
Example 4
Project: NotifyTools   File: DateLocaleConverter.java   Source Code and License Vote up 5 votes
/**
 * Convert the specified locale-sensitive input object into an output object of the
 * specified type.
 *
 * @param value The input object to be converted
 * @param pattern The pattern is used for the convertion
 * @return the converted Date value
 *
 * @throws org.apache.commons.beanutils.ConversionException
 * if conversion cannot be performed successfully
 * @throws ParseException if an error occurs parsing
 */
@Override
protected Object parse(final Object value, String pattern) throws ParseException {

    // Handle Date
    if (value instanceof java.util.Date) {
        return value;
    }

    // Handle Calendar
    if (value instanceof java.util.Calendar) {
        return ((java.util.Calendar)value).getTime();
    }

     if (locPattern) {
         pattern = convertLocalizedPattern(pattern, locale);
     }

     // Create Formatter - use default if pattern is null
     final DateFormat formatter = pattern == null ? DateFormat.getDateInstance(DateFormat.SHORT, locale)
                                            : new SimpleDateFormat(pattern, locale);
     formatter.setLenient(isLenient);


     // Parse the Date
    final ParsePosition pos = new ParsePosition(0);
    final String strValue = value.toString();
    final Object parsedValue = formatter.parseObject(strValue, pos);
    if (pos.getErrorIndex() > -1) {
        throw new ConversionException("Error parsing date '" + value +
                "' at position="+ pos.getErrorIndex());
    }
    if (pos.getIndex() < strValue.length()) {
        throw new ConversionException("Date '" + value +
                "' contains unparsed characters from position=" + pos.getIndex());
    }

    return parsedValue;
 }
 
Example 5
Project: GitHub   File: HttpDate.java   Source Code and License Vote up 5 votes
@Override protected DateFormat initialValue() {
  // RFC 2616 specified: RFC 822, updated by RFC 1123 format with fixed GMT.
  DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
  rfc1123.setLenient(false);
  rfc1123.setTimeZone(UTC);
  return rfc1123;
}
 
Example 6
Project: GitHub   File: HttpDate.java   Source Code and License Vote up 5 votes
@Override protected DateFormat initialValue() {
  // Date format specified by RFC 7231 section 7.1.1.1.
  DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
  rfc1123.setLenient(false);
  rfc1123.setTimeZone(UTC);
  return rfc1123;
}
 
Example 7
Project: lams   File: DateFormatter.java   Source Code and License Vote up 5 votes
protected DateFormat getDateFormat(Locale locale) {
	DateFormat dateFormat = createDateFormat(locale);
	if (this.timeZone != null) {
		dateFormat.setTimeZone(this.timeZone);
	}
	dateFormat.setLenient(this.lenient);
	return dateFormat;
}
 
Example 8
Project: data-binding-validator   File: AbstractCalendarValidator.java   Source Code and License Vote up 5 votes
/**
 * <p>Returns a <code>DateFormat</code> for the specified Locale.</p>
 *
 * @param locale The locale a <code>DateFormat</code> is required for,
 *        system default if null.
 * @return The <code>DateFormat</code> to created.
 */
protected Format getFormat(Locale locale) {

    DateFormat formatter = null;
    if (dateStyle >= 0 && timeStyle >= 0) {
        if (locale == null) {
            formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle);
        } else {
            formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
        }
    } else if (timeStyle >= 0) {
        if (locale == null) {
            formatter = DateFormat.getTimeInstance(timeStyle);
        } else {
            formatter = DateFormat.getTimeInstance(timeStyle, locale);
        }
    } else {
        int useDateStyle = dateStyle >= 0 ? dateStyle : DateFormat.SHORT;
        if (locale == null) {
            formatter = DateFormat.getDateInstance(useDateStyle);
        } else {
            formatter = DateFormat.getDateInstance(useDateStyle, locale);
        }
    }
    formatter.setLenient(false);
    return formatter;

}
 
Example 9
Project: lemon   File: DateConverter.java   Source Code and License Vote up 4 votes
public Date tryConvert(String text, String pattern) {
    DateFormat dateFormat = new SimpleDateFormat(pattern);
    dateFormat.setLenient(false);

    try {
        return dateFormat.parse(text);
    } catch (ParseException ex) {
        logger.debug(ex.getMessage(), ex);
    }

    return null;
}
 
Example 10
Project: mumu   File: SystemUserController.java   Source Code and License Vote up 4 votes
@InitBinder
public void initBinder(WebDataBinder binder) {
	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	dateFormat.setLenient(true);
	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
}
 
Example 11
Project: boohee_v5.6   File: HttpDate$1.java   Source Code and License Vote up 4 votes
protected DateFormat initialValue() {
    DateFormat rfc1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
    rfc1123.setLenient(false);
    rfc1123.setTimeZone(HttpDate.access$000());
    return rfc1123;
}
 
Example 12
Project: mbed-cloud-sdk-java   File: ApiMetadata.java   Source Code and License Vote up 2 votes
/**
 * Sets the date from a string representation.
 * 
 * @param dateString
 *            the date to set
 * @throws MbedCloudException
 *             if string cannot be interpreted as a date
 */
public synchronized void setDateFromString(String dateString) throws MbedCloudException {
    final DateFormat format = REQUEST_DATE_FORMAT;
    format.setLenient(true);
    setDate(TranslationUtils.convertTimestamp(dateString, format));
}