DateHelper: isValidDate(String dt)


package de.eknori.ssjsplus.javascript;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateHelper {

	/**
	 * @author Ulrich krause
	 * @since 2012/05/06
	 * @return true/false Boolean 
	 */
	public boolean isValidDate(String dt) {

		if (dt == null)
			return false;

		// set the format to use as a constructor argument
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				this.getDateFormatPattern());

		if (dt.trim().length() != dateFormat.toPattern().length())
			return false;

		dateFormat.setLenient(false);

		try {
			// parse the inDate parameter
			dateFormat.parse(dt.trim());
		} catch (ParseException pe) {
			return false;
		}
		return true;
	}

	/**
	 * @author Ulrich krause
	 * @since 2012/05/06
	 * @return DateFormatPattern String (i.e. yyyy/MM/dd )
	 */
	public String getDateFormatPattern() {
		SimpleDateFormat df = (SimpleDateFormat) DateFormat
				.getDateInstance(DateFormat.MEDIUM);
		return df.toPattern();
	}
}
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...