一、String类型
/*****************************************************
* 函数名称:CheckDate
* 机能概要: 日期类型CHECK
*
* 参数: strYear CHECK的年份
* strMonth CHECK的月份
* strDate CHECK的日期
* 返回值: "111": 输入的日期正确
* ^^^
* |||________________年:1:正确 0:错误
* ||_________________月:1:正确 0:错误
* |__________________日:1:正确 0:错误
*********************************************************/public static String CheckDate(String strYear, String strMonth,
String strDate)
{
int intYear = 0;
int intMonth = 0;
int intDate = 0;
String strYear_Err="1";
String strMonth_Err="1";
String strDate_Err="1";
String result;
//长度、数字类型及空检查 if (strYear==null || strYear.length () != 4 || strYear.equals("") || IsNum(strYear)==false){
strYear_Err="0";
}else{
intYear = Integer.valueOf (strYear).intValue ();
if(intYear==0) strYear_Err="0";
}
if (strMonth==null || strMonth.length () > 2 || strMonth.equals("") || IsNum(strMonth)==false){
strMonth_Err="0";
}else{
intMonth = Integer.valueOf (strMonth).intValue ();
if (intMonth < 1 || intMonth > 12) strMonth_Err="0";
}
if (strDate==null || strDate.length () > 2 || strDate.equals("") || IsNum(strDate)==false){
strDate_Err="0";
}else{
intDate = Integer.valueOf (strDate).intValue ();
if (intDate < 1 || intDate > 31) strDate_Err="0";
}
if(strMonth_Err.equals("1") && strDate_Err.equals("1")){
switch(intMonth){
case 4:
case 6:
case 9:
case 11:
if (intDate > 30) strDate_Err="0";
break;
case 2:
GregorianCalendar GreDate = new GregorianCalendar();
if (GreDate.isLeapYear (intYear)){
if (intDate > 29) strDate_Err="0";
}else{
if (intDate > 28) strDate_Err="0";
}
break;
}
}
result=strYear_Err+strMonth_Err+strDate_Err;
System.out.println(result);
return result;
}
二、Int类型
/*****************************************************
* 函数名称:CheckDate
* 机能概要: 日期类型CHECK
*
* 参数: intYear CHECK的年份
* intMonth CHECK的月份
* intDate CHECK的日期
* 返回值: "111": 输入的日期正确
* ^^^
* |||________________年:1:正确 0:错误
* ||_________________月:1:正确 0:错误
* |__________________日:1:正确 0:错误
*********************************************************/
public static String CheckDate(int intYear, int intMonth, int intDate){
String strYear, strMonth, strDate;
String strYear_Err="1";
String strMonth_Err="1";
String strDate_Err="1";
strYear = new Integer(intYear).toString();
strMonth = new Integer(intMonth).toString();
strDate = new Integer(intDate).toString();
//长度及空检查
if (strYear.length () != 4) strYear_Err="0";
if (strMonth.length () > 2) strMonth_Err="0";
if (strDate.length () > 2) strDate_Err="0";
if (intYear ==0) strYear_Err="0";
if (intMonth < 1 || intMonth > 12) strMonth_Err="0";
if (intDate < 1 || intDate > 31) strDate_Err="0";
switch(intMonth){
case 4:
case 6:
case 9:
case 11:
if (intDate > 30) strDate_Err="0";
break;
case 2:
GregorianCalendar GreDate = new GregorianCalendar();
if (GreDate.isLeapYear (intYear)){
if (intDate > 29) strDate_Err="0";
}else{
if (intDate > 28) strDate_Err="0";
}
break;
}
return strYear_Err+strMonth_Err+strDate_Err;
}
三、含有分隔符
/*****************************************************
* 函数名称:CheckDate
* 机能概要: 日期类型CHECK
*
* 参数: Strig inStr 待CHECK的字符串
* String inPos 分割符
*
* 返回值: true : 输入的日期正确
* false : 输入的日期错误
*********************************************************/
public static boolean CheckDate(String inStr, String inPos){
String numStr = "1234567890";
String checkedStr = null ;
//长度及空检查
if (inStr.trim().length () == 0) {
return true;
}
if (inStr.trim().length () > 10 || inStr.trim().length () < 8) {
return false;
}
int flg = 0;
for (int i = 0;i
if(inPos.indexOf(inStr.charAt(i),0) == 0){
flg ++;
} else if (numStr.indexOf(inStr.charAt(i),0)==-1) {
return false;
}
}
if (flg != 2) {
return false;
}
int start = inStr.indexOf(inPos,0);
int end = inStr.indexOf(inPos,start + 1);
if (start == 0 || end == inStr.trim().length() - 1 ) {
return false;
}
checkedStr = CheckDate(inStr.substring(0,start),
inStr.substring(start+1,end),
inStr.substring(end+1,inStr.trim().length()));
if (!checkedStr.equals("111")) {
return false;
}
return true;
}
四、分隔符“/”
/*****************************************************
* 函数名称:CheckDate
* 机能概要: 日期类型CHECK
* 分割符="/"
* 参数: Strig inStr 待CHECK的字符串
*
* 返回值: true : 输入的日期正确
* false : 输入的日期错误
*********************************************************/
public static boolean CheckDate(String inStr ){
String inPos = "/" ;
return CheckDate(inStr,inPos);
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12379366/viewspace-83506/
,如需转载,请注明出处,否则将追究法律责任。