Refine search
@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; } };
final static String DATE_FORMAT = "dd-MM-yyyy"; public static boolean isDateValid(String date) { try { DateFormat df = new SimpleDateFormat(DATE_FORMAT); df.setLenient(false); df.parse(date); return true; } catch (ParseException e) { return false; } }
Date parseDate(String maybeDate, String format, boolean lenient) { Date date = null; // test date string matches format structure using regex // - weed out illegal characters and enforce 4-digit year // - create the regex based on the local format string String reFormat = Pattern.compile("d+|M+").matcher(Matcher.quoteReplacement(format)).replaceAll("\\\\d{1,2}"); reFormat = Pattern.compile("y+").matcher(reFormat).replaceAll("\\\\d{4}"); if ( Pattern.compile(reFormat).matcher(maybeDate).matches() ) { // date string matches format structure, // - now test it can be converted to a valid date SimpleDateFormat sdf = (SimpleDateFormat)DateFormat.getDateInstance(); sdf.applyPattern(format); sdf.setLenient(lenient); try { date = sdf.parse(maybeDate); } catch (ParseException e) { } } return date; } // used like this: Date date = parseDate( "21/5/2009", "d/M/yyyy", false);
localSimpleDateFormat = new ThreadLocal<SimpleDateFormat>() { protected SimpleDateFormat initialValue() { return new SimpleDateFormat(); localSimpleDateFormat = new ThreadLocal<SimpleDateFormat>() { protected SimpleDateFormat initialValue() { return new SimpleDateFormat(pattern); localSimpleDateFormat = new ThreadLocal<SimpleDateFormat>() { protected SimpleDateFormat initialValue() { return new SimpleDateFormat(pattern, formatSymbols); return localSimpleDateFormat.get().parse(source); localSimpleDateFormat.get().setLenient(lenient);
: new SimpleDateFormat(pattern, locale); formatter.setLenient(isLenient);
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy"); df.setLenient(false); df.parse("03/88/2013"); // Throws an exception
private static DateFormat strictDateFormatForPattern(String pattern) { DateFormat dateFormat = new SimpleDateFormat(pattern); dateFormat.setLenient(false); return dateFormat; }
: new SimpleDateFormat(pattern, locale); formatter.setLenient(isLenient);
dateFormat.setLenient(false); final Date d = dateFormat.parse(s, pp); if (null == d) { return null;
DateFormat fromFormat = new SimpleDateFormat("yyyy-MM-dd"); fromFormat.setLenient(false); DateFormat toFormat = new SimpleDateFormat("dd-MM-yyyy"); toFormat.setLenient(false); String dateStr = "2011-07-09"; Date date = fromFormat.parse(dateStr); System.out.println(toFormat.format(date));
@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; } };
formatter.setLenient(false); formatter.parse(value); } catch(ParseException e) { return false;
public static Date getTwitterDate(String date) throws ParseException { final String TWITTER="EEE MMM dd HH:mm:ss ZZZZZ yyyy"; SimpleDateFormat sf = new SimpleDateFormat(TWITTER); sf.setLenient(true); return sf.parse(date); } public static void main (String[] args) throws java.lang.Exception { System.out.println(getTwitterDate("Thu Dec 3 18:26:07 +0000 2010")); }
private static DateFormat strictDateFormatForPattern(String pattern) { DateFormat dateFormat = new SimpleDateFormat(pattern); dateFormat.setLenient(false); return dateFormat; }
formatterShort.setLenient(false); formatterDefault.setLenient(false); date = formatterShort.parse(value); } catch (ParseException e) { date = formatterDefault.parse(value);
public static boolean isValid(String text) { if (text == null || !text.matches("\\d{4}-[01]\\d-[0-3]\\d")) return false; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); df.setLenient(false); try { df.parse(text); return true; } catch (ParseException ex) { return false; } }
@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; } };
/** * 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; }
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm"); formatter.setLenient(false); Date curDate = new Date(); long curMillis = curDate.getTime(); String curTime = formatter.format(curDate); String oldTime = "05.01.2011, 12:45"; Date oldDate = formatter.parse(oldTime); long oldMillis = oldDate.getTime();
@Override protected DateFormat initialValue() { DateFormat val = new SimpleDateFormat("yyyy-MM-dd"); val.setLenient(false); // Without this, 2020-20-20 becomes 2021-08-20. val.setTimeZone(TimeZone.getTimeZone("UTC")); return val; } };