Description
日期格式校验问题
Input
若干行数据,每行代表一个日期,格式为yyyy-mm-dd,即4位的年份、2位的月份和2位的日数据。
Output
如果是合法日期数据则输出legal date format!否则输出illegal date format!
Sample Input
2012-09-08
2000-12-32
1997-07-01
1a23-09-01
Sample Output
legal date format!
illegal date format!
legal date format!
illegal date format!
Hint
不必考虑平年闰年问题,但是有每月天数最多31的限制。
//date check
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
while(input.hasNext())
{
int vYear,vMonth,vDay;
int mistake=0;
String str=input.next();
for(int i=0;i<4;i++)
{
if(str.charAt(i)<48||str.charAt(i)>57)mistake+=1;
}
for(int i=5;i<7;i++)
{
if(str.charAt(i)<48||str.charAt(i)>57)mistake+=1;
}
for(int i=8;i<10;i++)
{
if(str.charAt(i)<48||str.charAt(i)>57)mistake+=1;
}
String month = str.substring(5,7);
vMonth = Integer.parseInt(month);
if(mistake==0) {
if(vMonth<0||vMonth>12)mistake+=1;
}
if(mistake==0) {
String day = str.substring(8,10);
vDay = Integer.parseInt(day);
switch (vMonth) {
case 1:if(vDay<0||vDay>31)mistake+=1;break;
case 2: {
String year = str.substring(0,4);
vYear = Integer.parseInt(year);
if(vYear % 4 == 0 && vYear % 100 != 0 || vYear % 400 == 0) {
if(vDay<0||vDay>29)mistake+=1;
}
else {
if(vDay<0||vDay>28)mistake+=1;
}
};break;
case 3:if(vDay<0||vDay>31)mistake+=1;break;
case 4:if(vDay<0||vDay>30)mistake+=1;break;
case 5:if(vDay<0||vDay>31)mistake+=1;break;
case 6:if(vDay<0||vDay>30)mistake+=1;break;
case 7:if(vDay<0||vDay>31)mistake+=1;break;
case 8:if(vDay<0||vDay>31)mistake+=1;break;
case 9:if(vDay<0||vDay>30)mistake+=1;break;
case 10:if(vDay<0||vDay>31)mistake+=1;break;
case 11:if(vDay<0||vDay>30)mistake+=1;break;
case 12:if(vDay<0||vDay>31)mistake+=1;break;
}
}
if(mistake==0)System.out.println("legal date format!");
else System.out.println("illegal date format!");
}
}
}