0

I am developing a simple web project. I want to store the date selected by the user with current time in the MySQL database. While trying to do that I am getting time as 00:00:00. What should I do to get the current time?

HERE IS MY CODE

String startdate = req.getParameter("date1");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate = dateFormat.parse(startdate);
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
emp.store(timestamp); //method to store in database

In HTML page:

<input type="datetime" name="date1"/>
iqstatic
  • 2,322
  • 3
  • 21
  • 39
Avi nash
  • 11
  • 4
  • You're using a date-only format mask when you're parsing the incoming date. Why would it retain the time component when you haven't asked it to? – JonK Oct 19 '15 at 09:47
  • you are formatting to have date alone. change something like `SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");` – PK20 Oct 19 '15 at 09:49
  • hi JonK when i try that it's giving unparsable date exception. – Avi nash Oct 19 '15 at 09:51
  • So what format is the incoming date in? If you could provide an example that would be good. – JonK Oct 19 '15 at 09:52
  • refer this link to get current date/time. Since you said date is input by user and you have to get current time to insert into db. [link](http://stackoverflow.com/questions/5175728/how-to-get-the-current-date-time-in-java) – PK20 Oct 19 '15 at 09:54
  • 2015-10-12 this is my incoming date from html page.I'm not getting time here that is my problem – Avi nash Oct 19 '15 at 09:54
  • read the link in my above comment to get current time. Regards – PK20 Oct 19 '15 at 09:55
  • What browser are you running this in? – JonK Oct 19 '15 at 10:01
  • This is the method I use since we have to work in JRE 6 and 7. `String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime().set(Calendar.HOUR, 0).set(Calendar.MINUTE, 0).set(Calendar.SECOND, 0));` – Darren Willows Oct 19 '15 at 10:04
  • While i am executing this i get the current date and time but i need only the current time not the current date because date is to be selected by the user. – Avi nash Oct 19 '15 at 10:07

0 Answers0