如何在Servlet里,对日期做格式验证?

解决方案 »

  1.   

    /**
         * 检验输入是否为正确的日期格式(不含秒的任何情况),严格要求日期正确性,格式:yyyy-MM-dd HH:mm
         * @param sourceDate
         * @return
         */
        public static boolean checkDate(String sourceDate){
            if(sourceDate==null){
                return false;
            }
            try {
                   SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                   dateFormat.setLenient(false);
                   dateFormat.parse(sourceDate);
                   return true;
            } catch (Exception e) {
            }
             return false;
        }
       
        /**
         * 根据日期获得日期(不含秒的任何情况),严格要求日期正确性,格式:yyyy-MM-dd HH:mm
         * @param sourceDate
         * @return
         */
        public static Date getDate(String sourceDate) throws DataFormatException{
               if(sourceDate==null){
                   throw new DataFormatException("源数据为null");
               }
               try {
                         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                         dateFormat.setLenient(false);
                          return (Date)dateFormat.parse(sourceDate);
               } catch (Exception e) {
                         throw new DataFormatException("源数据格式错误");
               }
        }
    //日期格式可以自定义.
        public static void main(String[] args) throws IOException {
                 String s="2006/01/30 12:26";
                 System.out.println(s+"是否为日期:"+TestCompile.checkDate(s));             s="2006/1/30 12:26";
                 System.out.println(s+"是否为日期:"+TestCompile.checkDate(s));
           
                s="2006/01/32 12:26";
                System.out.println(s+"是否为日期:"+TestCompile.checkDate(s));
        }
      

  2.   

    我的程序是这样:
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter(); Long libraryId = Long.valueOf(request.getParameter("library_id"));
    LibraryService libraryservice = new LibraryService();
    Libraryinfo libraryinfo = libraryservice.getLibraryinfoById(libraryId
    .longValue());
    String title = request.getParameter("title");
    String type = request.getParameter("type");
    String author = request.getParameter("author");
    String local_foreign = request.getParameter("local_foreign");
                    String otherinformation = request.getParameter("otherinfomation");
    String edition = request.getParameter("edition");
    String publisher = request.getParameter("publisher");
    String shelfno = request.getParameter("shelfno");
    String publicationdate = request.getParameter("publicationdate");
                  我想给这个publicationdate值作一个日期格式验证,要有错误提示,怎么写?
                   
      

  3.   

    请参看:http://www.javaren.net.cn/dispbbs.asp?boardid=5&id=69&page=1&star=1里面有java中利用正则表达式进行的各种验证,方法比较全,效率也很高