⑴ java 校驗字元串是不是日期格式
可以通過正則表達式控制,或者用字元串截取,前4位為年份,是否符合,再截取後面兩位是否小於12,再截取最後兩位是否小於31,以日期之間的「-」截取,不過這樣判斷大月小月的日期可能會復雜點,個人認為還是正則表達式更簡單。
⑵ java檢查字元串是否是合法的日期
ublic 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用正則表達式判斷字元串是不是時間
具體代碼如下:
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類來判斷是否是正確的日期格式。
下面的例子供參考。更詳細的內容可以參考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); } }
⑸ java怎麼判斷輸入的字元串是否為時間格式
通過正則表達式判斷。代碼如下:
public boolean isDate(String date) {
String path="\\d{4}-\\d{2}-\\d{2}";//定義匹配規則
Pattern p=Pattern.compile(path);//實例化Pattern
Matcher m=p.matcher(date);//驗證字元串內容是否合法
if(m.matches()){
return true;
}
return false;
}
⑹ 如何判斷java 一個String字元串里有沒有日期並且修改成指定格式
可以使用正則表達式進行匹配替換,例如下面代碼:
publicclassApp50{
publicstaticvoidmain(String[]args){
Stringdemo="這個日期是2018/5/6書寫出來的";
//xxxx年xx月xx日格式
Stringdemo2=demo.replaceAll("(\d{4})/(\d{1,2})/(\d{1,2})","$1年$2月$3日");
System.out.println(demo2);
//xxxx-xx-xx格式
demo2=demo.replaceAll("(\d{4})/(\d{1,2})/(\d{1,2})","$1-$2-$3");
System.out.println(demo2);
}
}
運行結果:
這個日期是2018年5月6日書寫出來的
這個日期是2018-5-6書寫出來的
⑺ JAVA如何判斷一個字元串是不是一個合法的日期格式
Java為了支持多語言,沒有固定的日期格式。你需要根據自己的需要指定日期格式,然後用DateFormat類或者SimpleDateFormat類來判斷是否是正確的日期格式。
下面的例子供參考。更詳細的內容可以參考javadoc。
publicclassDateUtil
{
=null;
static
{
dateFormat=newSimpleDateFormat("yyyy/MM/dd");
dateFormat.setLenient(false);
}
(Strings)
{
try
{
dateFormat.parse(s);
returntrue;
}
catch(Exceptione)
{
returnfalse;
}
}
publicstaticStringformatDate(Dated)
{
returndateFormat.format(d);
}
}
⑻ 判斷java 日期字元串
你可以先設置格式,然後根據「2010/12/06
14:30:30」得到Date格式的日期,如果能得到就可以返回一個Date對象,否則返回null
String
text="2010/12/06
14:30:30";
String
date="yyyy/MM/dd
HH:mm:ss";//HH表示為24小時制,hh表示為12小時制
Date
d=new
SimpleFormatDate(date).parse(text);
if(d==null){
...
}else{
...
}
⑼ 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;
}