当前位置: 技术问答>java相关
如何判断日期的有效性
来源: 互联网 发布时间:2015-02-23
本文导语: 例如: 输入一个字符串“2001/11/11”。如何判断输入的这个日期是正确的日期。 | String tmp = "2001-02-29"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); ...
例如:
输入一个字符串“2001/11/11”。如何判断输入的这个日期是正确的日期。
输入一个字符串“2001/11/11”。如何判断输入的这个日期是正确的日期。
|
String tmp = "2001-02-29";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
if (sdf.format(sdf.parse(tmp)).equalsIgnoreCase(tmp))
System.out.println("OK");
else
System.out.println("Err");
} catch (Exception ex) {
System.out.println("Err");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
if (sdf.format(sdf.parse(tmp)).equalsIgnoreCase(tmp))
System.out.println("OK");
else
System.out.println("Err");
} catch (Exception ex) {
System.out.println("Err");
}
|
这里是javascript的,你可以修改它。用到正则表达式。
function validateUSDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
valid dates with 2 digit month, 2 digit day,
4 digit year. Date separator can be ., -, or /.
Uses combination of regular expressions and
string parsing to validate date.
Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
PARAMETERS:
strValue - String to be tested for validity
RETURNS:
True if valid, otherwise false.
REMARKS:
Avoids some of the limitations of the Date.parse()
method such as the date separator character.
*************************************************/
var objRegExp = /^d{1,2}(-|/|.)d{1,2}1d{4}$/
//check to see if in correct format
if(!objRegExp.test(strValue))
return false; //doesn't match pattern, bad date
else{
var strSeparator = strValue.substring(2,3) //find date separator
var arrayDate = strValue.split(strSeparator); //split date into month, day, year
//create a lookup for months not equal to Feb.
var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
'08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
var intDay = parseInt(arrayDate[1]);
//check if month value and day value agree
if(arrayLookup[arrayDate[0]] != null) {
if(intDay
function validateUSDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
valid dates with 2 digit month, 2 digit day,
4 digit year. Date separator can be ., -, or /.
Uses combination of regular expressions and
string parsing to validate date.
Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
PARAMETERS:
strValue - String to be tested for validity
RETURNS:
True if valid, otherwise false.
REMARKS:
Avoids some of the limitations of the Date.parse()
method such as the date separator character.
*************************************************/
var objRegExp = /^d{1,2}(-|/|.)d{1,2}1d{4}$/
//check to see if in correct format
if(!objRegExp.test(strValue))
return false; //doesn't match pattern, bad date
else{
var strSeparator = strValue.substring(2,3) //find date separator
var arrayDate = strValue.split(strSeparator); //split date into month, day, year
//create a lookup for months not equal to Feb.
var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
'08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
var intDay = parseInt(arrayDate[1]);
//check if month value and day value agree
if(arrayLookup[arrayDate[0]] != null) {
if(intDay