Here you can find the source of isValidDate(String date, String format)
Parameter | Description |
---|---|
date | A date string. |
format | Date format to conform to. |
public static boolean isValidDate(String date, String format)
//package com.java2s; /*/* w w w .ja v a2 s.c o m*/ * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF * Institute of Systems Science, National University of Singapore * * Copyright 2012 Team 4(Part-Time), ISS, NUS, Singapore. All rights reserved. * Use of this source code is subjected to the terms of the applicable license * agreement. * * ----------------------------------------------------------------- * REVISION HISTORY * ----------------------------------------------------------------- * DATE AUTHOR REVISION DESCRIPTION * 10 March 2012 Chen Changfeng 0.1 Class creating * * * * * */ import java.text.SimpleDateFormat; import java.util.*; public class Main { private static String dateFormat = "dd/MM/yyyy"; /** * Tests the input value to ensure that a valid Date instance can be created from it. * Roll over dates are not allowed here and will return a false value. * Eg. isValidDate(2002, 10, 32) will return false. * <p> * @param year The year value. * @param month The month value. ( 1 - 12 ) * @param day The day value. ( 1 - 31 ) * @return True if all values can be used to create a single valid Date instance. */ public static boolean isValidDate(int year, int month, int day) { if (day <= 0 || month <= 0 || year <= 0) return false; if (month > 12 || day > 31) return false; Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month - 1); // Find the maximum field value possible for the day with the year and month. int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); if (day > maxDay) return false; return true; } /** * Tests the input string to ensure that a valid Date instance can be created from it * according to the format provided. * <p> * @param date A date string. * @param format Date format to conform to. * @return True if it conforms to the specified date format; False otherwise. */ public static boolean isValidDate(String date, String format) { if (date == null) return false; try { parse(date, format); } catch (java.text.ParseException e) { return false; } return true; } /** * Tests the input string to ensure that a valid Date instance can be created * according to the date format specified in the System property. * <p> * If the properties file is not available or the dateformat property has * not been specified, the default format "dd/MM/yyyy" will be used. * <p> * @param date A date string. * @return True if it conforms to the system date format; False otherwise. */ public static boolean isValidDate(String date) { if (date == null) return false; try { parse(date); } catch (java.text.ParseException e) { return false; } return true; } /** * Returns a Date object instance from the input string. * The input string is parsed to return a date object based * on the date format specified in the System property. * <p> * @param date The date string. * @return The date instance created. * @throws java.text.ParseException - if the beginning of the specified string cannot be parsed. */ public static Date parse(String date) throws java.text.ParseException { // Modified by Aris on 02/02/2009 for case 1296351 //if ( date == null || date.length()==0) return null; if (date == null) return null; String dateformat = null; // Use the default format of "dd/MM/yyyy". dateformat = dateFormat; return parse(date, dateformat); } /** * Returns a Date object instance from the input string. * The input date string is parsed to return a date object * based on the format provided. * <p> * Eg., to parse the date string "01/01/2003 9:2 PM", use the * format "dd/MM/yyyy h:m a". The resultant data object will have * the value "Mar 11 2003 09:02", as displayed in 24 hr format. * <p> * @param date The date string. * @param format The date format that the date string conforms to. * @return The date instance created. * @throws java.text.ParseException - if the beginning of the specified string cannot be parsed. */ public static Date parse(String date, String format) throws java.text.ParseException { SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setLenient(false); return sdf.parse(date); } }