import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Float.isNaN;
/** 验证 */
public class Checks {
public static void main(String[] args) {
String date="200013";
System.out.println("是否是有效日期:"+ isMoney("-0.1"));
}
// 验证是否是有效日期 dateType y:年, m:代表年月,d代表年月日
public static boolean ifDate(String date,String dateType){
int intYear;
int intMonth;
int intDay;
// 日期年验证
if(dateType.equals(ECheckDateDict.CHECK_DATE_YEAR.code)){
intYear=Integer.valueOf(date);
if (intYear > 2100 || intYear < 1900 ) {
return false;
}
}else if(dateType.equals(ECheckDateDict.CHECK_DATE_MONTH.code)){
StringBuffer stringBuilder1=new StringBuffer(date);
date= stringBuilder1.insert(4,"-").toString();
String arr[] = date.split("-");
intYear = Integer.valueOf(arr[0], 10);
intMonth = Integer.valueOf(arr[1], 10);
if (isNaN(intYear) || isNaN(intMonth)) {
return false;
}
if (intYear > 2100 || intYear < 1900 || intMonth > 12 || intMonth < 0 ) {
return false;
}
if (intMonth<1 || intMonth>12) {
return false;
}
}else if(dateType.equals(ECheckDateDict.CHECK_DATE_DAY.code)){
StringBuffer stringBuilder1=new StringBuffer(date);
stringBuilder1.insert(6,"-");
date= stringBuilder1.insert(4,"-").toString();
String arr[] = date.split("-");
if (arr.length == 3) {
intYear = Integer.valueOf(arr[0], 10);
intMonth = Integer.valueOf(arr[1], 10);
intDay = Integer.valueOf(arr[2], 10);
if (isNaN(intYear) || isNaN(intMonth) || isNaN(intDay)) {
return false;
}
if (intYear > 2100 || intYear < 1900 || intMonth > 12 || intMonth < 0 || intDay > 31 || intDay < 0) {
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && intDay > 30) {
return false;
}
if ((intYear % 100>0 && intYear % 400>0) || (intYear % 100>0 && intYear % 4 >0)) {
if (intDay > 29) return false;
} else {
if (intDay > 28){ return false;
}
}
}
}else{
return false;
}
return true;
}
// 验证是否是金额(小数点两位,不可为负数)
public static boolean isMoney(String str) {
// 判断小数点后2位的数字的正则表达式
Pattern pattern = Pattern.compile("(^[1-9]([0-9]+)?(\\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\\.[0-9]([0-9])?$)");
Matcher match = pattern.matcher(str);
return match.matches();
}
// 验证是否是正整数或0
public static boolean isNum(String str){
String regex="^+?(0|[1-9]+[0-9]*)$";
// 不包含0 Pattern pattern = Pattern.compile("(^\\+?[1-9][0-9]*$)");
Pattern pattern = Pattern.compile(regex);
Matcher match = pattern.matcher(str);
return match.matches();
}
// 验证是否是正整数或0
public static boolean isNum2(String str){
String regex="(0|[1-9][0-9]*|-[1-9][0-9]*)";
Pattern pattern = Pattern.compile(regex);
Matcher match = pattern.matcher(str);
return match.matches();
}
public static boolean isDate(String time,String dateType){
SimpleDateFormat sdf=new SimpleDateFormat();
if(dateType.equals(ECheckDateDict.CHECK_DATE_YEAR.code)){
sdf = new SimpleDateFormat("yyyy");
} else if(dateType.equals(ECheckDateDict.CHECK_DATE_MONTH.code)){
sdf = new SimpleDateFormat("yyyyMM");
} else if(dateType.equals(ECheckDateDict.CHECK_DATE_DAY.code)){
sdf = new SimpleDateFormat("yyyyMMdd");
}
String date=sdf.format(new Date());
/* sdf.parse(time,"yyyyMM");*/
// 判断日期是否为空或超过当前年
Integer i=time.compareTo(date);
if(time==null || time.isEmpty() || Checks.ifDate(time, dateType)==false || i>0){
return false;
}
return true;
}
/**
* 判断时间是否在时间段内
*
* @param nowTime
* @param beginTime
* @param endTime
* @return
*/
public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(beginTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else if (nowTime.compareTo(beginTime) == 0 || nowTime.compareTo(endTime) == 0) {
return true;
} else {
return false;
}
}
/** 判断日期是否大于当前年+1 */
public static int compareBeforeDate(String time){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date nowdate = new Date();
Calendar cal = Calendar.getInstance();
// 设置起时间
cal.setTime(nowdate);
// 增加一年
cal.add(Calendar.YEAR, 1);
// 判断当前日期是否超过系统时间
String date=sdf.format(cal.getTime());
Integer i=time.compareTo(date);
// 年月日不可填写大于当前系统日期值
return i;
}
/** 判断日期是否小于1999年 */
public static int compareAfterDate(String time) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date nowdate = sdf.parse("199901");
Calendar cal = Calendar.getInstance();
// 设置起时间
cal.setTime(nowdate);
// 判断当前日期是否超过系统时间
String date=sdf.format(cal.getTime());
Integer i=time.compareTo(date);
// 年月日不可填写大于当前系统日期值
return i;
}
}
常用正则验证表达式、即校验
最新推荐文章于 2024-07-16 23:28:44 发布