「java日期验证」java验证日期合法性的方法
今天给各位分享java日期验证的知识,其中也会对java验证日期合法性的方法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java 前后台日期验证
前台用正则表达式去验证。
后台
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("格式错误");
}
java中如何判断输入的日期是否合法?
java.text.DateFormat dateFormat= new java.text.SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale.ENGLISH);
dateFormat.setLenient(false);
java.util.Date timeDate = dateFormat.parse(dateString);
//转换为util类型
看到dateFormat.setLenient(false);没有,设定其为false就是强制判断是否非法日期,不让系统自动转换,否则2月31号系统会自动转换为3月2号或者3号。
Java中对Date型的变量进行日期验证
在页面时间这块都是选着输入。
在注册账号的时候有出生日期这项都是时间插件。
那你非要手动输入的话也有解决方法:
将用户输的日期设为字符串,通过正则表达式匹配。
日期格式正确后以字符串形式传到后台。
后台接收到,在转换成想要的时间格式。
//字符串转时间代码如下
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String dateString = "2011-07-29 14:50:11";
Date date = simpleDateFormat.parse(dateString);
java判断是否是日期
楼主提出的问题有点片面,我的理解是,你是不是想判断字符串是不是日期格式?如果已经是日期类型,那就不需要判断了,对把。判断给定字符串是不是日期我给你提供两种解决思路,一种是用正则,代码我给你写好了。
public boolean isDate(String date) {
/**
* 判断日期格式和范围
*/
String rexp = "^((\\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]))))))";
Pattern pat = Pattern.compile(rexp);
Matcher mat = pat.matcher(date);
boolean dateType = mat.matches();
return dateType;
}
参数就是你要判断的日期字符串,返回布尔值;
另一种方式就是:玩字符串正则才是王道嘛!希望采纳
public boolean isValidDate(String str) {
boolean convertSuccess = true;
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
//如果想判断格式为yyyy-MM-dd,需要写成-分隔符的形式
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
try {
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
// e.printStackTrace();
// 如果抛出ParseException或者NullPointerException,就说明格式不对
convertSuccess = false;
}
return convertSuccess;
}
推荐使用正则,
java中怎么判断输入的日期是否合法?
import java.util.*;
import java.util.regex.*;
import java.text.*;
/** 这个是按照楼主的描述使用通过判断字符验证时间合法性 */
public class DateUtils2 {
//测试代码 begin
public static void main(String[] s){
//以下是测试代码
test("20099-1-1");
test("20099-100-1");
test("20099-1-100");
test("2009-1-1");
test("2009-1-31");
test("2009-2-28");
test("2009-2-29");
test("2008-2-29");
}
private static void test(String stringdate){
System.out.println("输入[" + stringdate + "]是否合法:" + validate(stringdate));
}
//测试代码 end
//==
/** 判断主方法 */
public static boolean validate(String dateString){
//使用正则表达式 测试 字符 符合 dddd-dd-dd 的格式(d表示数字)
Pattern p = Pattern.compile("\\d{4}+[-]\\d{1,2}+[-]\\d{1,2}+");
Matcher m = p.matcher(dateString);
if(!m.matches()){ return false;}
//得到年月日
String[] array = dateString.split("-");
int year = Integer.valueOf(array[0]);
int month = Integer.valueOf(array[1]);
int day = Integer.valueOf(array[2]);
if(month1 || month12){ return false;}
int[] monthLengths = new int[]{0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(isLeapYear(year)){
monthLengths[2] = 29;
}else{
monthLengths[2] = 28;
}
int monthLength = monthLengths[month];
if(day1 || daymonthLength){
return false;
}
return true;
}
/** 是否是闰年 */
private static boolean isLeapYear(int year){
return ((year % 4 == 0 year % 100 != 0) || year % 400 == 0) ;
}
}
关于java日期验证和java验证日期合法性的方法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。