阅读127 返回首页    go 技术社区[云栖]


SimpleDateFormat严格限制日期转换setLenient(false)

 

String  text  ="1996-2-1";
Date  d  =  null; 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient(false);
try

   d = df.parse(text); 
}
catch(Exception  e)

   d=new  Date();
   System.out.println("你输入的日期不合法,请重新输入"); 

String sdata=df.format(d);
System.out.println(sdata);
   
df.setLeninet(false)这个方法有以下功能:

①这个的功能是不把1996-13-3转换为1997-1-3。

②这样输入1996-2-31这样的数据也会验证出来错误的。
 

原帖地址:https://hi.baidu.com/narshal/blog/item/70612b1f1fff3bd7a78669e0.html

 

 

最后更新:2017-04-02 16:47:43

  上一篇:go Android ActionBar 作为导航条的一个Bug
  下一篇:go 2.1 Objective-C概述