-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!

2

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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));

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.