java判断是否为日期
㈠ java 校验字符串是不是日期格式
可以通过正则表达式控制,或者用字符串截取,前4位为年份,是否符合,再截取后面两位是否小于12,再截取最后两位是否小于31,以日期之间的“-”截取,不过这样判断大月小月的日期可能会复杂点,个人认为还是正则表达式更简单。
㈡ java判断是否是日期
楼主提出的问题有点片面,我的理解是,你是不是想判断字符串是不是日期格式?如果已经是日期类型,那就不需要判断了,对把。判断给定字符串是不是日期我给你提供两种解决思路,一种是用正则,代码我给你写好了。
publicbooleanisDate(Stringdate){
/**
*判断日期格式和范围
*/
Stringrexp="^((\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]))))))";
Patternpat=Pattern.compile(rexp);
Matchermat=pat.matcher(date);
booleandateType=mat.matches();
returndateType;
}
参数就是你要判断的日期字符串,返回布尔值;
另一种方式就是:玩字符串正则才是王道嘛!希望采纳
publicbooleanisValidDate(Stringstr){
booleanconvertSuccess=true;
//指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
//如果想判断格式为yyyy-MM-dd,需要写成-分隔符的形式
SimpleDateFormatformat=newSimpleDateFormat("yyyy/MM/ddHH:mm");
try{
format.setLenient(false);
format.parse(str);
}catch(ParseExceptione){
//e.printStackTrace();
//如果抛出ParseException或者NullPointerException,就说明格式不对
convertSuccess=false;
}
returnconvertSuccess;
}
推荐使用正则,
㈢ java用正则表达式判断字符串是不是时间
具体代码如下:
1 public static boolean isValidDate(String str) {
2 boolean convertSuccess=true;
3 // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
4 SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
5 try {
6 // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
7 format.setLenient(false);
8 format.parse(str);
9 } catch (ParseException e) {
10 // e.printStackTrace();
11 // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
12 convertSuccess=false;
13 }
14 return convertSuccess;
㈣ java中怎么做到判断输入的日期是否合法
Java为了支持多语言,没有固定的日期格式。你需要根据自己的需要指定日期格式,然后用DateFormat类或者SimpleDateFormat类来判断是否是正确的日期格式。下面的例子供参考。更详细的内容(比如yyyy,MM,dd各代表什么)可以参考javadoc。
public class DateUtil
{
private static final SimpleDateFormat dateFormat = null;
static
{
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
dateFormat = new SimpleDateFormat("yyyy/MM/dd");
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
dateFormat.setLenient(false);
}
public static boolean isValidDate(String s)
{
try
{
dateFormat.parse(s);
return true;
}
catch (Exception e)
{
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
return false;
}
}
// 下面这个方法则可以将一个日期按照你指定的格式输出
public static String formatDate(Date d)
{
return dateFormat.format(d);
}
}
㈤ java语言中判定一个日期是否为指定日期,的判定语句怎么写
其实对于一般的数据格式一致的日期来说 可以用if(date1 == date2 || date1.equals(date2)){}
㈥ Java传入一个字符判断是不是日期格式
public static boolean isValidDate(String str) { boolean convertSuccess=true;// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写; SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm"); try {// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01 format.setLenient(false); format.parse(str); } catch (ParseException e) { // e.printStackTrace();// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对 convertSuccess=false; } return convertSuccess;}
㈦ 在java中给三个数检验它是否为合法日期的程序
import java.util.*;
public class A
{
public static void main(String[] args)
{
int[] a=new int[]{5,2,9};
int min=a[0];
for(int i=0;i<a.length;i++)
{
if(min>a[i])
{
min=a[i];
}
}
System.out.println("最小值是:"+min);
}
}
㈧ java 编程从屏幕输入8-10位字符串,判断是否为日期
publicstaticvoidmain(String[]args)
{
StringcheckValue="2000/03/31";
checkValue=checkValue.replaceAll("/","")+"000000";
Stringyear=checkValue.substring(0,4);//获取年份
Stringmonth=checkValue.substring(4,6);//获取月份
BooleanisLeap=leapYear(Integer.parseInt(year));//判断闰年
System.out.println(isLeap);
StringBuffereL=newStringBuffer();
StringlongMonth="01030507081012";//31天的月份
Stringfix="([2][0-3]|[0-1][0-9]|[1-9])[0-5][0-9]([0-5][0-9]|[6][0])";
if(isLeap&&month.equals("02")){//针对2月份的情况【闰年】
eL.append("\d{4}([1][0-2]|[0][0-9])([2][0-1]|[1-2][0-9]|[0][1-9]|[1-9])"+fix);
}elseif(!isLeap&&month.equals("02")){//针对2月份的情况【非闰年】
eL.append("\d{4}([1][0-2]|[0][0-9])([2][0-1]|[1-2][0-8]|[0][1-9]|[1-9])"+fix);
}elseif(longMonth.contains(month)){//31天月份
eL.append("\d{4}([1][0-2]|[0][0-9])([3][0-1]|[1-2][0-9]|[0][1-9]|[1-9])"+fix);
}else{//30天月份
eL.append("\d{4}([1][0-2]|[0][0-9])([3][0]|[1-2][0-9]|[0][1-9]|[1-9])"+fix);
}
Patternp=Pattern.compile(eL.toString());
Matcherm=p.matcher(checkValue);
booleanflag=m.matches();
if(flag)
{
System.out.println("格式正确");
}
else
{
System.out.println("格式错误");
}
}
publicstaticbooleanleapYear(intyear){
BooleanisLeap=false;
if(((year%100==0)&&(year%400==0))
||((year%100!=0)&&(year%4==0)))
isLeap=true;
returnisLeap;
}
㈨ java判断字符串是否是日期类型
Java为了支持多语言,没有固定的日期格式。你需要根据自己的需要指定日期格式,然后用DateFormat类或者SimpleDateFormat类来判断是否是正确的日期格式。
下面的例子供参考。更详细的内容可以参考javadoc。
public class DateUtil{ private static final SimpleDateFormat dateFormat = null; static { dateFormat = new SimpleDateFormat("yyyy/MM/dd"); dateFormat.setLenient(false); } public static boolean isValidDate(String s) { try { dateFormat.parse(s); return true; } catch (Exception e) { return false; } } public static String formatDate(Date d) { return dateFormat.format(d); } }