Home>

I am creating an "employee management system" and it is designed to input "birth date" in the format of "YYYY/MM/DD" on the "registration" screen below. If i use this format, the following image error will occur. However, if you enter it in the format of "YYYY-MM-DD", it will be implemented correctly. Where should I change it?

regist_input.jsp
<% @ page language = "java" contentType = "text/html;charset = UTF-8"
    pageEncoding = "UTF-8"%>
<% @ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core"%>
<! DOCTYPE html>
<html>
<head>
<meta http-equiv = "Content-Type" content = "text/html;charset = UTF-8">
<link rel = "stylesheet" href = "<% = request.getContextPath ()%>/css/layout.css" />
<link rel = "stylesheet" href = "<% = request.getContextPath ()%>/css/style.css" />
<title>Employee management system</title>
</head>
<body>
    <header>
        
             Employee management system 
            
                Welcome,  /html/update/update_input.jsp">Jiro Tanaka  |  Logout 
            
        
    </header>
    
        
             Employee name search 
            
                <form action = "<% = request.getContextPath ()%>/SelectName" method = "post">
                    <input type = "text" name = "empName" />
                    <input type = "submit" value = "search" />
                </form>
            
             Department search 
            
                <form action = "<% = request.getContextPath ()%>/SelectDeptId" method = "post">
                    <select name = "deptId">
                        <option value = "1" selected = "selected">Sales Department</option>
                        <option value = "2">Accounting Department</option>
                        <option value = "3">General Affairs Department</option>
                    </select>
                    <input type = "submit" value = "search" />
                </form>
            
        </aside>
        
             Employee registration confirmation screen 
                
                     Password: 
                     * Hide 
                
                
                     Employee name: 
                     ${emp.empName} 
                
                
                     Gender: 
                     ${emp.gender} 
                
                
                     Address: 
                     ${emp.address} 
                
                
                     Birth date: 
                     ${emp.birthday} 
                
                
                     Permission: 
                     ${emp.authority} 
                
                
                     Department name: 
                     ${emp.dept.deptName} 
                
                <form action = "<% = request.getContextPath ()%>/Insert" method = "post">
                    
                         
                        
                            <input type = "hidden" name = "empPass" value = "${emp.empPass}" />
                            <input type = "hidden" name = "empName" value = "${emp.empName}" /><input type = "hidden"
                                name = "gender" value = "${emp.gender}" /><input type = "hidden" name = "address"
                                value = "${emp.address}" /><input type = "hidden" name = "birthday"
                                value = "${emp.birthday}" /><input type = "hidden" name = "authority"
                                value = "${emp.authority}" /><input type = "hidden" name = "deptId" value = "${emp.dept.deptId}" /><input
                                type = "submit" value = "registration" />
                        
                    
                </form>
                <form action = "<% = request.getContextPath ()%>/html/regist/regist_input.jsp">
                    
                         
                        <input type = "submit" value = "return" />
                        
                    
                </form>
            
        </article>
    
    <footer>Copyright (C) 2017 System Shared co., ltd, ALL Rights Reserved</footer>
</body>
</html>
package jp.co.sss.crud.servlet;
import java.io.IOException;
import java.sql.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jp.co.sss.crud.bean.Dept;
import jp.co.sss.crud.bean.Emp;
import jp.co.sss.crud.db.DeptDao;
import jp.co.sss.crud.db.EmpDao;
@WebServlet ("/ Insert")
public class Insert extends HttpServlet {
    protected void doPost (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding ("UTF-8");
        String empPass = request.getParameter ("empPass");
        String empName = request.getParameter ("empName");
        String gender = request.getParameter ("gender");
        String address = request.getParameter ("address");
        String birthday = request.getParameter ("birthday");
        String authority = request.getParameter ("authority");
        String deptId = request.getParameter ("deptId");
        Dept dept = DeptDao.findById (deptId);
        Emp emp = new Emp ();
        emp.setEmpPass (empPass);
        emp.setEmpName (empName);
        emp.setGender (Integer.parseInt (gender));
        emp.setAddress (address);
        emp.setBirthday (Date.valueOf (birthday));
        emp.setAuthority (Integer.parseInt (authority));
        emp.setDept (dept);
        EmpDao.insert (emp);
        request.getRequestDispatcher ("/ html/regist/regist_complete.jsp"). forward (request, response);
    }
}
  • Answer # 1

    (docs.oracle.com) Date valueOf ()

      

    public static Date valueOf (String s)
      Converts a string in JDBC date escape format to a Date value.
      Parameters:
      s-A String object that represents a date in the format "yyyy- [m] m- [d] d". Leading zeros in mm and dd can be omitted.

    So why not replace/with-asbirthday.replace ("/", "-")?

    The best one is "Convert properly".

    SimpleDateFormat fmt = new SimpleDateFormat ("yyyy/MM/dd");
    fmt.parse (birthday);

  • Answer # 2

    The simple thing is to replace the slash in the birthday string with a hyphen?

    String birthday = request.getParameter ("birthday"). replace ("/", "-");