如何在mysql中存储日期格式? [英] How to store date format in mysql ?

查看:280
本文介绍了如何在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]


这篇关于如何在mysql中存储日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆