導航:首頁 > 編程語言 > java字元串是否是日期

java字元串是否是日期

發布時間:2022-04-26 08:27:01

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

閱讀全文

與java字元串是否是日期相關的資料

熱點內容
銀河麒麟字體庫存在哪個文件夾 瀏覽:956
魔獸加丁伺服器的航空叫什麼 瀏覽:152
花冠改裝案例哪個app多 瀏覽:515
成績單app哪個好用 瀏覽:140
北美程序員vs國內程序員 瀏覽:181
php解析xml文檔 瀏覽:121
石墨文檔APP怎麼橫屏 瀏覽:185
牆主鋼筋加密和非加密怎麼看 瀏覽:144
金山區文件夾封套定製 瀏覽:708
soho程序員 瀏覽:672
java位元組截取 瀏覽:525
php提交作業 瀏覽:815
房產還沒解壓可以辦理贈予嗎 瀏覽:224
java毫秒轉分鍾 瀏覽:753
模式識別中文pdf 瀏覽:774
c語言平均數字編譯錯誤 瀏覽:171
單片機算交流 瀏覽:45
php自適應網站 瀏覽:467
2b2t伺服器怎麼獲得許可權 瀏覽:816
c語言javaphp 瀏覽:804