SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
try {
sdf.parse("199a");
} catch (ParseException e) {
System.out.println("年份格式不对");
}

解决方案 »

  1.   

    把字符串截成“年份”,“月份”,然后转换为int形,然后在判断是否争取
      

  2.   

    上面是年,这是月份,10月以下的自己先补0再检查。SimpleDateFormat sdf = new SimpleDateFormat("MM");
    try {
    sdf.parse("11");
    } catch (ParseException e) {
    System.out.println("年份格式不对");
    }
      

  3.   

    用SimpleDateFormat判断是最简单得;
      

  4.   

    用SimpleDateFormat不能检查月份,试试new SimpleDateFormat("MM").parse("14")就知道了。转成int再进行判断。try
    {
        int year = Integer.parseInt("123a");
    }
    catch(NumberFormatException e)
    {
       System.out.println("年份格式不对");
    }try
    {
        int month = Integer.parseInt("13");
        if(month < 1 || month > 12)
            System.out.println("月份格式不对");
    }
    catch(NumberFormatException e)
    {
       System.out.println("月份格式不对");
    }
      

  5.   

    //strDate 格式为:"0000-00-00"
    public boolean isAllowDate(String strDate) {
    if (strDate == null || strDate.trim().length() == 0)
    return true;
    if (strDate.trim().length() != 10)
    return false;
    for (int i = 0; i < 10; i++) {
    char c = strDate.trim().charAt(i);
    if (i == 4 || i == 7) {
    if (c != '-')
    return false;
    } else
    if (c < '0' || c > '9')
    return false;
    }
    int year = Integer.parseInt(strDate.trim().substring(0, 4));
    int month = Integer.parseInt(strDate.trim().substring(5, 7));
    if (month < 1 || month > 12)
    return false;
    int day = Integer.parseInt(strDate.trim().substring(8, 10));
    int MONTH_LENGTH[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int LEAP_MONTH_LENGTH[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int daymax = isLeapYear(year) ? LEAP_MONTH_LENGTH[month - 1] : MONTH_LENGTH[month - 1];
    if (day < 1 || day > daymax)
    return false;
    return true;
    }
    public  boolean isLeapYear(int year) {
    if ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0))
    return true;
    else
    return false;
    }
      

  6.   

    同志们,这样的判断还是放在 用户端判断把……<script language="JavaScript">
    // *****************************************************************************
    // 得到 Form 中的 Element;
    // ** 由于输入框以及其他Input的名字在 JavaScript 语法有冲突,所以只能通过这种方法来获得其 Handle
    // ** 相当于: document.all.[sysConfig.message]function CheckYearValue (theYearValue)
    {
    var theChar;
    var Result = "";
    for (var i=0; i<theYearValue.length; i++)
    {
    theChar = theYearValue.charAt(i);
    if (theChar <'0' || theChar >'9')
    {
    Result = Result + "\r\n" + theChar;
    }
    }
    // 俺只判断了输入是否是数字…… 其他的判断可以自己加了……
    return Result;
    }function CheckInput (IsNotifyUserWhenDataIsCorrect)
    {
    // for User Interface
    var Result = ""; //
    var ErrorCount = 0; // 检测输入的日期格式是否正确……
    TempResult = CheckYearValue (document.all.beginYear.value);
    if (TempResult.length > 0)
    {
    ErrorCount ++;
    Result = Result + "\r\n" + ErrorCount + ":开始年份有错误的字符:" + TempResult;
    }
    TempResult = CheckYearValue (document.all.endYear.value);
    if (TempResult.length > 0)
    {
    ErrorCount ++;
    Result = Result + "\r\n" + ErrorCount + ":结束年份有错误的字符:" + TempResult;
    } // for User Interface...
    if (Result.length>0)
    {
    alert ("您的输入有 " + ErrorCount + " 个错误:\r\n\r\n" + Result);
    return false;
    }
    else
    {
    if (IsNotifyUserWhenDataIsCorrect)
    alert ("输入正确。");
    return true;
    }
    }
    </script>