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); } }