日期:2014-05-20 浏览次数:20950 次
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class TTest {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
SimpleDateFormat Format = new SimpleDateFormat("MM/dd");
String birthday;
String s;
do{
System.out.print("请输入会员的生日<月/日:00/00>:");
birthday = input.next();//接收会员生日
s = Format.format(birthday);
}while(!(birthday.equals(s)));
System.out.println("会员生日是:"+birthday);
}
}
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
String date = null;
while (true) {
System.out.print("输入生日[格式: 月/日]:");
date = scanner.next();
// 日期格式校验
if (!date.matches("\\d{1,2}/\\d{1,2}")) {
System.out.println("输入格式不正确!");
continue;
}
String[] s = date.split("/");
int month = Integer.parseInt(s[0]);
int day = Integer.parseInt(s[1]);
if (month > 12
------解决方案--------------------
month < 1) {
System.out.println("错误的月份!");
continue;
}
if (day > 31
------解决方案--------------------
day < 1) {
System.out.println("错误的日期!");
continue;
}
if (month == 2 && day > 29) {
System.out.println("错误的日期!");
continue;
}
if ((month == 4
------解决方案--------------------
month == 5
------解决方案--------------------
month == 9
------解决方案--------------------
month == 11) && day > 30) {
System.out.println("错误的日期!");
continue;
}
date = (month < 10 ? "0" + month : month) + "/" + (day < 10 ? "0" + day : day);
break;
}
System.out.println(date);
}
private static List<String> DAY_31=Arrays.asList(new String[]{"1","3","5","7","8","10","12"});还是很麻烦
private static List<String> DAY_30=Arrays.asList(new String[]{"2","4","6","9","11"});
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
SimpleDateFormat dateFormat=new SimpleDateFormat("MM/dd");
while(true){
System.out.println("请输入:");
String str=sc.nextLine();
try {
dateFormat.parse(str);
String mouth=str.split("/")[0];
int day=Integer.parseInt(str.split("/")[1]);
if(DAY_31.contains(mouth)&&(day>31
------解决方案--------------------
day<1)){
System.out.println("格式错误,请重新输入");
continue;
}
else if(DAY_30.contains(mouth)&&(day>30
------解决方案--------------------
day<1)){
System.out.println("格式错误,请重新输入");
continue;
}
else if(mouth.equals("2")&&(day>29
------解决方案--------------------
day<1)){
System.out.println("格式错误,请重新输入");
continue;
}else{
System.out.println("验证成功:"+str);
break;
}
} catch (Exception e) {
System.out.println("格式错误,请重新输入");
continue;
}
}
}