如何在mysql中存储日期格式?
问题描述:
我发送日期的格式为MM / dd / yyyy。我希望将它存储到mysql数据库中,我在mysql中将日期作为数据类型。这里我应该进行日期转换,因为mysql不会直接从前端形式将日期格式作为MM / dd / yyyy。
在前端,我使用的是js datepicker。
我尝试了什么:
我尝试使用下面的代码,但它给出了错误
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
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));
答
将日期放在单引号中并将括号(在是之后)移到结尾:
Put the date in single quotes and move the parenthesis (after the 'yes') to the end:
INSERT INTO custorder
VALUES ('Kevin', 'yes' , STR_TO_DATE('1-01-2012', '%d-%m-%Y') ) ;
^ ^
---parenthesis removed--| and added here ------|
偏离主题的链接已被删除 - Maciej Los [/ EDIT]