[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Store Java date and time in MySQL

Posted on 2010-04-06
2
Medium Priority
?
667 Views
Last Modified: 2013-11-24
How do I store a date and time in MySQL using java servlets?
First I create current date/time:
            Calendar cal = Calendar.getInstance();
            Date now = cal.getTime();
This is correct. I can output correct date and time:
            SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
                System.out.println(sdf.format(now));
gives correct date and time.
In prepared statement I use:
                  pstmt.setDate(5, new java.sql.Date(ticket.getCreationDate().getTime()));
When I check the table, it has only stored the date: 2010-04-06 00:00:00
The MySQL datatype is DATETIME.
I do not want to store it as a string, because I want to sort by date when retrieving the data.
0
Comment
Question by:wolfje_xp
2 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 2000 total points
ID: 29881632
use DATETIME datatype in MySQL and use
pstmt.setTimestamp( new Timestamp( ticket.getCreationDate().getTime() ) ) ;
0
 
LVL 5

Author Closing Comment

by:wolfje_xp
ID: 31711246
Thank you very much!
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question