-2
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); 
            java.util.Date date = null;
             date = sdf.parse(request.getParameter("cdate"));
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
manojrohit
  • 17
  • 1
  • 4

2 Answers2

2

Mistake in syntax change your new SimpleDateFormat("yyyy-mm-dd hh:mm:ss") to new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")..

 System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2015-04-12 23:59:59")); 

and your request.getParameter("cdate") string must be in same format i.e. "yyyy-MM-dd HH:mm:ss"

Nikhi K. Bansal
  • 444
  • 2
  • 14
0

You should really be doing
Date date = sdf.parse(ar);
to reuse the earlier string.

To make this a bit smoother:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
    String ar=request.getParameter("cdate"); 
    if(ar == null)
        ar = "";
    ar = ar.trim();
    if(request.getParameter("Submit")!=null) {
        PreparedStatement ps=null;
        Connection con=null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); 
            java.util.Date date = null;
            date = sdf.parse(ar);

            Class.forName("com.mysql.jdbc.Driver");
            con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/form","root","");
            String q="insert into t1 values (?)";

            ps= (PreparedStatement) con.prepareStatement(q);

            ps.setString(1,ar);

            ps.executeUpdate();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

It would be very helpful if you listed any examples of the failure, but hopefully this fixes any issues you have.

Justin
  • 11
  • 3
  • still I get same error as I got earlier like "java.text.ParseException: Unparseable date: "01/05/2016" " Can you explain me from code where I make mistake because I did as you said but still get prblm it means I am making mistake so explain me. – manojrohit Jan 05 '16 at 07:04