-1

I am trying to convert the string "5/23/14 02:23:24" from a String in Eclipse to a Date to insert into a SQL statement. The code I have is as follows:

String dateAndTime = "5/23/14 02:23:24";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
Date date = sdf.parse(dateAndTime);
long dateLong = date.getTime();
insertStatement.setDate(1, new java.sql.Date(dateLong));

I expect to see

23-MAY-2014 2.23.24.000000000 PM

in my table, but instead I see

23-MAY-2014 12.00.00.000000000 AM

Can anyone shed some light on what I am doing wrong?

Thanks!

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
ola
  • 882
  • 2
  • 12
  • 29
  • Related: [java.util.Date vs java.sql.Date](http://stackoverflow.com/questions/2305973/java-util-date-vs-java-sql-date) – Pshemo May 23 '14 at 16:31
  • Check this answer by Jon. http://stackoverflow.com/questions/8530545/java-sql-date-time – Venkatesh May 23 '14 at 16:40

3 Answers3

7

A standard DATE SQL type doesn't have any time information associated with it, so the javadocs for java.sql.Date state:

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

You'll want to use a java.sql.Timestamp instead. It corresponds to the SQL type TIMESTAMP, which holds date and time of day data.

rgettman
  • 176,041
  • 30
  • 275
  • 357
6

You're calling setDate, which uses a java.sql.Date. That represents just a date, not a date and time.

You should consider using setTimestamp instead, with a java.sql.Timestamp. (There may be other ways of doing it for your specific database, but that's probably the simplest general solution):

long dateLong = date.getTime();
insertStatement.setTimestamp(1, new java.sql.Timestamp(dateLong));
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

java.time

The legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.

Solution using modern date-time API:

  1. Parse the date-time string into LocalDateTime:
String dateAndTime = "5/23/14 02:23:24";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d/uu H:m:s", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(dateAndTime, dtf);
  1. Set the instance of LocalDateTime into the database using PreparedStatement#setObject:
Statement st = conn.createStatement();
PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
st.setObject(1, ldt);
st.executeUpdate();
st.close();
  1. In order to read it from the database, use Resultset#getObject:
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM mytable WHERE ...");
while (rs.next()) {
   LocalDateTime ldt = rs.getObject(1, LocalDateTime.class));
   System.out.println(ldt);
}
rs.close();
st.close();

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110