/**
     * Convert string to date object
     * @param strD the string date format:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date strToDate(String strD)
    {
        try
        {
            String strFormat = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
            return sdf.parse(strD);
        }catch(java.text.ParseException e)
        {
            throw new mfutil.MFUtilException("Can not convert string to date object:"+ strD);
        }
    }

解决方案 »

  1.   

    /*****************************************************************
    *检查日期格式是否合法
    *****************************************************************/function isDate (theStr) {
        var the1st = theStr.indexOf('-');
        var the2nd = theStr.lastIndexOf('-');
        
        if (the1st == the2nd) { return(false); }
        else {
            var y = theStr.substring(0,the1st);
            var m = theStr.substring(the1st+1,the2nd);
            var d = theStr.substring(the2nd+1,theStr.length);
            var maxDays = 31;
            
            if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {
                return(false); }
            else if (y.length < 4) { return(false); }
            else if (!isBetween (m, 1, 12)) { return(false); }
            else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;
            else if (m==2) {
                if (y % 4 > 0) maxDays = 28;
                else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;
                  else maxDays = 29;
            }
            if (isBetween(d, 1, maxDays) == false) { return(false); }
            else { return(true); }
        }
    }
    /*****************************************************************
    *检查日期格式是否是一个有效的时间格式
    *****************************************************************/
    function isTime (theStr) {
        var colonDex = theStr.indexOf(':');    if ((colonDex<1) || (colonDex>2)) { return(false); }
        else {
            var hh = theStr.substring(0,colonDex);
            var ss = theStr.substring(colonDex+1, theStr.length);        if ((hh.length<1) || (hh.length>2) || (!isInt(hh))) { return(false); }
            else if ((ss.length<1) || (ss.length>2) || (!isInt(ss))) { return(false); }
            else if ((!isBetween(hh,0,23)) || (!isBetween(ss,0,59))) { return(false); }
            else { return(true); }
        }
    }
      

  2.   

    /**
         * Judge the str is yyyy-MM-dd HH:mm:ss format or not
         * Note,if no HH:mm:ss string,it also return false.
         */
        public static boolean isDate(String date)
        {
            Pattern p = Pattern.compile("([1][9][4-9][0-9])|([2][0-9][0-9][0-9])-" +
                                        "(([0]?[0-9])|([1]?[0-2]))-(([0-2]?[0-9])|([3][0-1]))" +
                                        "[ ](([0-1]?[0-9])|([2]?[0-3])):([0-5]?[0-9]):([0-5]?[0-9])$");
            Matcher m = p.matcher(date);
            boolean b = m.matches();
            return b;
        }    /**
         * Check the string format is HH:mm:ss or not
         * Test case
             abc false
             12:1:1 true
             2:1:612 false
             2:65:1 false
             25a:1:1 false
         * @param duration
         * @return
         */
        public static boolean isDuration(String duration)
        {
            Pattern p = Pattern.compile("(([0-1]?[0-9])|([2]?[0-3])):([0-5]?[0-9]):([0-5]?[0-9])$");
            Matcher m = p.matcher(duration);
            boolean b = m.matches();
            return b;
        }
      

  3.   

    谢谢
    例用SimpleDateFormat类要import哪个包才行?