Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am sending date in the format of MM/dd/yyyy. i want store this into the mysql database and i had given date as datatype in mysql. Here should i make date conversion beacuse mysql doesnt take date format as MM/dd/yyyy directly from frontend form.
In front end i am using js datepicker.

What I have tried:

I have tried with below code but it gives error
Java
String start_date = request.getParameter("datepicker1");
	     SimpleDateFormat formater = new SimpleDateFormat("MM-dd-yyyy");
	     Date result = formater.parse(start_date);
	 //    SimpleDateFormat newFormater = new SimpleDateFormat("yyyy-MM-dd");
	   //  out.println(newFormater.format(result));
	     
	     String end_date = request.getParameter("datepicker2");
	     SimpleDateFormat formater1 = new SimpleDateFormat("MM-dd-yyyy");
	     Date result1 = formater.parse(start_date);
	  //   SimpleDateFormat newFormater1 = new SimpleDateFormat("yyyy-MM-dd");
	    // out.println(newFormater1.format(result1));
Posted
Updated 17-May-17 21:11pm
Comments
Maciej Los 18-May-17 2:00am    
Date is a date and has nothing to its format. Format of date is the date string representation only and nothing else.
Venkatesh K H 18-May-17 2:07am    
Data truncation: Incorrect date value: '05/18/2017' for column 'start_date' at row 1... error arises like this
Richard MacCutchan 18-May-17 4:05am    
Forget formats, just get the data value from the date picker and pass that into the database. The only time you need string formats is when you want to display the date to a human.

1 solution

Put the date in single quotes and move the parenthesis (after the 'yes') to the end:

SQL
INSERT INTO custorder 
  VALUES ('Kevin', 'yes' , STR_TO_DATE('1-01-2012', '%d-%m-%Y') ) ;
                        ^                                     ^
---parenthesis removed--|                and added here ------|


[EDIT]Off-topic links has been removed - Maciej Los[/EDIT]
 
Share this answer
 
v2
Comments
Maciej Los 18-May-17 3:28am    
Off-topic links are treated as spam.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900