SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
java.util.Date date = null;
date = sdf.parse(request.getParameter("cdate"));
Asked
Active
Viewed 125 times
-2

BalusC
- 1,082,665
- 372
- 3,610
- 3,555

manojrohit
- 17
- 1
- 4
2 Answers
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
-
I did as you said but somehow I still get problem and show error as java.text.ParseException: Unparseable date: "01/05/2016" – manojrohit Jan 05 '16 at 07:01
-
you must have to give date in format of yyyy-MM-dd HH:mm:ss. your date it must be i.e. 2016-05-01 00:00:00 – Nikhi K. Bansal Jan 05 '16 at 07:10
-
I did it but somehow not get parse date. – manojrohit Jan 05 '16 at 07:11
-
if your incoming date in format of 01/05/2016 you must have to use. new SimpleDateFormat("dd/MM/yyyy").parse("01/05/2016") – Nikhi K. Bansal Jan 05 '16 at 07:12
-
tell me the output of your **request.getParameter("cdate")** – Nikhi K. Bansal Jan 05 '16 at 07:14
-
01/05/2016 this is output of my cdate – manojrohit Jan 05 '16 at 07:18
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99773/discussion-between-manojrohit-and-nikhi-k-bansal). – manojrohit Jan 05 '16 at 07:49
-
you have to use '**new SimpleDateFormat("dd/MM/yyyy").parse(request.getParameter("cdate"))** ' this format to parse your date – Nikhi K. Bansal Jan 05 '16 at 08:53
-
At last I get solution for that SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy"); java.util.Date util_StartDate = format.parse( request.getParameter("cdate") ); java.sql.Date sql_StartDate = new java.sql.Date( util_StartDate.getTime() ); ps.setDate( 1, sql_StartDate ); – manojrohit Jan 05 '16 at 15:24
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