java 判断是否为合法的日期时间字符串?
判断是否为合法的日期时间字符串
[解决办法]
public class JudgeDate {
/**
* 判断是否为合法的日期时间字符串
* @param str_input
* @return boolean;符合为true,不符合为false
*/
public static boolean isDate(String str_input,String rDateFormat){
if (!isNull(str_input)) {
SimpleDateFormat formatter = new SimpleDateFormat(rDateFormat);
formatter.setLenient(false);
try {
formatter.format(formatter.parse(str_input));
} catch (Exception e) {
return false;
}
return true;
}
return false;
}
public static boolean isNull(String str){
if(str==null)
return true;
else
return false;
}
public static void main(String args[]){
String str= "2007-5-12 ";
if(isDate(str, "yyyy.MM.dd ")|isDate(str, "yyyy-MM-dd "))
System.out.print( "This is true ");
else
System.out.println(str);
}
}
按照JDK-HELP.chm,rDateFormat的值可以参照以下格式赋值:
日期和时间模式 结果
"yyyy.MM.dd G 'at ' HH:mm:ss z " 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ' 'yy " Wed, Jul 4, '01
"h:mm a " 12:08 PM
"hh 'o ' 'clock ' a, zzzz " 12 o 'clock PM, Pacific Daylight Time
"K:mm a, z " 0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa " 02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z " Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ " 010704120856-0700
"yyyy-MM-dd 'T 'HH:mm:ss.SSSZ " 2001-07-04T12:08:56.235-0700