Here you can find the source of isDateValid(String dateToCheck, String pattern)
SimpleDateFormat
.
Parameter | Description |
---|---|
dateToCheck | The date string to check. |
pattern | The pattern to use. <p> |
true
if it contains a valid date in SimpleDateFormat
.
public static boolean isDateValid(String dateToCheck, String pattern)
//package com.java2s; /*/*from www.j a v a 2s .c om*/ * DateHelper.java * * Copyright (c) 2004-2011 Gregory Kotsaftis * gregkotsaftis@yahoo.com * http://zeus-jscl.sourceforge.net/ * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.text.ParseException; import java.text.SimpleDateFormat; public class Main { /** * Checks a string to see if it contains a valid date in * <code>SimpleDateFormat</code>. * <p> * * @param dateToCheck * The date string to check. * @param pattern * The pattern to use. * <p> * @return <code>true</code> if it contains a valid date in * <code>SimpleDateFormat</code>. */ public static boolean isDateValid(String dateToCheck, String pattern) { if (dateToCheck == null || pattern == null) return (false); boolean result = false; try { java.util.Date in = parseDate(dateToCheck, pattern); String out = dateToString(in, pattern); if (dateToCheck.compareTo(out) == 0) { result = true; } } catch (Exception e) { // result is already false } return (result); } /** * Parses a string into a date. String should be in * <code>SimpleDateFormat</code> format. e.g. * <code>java.util.Date d = parseDate(myDate, "dd/MM/yyyy");</code> * <p> * * @param myDate * The date string. * @param pattern * The pattern to use. * <p> * @return The <code>Date</code>. * <p> * @throws ParseException */ public static java.util.Date parseDate(String myDate, String pattern) throws ParseException { if (myDate == null || pattern == null) throw new ParseException("Null arguments!", 0); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); java.util.Date uDate = simpleDateFormat.parse(myDate); return (uDate); } /** * Converts a date to a string based on a <code>SimpleDateFormat</code> * pattern. e.g. <code>String s = dateToString(uDate, "dd/MM/yyyy");</code> * <p> * * @param uDate * The date string. * @param pattern * The pattern to use. * <p> * @return The string of the date or <code>null</code> on error. */ public static String dateToString(java.util.Date uDate, String pattern) { if (uDate == null || pattern == null) return (null); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String myDate = simpleDateFormat.format(uDate); return (myDate); } }