在JAVA中如何檢查字串日期的合法性

2021-05-24 02:14:53 字數 585 閱讀 7425

文/陳剛 from 

www.chengang.com.cn 2005-11-04 

在swt中還沒有日期控制項,所以我的日期是用文字框手工輸入的,這時就需要驗證輸入的日期是否合法。在之前我是複製很久以前寫的一段**,它的解決方法是分解日期字串,然後檢查,這很笨且不嚴格。正確的方案應該是利用dateformat類來檢查,如下所示:

public final class stringutil

public static date parsestringtodate(string datestr) else

} catch (exception e)

return date; }

}這個方法的test case也一併給出如下:

public class teststringutil extends testcase

}public void testparsestringtodateexception()

private void testparsestringtodateexception(string datestr) catch (exception e) }

}

檢查字串是否相等

在j a中可以使用 equals 方法判斷兩個字串是否相等。s.equals t 如果字串s與字串t相等,則返回true 否則,返回false。想要檢測兩個字串是否相等,而不區分大小寫,可以使用equalsignorecase方法 hello equalsignorecase hello 一定不要使...

jquery如何檢查字串中是否包含指定字元?

方法 使用indexof 或lastindexof 來檢查,它們都可返回指定字元在字串中的位置,如果字串不包含指定字元,則返回 1 因此只需判斷返回值是否大於等於即可,例 if 字串.indexof 字元 0 相關推薦 jquery 教程 方法一 使用indexof 和lastindexof 方法 ...

檢查字串中的結束標記

使用的模組為itertools,在其中主要使用的方法是itertools.imap function,iter1,iter2.itern 此方法的主要就是返回乙個迭代器,其中返回的項為function iter1,iter2,itern 返回的長度主要看最短的iter的長度,如果function為n...