当前位置: 代码迷 >> J2SE >> 抛java.text.ParseException: Unparseable date: "2008-12-32"
  详细解决方案

抛java.text.ParseException: Unparseable date: "2008-12-32"

热度:397   发布时间:2016-04-24 12:41:37.0
关于SimpleDateFormate.parse(String para)不能检查日期的合法性问题
如题所述,该方法可将字符串日期转换成为Date对象,但其不检查日期的合法性,
比如输入的日期是32-12-2008按照dd-MM-yyyy格式转换的结果是得到
2009年1月1日

有没有更好的方法类似SimpleDateFormate.parse(String para)
但能将不合法日期抛出异常

------解决方案--------------------
String str = "32-12-2008";
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
format.setLenient(false);//不允许自动调整
System.out.println(format.parse(str));

------解决方案--------------------
Java code
String str = "2008-12-32";        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");        format.setLenient(false);        System.out.println(format.parse(str));
------解决方案--------------------
探讨
Java codeString str="2008-12-32";
SimpleDateFormat format=newSimpleDateFormat("yyyy-MM-dd");
format.setLenient(false);
System.out.println(format.parse(str));
java.text.ParseException: Unparseable date: "2008-12-32"
  相关解决方案