当str = 2011/12 / 12aaaaaaaaa时,SimpleDateFormat parse(string str)不会抛出exception?

这是一个例子:

public MyDate() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d"); sdf.setLenient(false); String t1 = "2011/12/12aaa"; System.out.println(sdf.parse(t1)); } 

2011/12 / 12aaa不是有效的日期字符串。 但是,该function打印“2011年12月12日00:00:00 PST”,并且不会抛出ParseException。

谁能告诉我如何让SimpleDateFormat将“2011/12 / 12aaa”视为无效的日期字符串并抛出exception?

parse(...)上的JavaDoc声明如下:

解析不一定使用直到字符串末尾的所有字符

看起来你不能让SimpleDateFormat抛出exception,但你可以做到以下几点:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d"); sdf.setLenient(false); ParsePosition p = new ParsePosition( 0 ); String t1 = "2011/12/12aaa"; System.out.println(sdf.parse(t1,p)); if(p.getIndex() < t1.length()) { throw new ParseException( t1, p.getIndex() ); } 

基本上,您检查解析是否消耗了整个字符串,如果没有,则输入无效。

查看日期是否有效如果日期有效则返回以下方法,否则返回false。

 public boolean isValidDate(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d"); Date testDate = null; try { testDate = sdf.parse(date); } catch (ParseException e) { return false; } if (!sdf.format(testDate).equals(date)) { return false; } return true; } 

看看下面的类,它可以检查日期是否有效

**示例**

 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateValidCheck { public static void main(String[] args) { if(new DateValidCheck().isValidDate("2011/12/12aaa")){ System.out.println("...date is valid"); }else{ System.out.println("...date is invalid..."); } } public boolean isValidDate(String date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d"); Date testDate = null; try { testDate = sdf.parse(date); } catch (ParseException e) { return false; } if (!sdf.format(testDate).equals(date)) { return false; } return true; } } 

在成功解析整个模式字符串之后, SimpleDateFormat停止评估它被解析的数据。

可以使用Java 8 LocalDate:

 public static boolean isDate(String date) { try { LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd")); return true; } catch (DateTimeParseException e) { return false; } } 

如果输入参数为"2011/12/12aaaaaaaaa" ,则输出为false ;

如果输入参数为"2011/12/12" ,则输出为true

查看方法文档,其中说明: ParseException if the beginning of the specified string cannot be parsed

使用javadoc的方法源代码:

 /** * Parses text from the beginning of the given string to produce a date. * The method may not use the entire text of the given string. * 

* See the {@link #parse(String, ParsePosition)} method for more information * on date parsing. * * @param source A String whose beginning should be parsed. * @return A Date parsed from the string. * @exception ParseException if the beginning of the specified string * cannot be parsed. */ public Date parse(String source) throws ParseException { ParsePosition pos = new ParsePosition(0); Date result = parse(source, pos); if (pos.index == 0) throw new ParseException("Unparseable date: \"" + source + "\"" , pos.errorIndex); return result; }

您可以使用ParsePosition类或sdf.setLenient(false)函数

文档: http : //docs.oracle.com/javase/7/docs/api/java/text/ParsePosition.html http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat html的#setLenient(布尔值)