function chkdatestr(checkStr) {
  var tmpy = "";
  var tmpm = "";
  var tmpd = "";
  var checkCode = 0;
  for (i=0; i<checkStr.length ;i++) {
    ch = checkStr.charAt(i);
    if (ch == '-') checkCode++;
    if (checkCode > 2) return(false);
    else if (checkCode == 0 && ch != '-') tmpy += ch;
    else if (checkCode == 1 && ch != '-') tmpm += ch;
    else if (checkCode == 2 && ch != '-') tmpd += ch;
  }
  if (chknumber(tmpy) && tmpy.length == 2) {
    if (tmpy > 70) tmpy = "19" + tmpy;
    else tmpy = "20" + tmpy;
  }
  return(chkdate(tmpy, tmpm, tmpd));
}

解决方案 »

  1.   

    boolean checkDate(String date) {

    Pattern p = Pattern.compile("\\d{4}/\\d{2}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}");
    Matcher m = p.matcher(date);

    if(m.matches()) { // 符合 xxxx\xx\xx xx:xx:xx (x代表数字) String[] dateSplit = date.split("\\s");

    int yyyy = Integer.parseInt(dateSplit[0].split("/")[0]);
    int MM = Integer.parseInt(dateSplit[0].split("/")[1]);
    int dd = Integer.parseInt(dateSplit[0].split("/")[2]);

    int HH = Integer.parseInt(dateSplit[1].split(":")[0]);
    int mm = Integer.parseInt(dateSplit[1].split(":")[1]);
    int ss = Integer.parseInt(dateSplit[1].split(":")[2]);

    if(yyyy<1990 || yyyy>2100) {

    return false;
    }
    else if(MM<1 || MM>12) {

    return false;
    }
    else if(dd<1 || dd>31) {

    return false;
    }
    else if(HH<0 || HH>23) {

    return false;
    }
    else if(mm<0 || mm>59) {

    return false;
    }
    else if(ss<0 || ss>59) {

    return false;
    }
    else {

    return true;
    }
    }
    else {

    return false;
    }
    }如果不考虑月份>12,小时>24之类的问题的话,用到前两行的正则表达式就行了
    Pattern p = Pattern.compile("\\d{4}/\\d{2}/\\d{2}\\s\\d{2}:\\d{2}:\\d{2}");
    Matcher m = p.matcher(date);这样做还差一点,就是比如2月31日用上面那个方法就判断不出是错的
    自己慢慢去补充吧 ^-^
      

  2.   

    public class Test { public static void main(String[] args) {
    String strDate = "2005/01/06 09:00:00";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");


    try {
    Date date = sdf.parse(strDate);
    System.out.println("解析的日期为:" + date);
    } catch (ParseException e) {
    System.out.println("不正确的日期格式");

    }

    }
    }
      

  3.   

    顶,同意楼上。
    java自己带日期格式化。
      

  4.   

    我也同意用日期格式化。用try
      

  5.   

    同意
         根根和icy_csdn