现在需要在java文件中校验日期是否符合yyyy-mm-dd格式并且为有效日期,也就是说不能出现0000-00-00这种的错误形式,请问有没有什么好办法?另外,还要校验时间格式,时间格式为 xx:xx:xx,时间不允许出现24:00:00这种情况。请问有没有对这方面比较牛的,麻烦给解答一下了 :idea:
[code="java"]
static int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
/**
* @param date yyyy-MM-dd HH:mm:ss
* @return
*/
public static boolean isValidDate(String date) {
try {
int year = Integer.parseInt(date.substring(0, 4));
if (year <= 0)
return false;
int month = Integer.parseInt(date.substring(5, 7));
if (month <= 0 || month > 12)
return false;
int day = Integer.parseInt(date.substring(8, 10));
if (day <= 0 || day > DAYS[month])
return false;
if (month == 2 && day == 29 && !isGregorianLeapYear(year)) {
return false;
}
int hour = Integer.parseInt(date.substring(11, 13));
if (hour < 0 || hour > 23)
return false;
int minute = Integer.parseInt(date.substring(14, 16));
if (minute < 0 || minute > 59)
return false;
int second = Integer.parseInt(date.substring(17, 19));
if (second < 0 || second > 59)
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static final boolean isGregorianLeapYear(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
public static void main(String[] args) {
System.out.println(isValidDate("2100-02-29 23:00:01"));
}
[/code]
正则表达式 + 组
如(\d{4})-(\d{2})(\d{2}),提取分组中的数据[即()中] 再判断每个分组数据。
或者先定义不合法的格式进行验证
(00|24)00-00-00
其实你在 入库的时候,可以先 弄一个时间的 js控件,这样就能保障数据的正确性了
[code="java"]import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DateTimeCheck
{
/**
* 验证日期时间格式
* @param args
*/
public static void main(String[] args)
{
String checkValue = "2008-09-11 14:17:11";
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
Date d = null;
if(checkValue != null && !checkValue.equals(""))
{
if(checkValue.split("/").length > 1)
{
dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
}
if (checkValue.split("-").length > 1)
{
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
}else
{
return;
}
try
{
d = dateFormat.parse(checkValue);
System.out.println(d);
}
catch(Exception e)
{
System.out.println("格式错误");
return;
}
String eL= "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-9]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
Pattern p = Pattern.compile(eL);
Matcher m = p.matcher(checkValue);
boolean b = m.matches();
if(b)
{
System.out.println("格式正确");
}
else
{
System.out.println("格式错误");
}
}
}
[/code]
用一个指定的format去parse,有异常就是日期格式不对:
[code="java"]
public boolean isValidDate(String s)
{
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.parse(s);
return true;
}
catch (Exception e)
{
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
return false;
}
}
[/code]
这是老实的办法,SimpleDateFormat的方法经我验证是不行的
[url=http://blog.jdk5.com/zh/java-validate-date-time-string/]Java正则表达式匹配一个时间格式的字符串[/url]
参考这个