Created
October 23, 2012 13:20
-
-
Save fanker/3938693 to your computer and use it in GitHub Desktop.
通过正则表达式来判断输入的日期格式是否正确
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.java13; | |
//创建一个Date类 | |
public class Date { | |
private String year; | |
private String month;//Date所包含的属性 | |
private String day; | |
public String getYear() { | |
return year; | |
} | |
public void setYear(String year) { | |
this.year = year; | |
} | |
public String getMonth() { | |
return month; | |
} | |
public void setMonth(String month) { | |
this.month = month; | |
} | |
public String getDay() { | |
return day; | |
} | |
public void setDay(String day) { | |
this.day = day; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.java13; | |
//创建一个DateAPP类来对运行程序 | |
public class DateApp { | |
public static void main(String[] args) { | |
DateTest.start(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.java13; | |
import java.util.Scanner; | |
public class DateTest { | |
public static void start() {//开始方法 | |
System.out.println("请选择日期输入格式:"); | |
System.out.println("1:YYYY-MM-DD"); | |
System.out.println("2:MM-DD-YYYY"); | |
System.out.println("3:退出"); | |
Scanner input = new Scanner(System.in); | |
String index = input.next(); | |
if("1".equals(index)){ | |
Method1();//调用方法1 | |
}else if("2".equals(index)) { | |
DateTest2.Method2();//调用DateTest2里面的Method2方法 | |
}else if("3".equals(index)){ | |
System.exit(0);//退出 | |
}else {System.out.println("选择错误,请重新输入:"); | |
start(); | |
} | |
} | |
static boolean flag = true;//初始化变量flag的布尔值为true | |
private static void Method1() { | |
Scanner input = new Scanner(System.in); | |
Date dt1 = new Date(); | |
System.out.print("请输入年份:"); | |
String y1= input.next(); | |
dt1.setYear(y1); | |
System.out.print("请输入月份:"); | |
String m1= input.next(); | |
dt1.setMonth(m1); | |
System.out.print("请输入日期:"); | |
String d1= input.next(); | |
dt1.setDay(d1); | |
int a = Integer.parseInt(y1); | |
if(dt1.getYear().matches("[1-9]\\d\\d\\d")){ | |
if((a%4 == 0 && a%100 != 0)||a%400 == 0){//判断输入的年份是否是闰年 | |
leapyear(dt1); //是闰年调用leapyear()方法 | |
}else { | |
normalyear(dt1);//不是闰年的话调用normalyear()方法 | |
} | |
} | |
} | |
private static void output(Date dt1) {//输出日期方法 | |
if(flag){ | |
System.out.println("日期格式输入正确"); | |
System.out.println("你输入的日期为:" + dt1.getYear() + "年" + dt1.getMonth() + "月" + dt1.getDay() + "日"); | |
start(); | |
}else { | |
System.out.println("日期格式输入不正确!"); | |
start(); | |
} | |
} | |
private static void normalyear(Date dt1) { | |
if(dt1.getMonth().matches("1") || dt1.getMonth().matches("3")|| dt1.getMonth().matches("5")|| dt1.getMonth().matches("7")|| dt1.getMonth().matches("8")|| dt1.getMonth().matches("10")|| dt1.getMonth().matches("12")){ | |
//利用正则表达式来对月份进行判断 | |
if(dt1.getDay().matches("(0?[1-9]|[1-2]\\d|[3][0-1])")){//利用正则表达式来对输入的当月的天数格式进行判断 | |
flag = true; | |
output(dt1); | |
}else { | |
System.out.println("日期格式输入错误"); | |
start(); | |
} | |
}else if(dt1.getMonth().matches("2")){//判断是否为2月 | |
if(dt1.getDay().matches("(0?[1-9]|[1-2][0-8])")){//判断输入的2月的天数是否在28天以内 | |
flag = true; | |
output(dt1); | |
}else { | |
System.out.println("日期格式输入错误"); | |
start(); | |
} | |
}else { | |
if(dt1.getDay().matches("(0?[1-9]|[1-2]\\d|[30])")){ | |
flag = true; | |
output(dt1); | |
}else { | |
System.out.println("日期格式输入错误"); | |
start(); | |
} | |
} | |
} | |
private static void leapyear(Date dt1) { | |
if(dt1.getMonth().matches("2")){ | |
if(dt1.getDay().matches("[1-2][0-9]")){ | |
flag = true; | |
output(dt1); | |
}else { | |
System.out.println("该年是闰年,你的日期格式输入错误"); | |
start(); | |
} | |
}else { | |
normalyear(dt1); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.java13; | |
import java.util.Scanner; | |
//创建DateTest2类 | |
//这个类里面和DtaeTest里面的方法差不多就是一样的,我感觉代码写重复了 | |
public class DateTest2 { | |
static boolean flag = true; | |
public static void Method2() { | |
Scanner input = new Scanner(System.in); | |
Date dt2 = new Date(); | |
System.out.print("请输入月份:"); | |
String m2= input.next(); | |
dt2.setMonth(m2); | |
System.out.print("请输入日期:"); | |
String d2= input.next(); | |
dt2.setDay(d2); | |
System.out.print("请输入年份:"); | |
String y2= input.next(); | |
dt2.setYear(y2); | |
int a = Integer.parseInt(y2); | |
if(dt2.getYear().matches("[1-9]\\d\\d\\d")){ | |
if((a%4 == 0 && a%100 != 0)||a%400 == 0){ | |
leapyear(dt2); | |
}else { | |
normalyear(dt2); | |
} | |
} | |
} | |
private static void output(Date dt2) { | |
if(flag){ | |
System.out.println("日期格式输入正确"); | |
System.out.println("你输入的日期为:" + dt2.getYear() + "年" + dt2.getMonth() + "月" + dt2.getDay() + "日"); | |
DateTest.start(); | |
}else { | |
System.out.println("日期格式输入不正确!"); | |
DateTest.start(); | |
} | |
} | |
private static void normalyear(Date dt2) { | |
if(dt2.getMonth().matches("1") || dt2.getMonth().matches("3")|| dt2.getMonth().matches("5")|| dt2.getMonth().matches("7")|| dt2.getMonth().matches("8")|| dt2.getMonth().matches("10")|| dt2.getMonth().matches("12")){ | |
if(dt2.getDay().matches("(0?[1-9]|[1-2]\\d|[3][0-1])")){ | |
flag = true; | |
output(dt2); | |
}else { | |
System.out.println("日期格式输入错误"); | |
DateTest.start(); | |
} | |
}else if(dt2.getMonth().matches("2")){ | |
if(dt2.getDay().matches("(0?[1-9]|[1-2][0-8])")){ | |
flag = true; | |
output(dt2); | |
}else { | |
System.out.println("日期格式输入错误"); | |
DateTest.start(); | |
} | |
}else { | |
if(dt2.getDay().matches("(0?[1-9]|[1-2]\\d|[30])")){ | |
flag = true; | |
output(dt2); | |
}else { | |
System.out.println("日期格式输入错误"); | |
DateTest.start(); | |
} | |
} | |
} | |
private static void leapyear(Date dt2) { | |
if(dt2.getMonth().matches("2")){ | |
if(dt2.getDay().matches("[1-2][0-9]")){ | |
flag = true; | |
output(dt2); | |
}else { | |
System.out.println("该年是闰年,你的日期格式输入错误"); | |
DateTest.start(); | |
} | |
}else { | |
normalyear(dt2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment