try
        {
            String str = "20020212";
            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
            Date dat = sf.parse(str);
            System.out.println(dat);
        }
        catch (ParseException ex)
        {
            ex.printStackTrace();
        }

解决方案 »

  1.   

    你在catch里flag = false;不就得了!!!
      

  2.   

    public static boolean isValidDate(String format, String s)
    {
      java.text.SimpleDateFormat sdf = new SimpleDateFormat(format);
      try
      {
        if(s.equals(sdf.format(sdf.parse(s)))
            return true;
        else
            return false;
      }
      catch(ParseException e)
      {
            return false;
      }
    }public static void main(String[] args)
    {
        System.out.println(isValidDate("yyyyMMdd", "19750876")); //false
        System.out.println(isValidDate("yyyyMMdd", "19750816")); //true
        System.out.println(isValidDate("yyyyMMdd", "1975-08-76")); //false
    }
      

  3.   

    SimpleDateFormat只能check格式,但对数字无法check,比如说19802399他就认为是正确的。
    用下面的方法:
        public static boolean isDate(String sDate) {        if(stDate.length() != 8) {
                // 长度不符
                return false;
            }        try {
                int inDate = Integer.parseInt(sDate);
            } catch (Exception e) {
                // 有非数字字符
                return false;
            }
            Calendar calendar = Calendar.getInstance();
            calendar.setLenient( false );
            calendar.set(Integer.parseInt(stDate.substring(0,4)),
                         Integer.parseInt(stDate.substring(4,6)) - 1,
                         Integer.parseInt(stDate.substring(6,8)));
            try {
                Date date = calendar.getTime();
            } catch (IllegalArgumentException e) {
                return false;
            }
            return true;
        }
      

  4.   

    to zhousm6([email protected]):
      请仔细看看我的代码:
        if(s.equals(sdf.format(sdf.parse(s)))
            return true;
        else
            return false;