Saturday 21 October 2017

java sql date time

itemprop="text">

When I insert a SQL
DateTime to the database I get 2007-02-07
12:00:00.00



But I made the
Date object like this : 2007-02-07
17:29:46.00



How to get the value of
the seconds in the database. It always changes it back to
12:00:00.00




date.setYear(Integer.valueOf(parsedDate[2].replaceAll("
", "")) - 1900);
date.setMonth(Integer.valueOf(parsedDate[0].replaceAll(" ",
"")));
date.setDate(Integer.valueOf(parsedDate[1].replaceAll(" ",
"")));
...
java.sql.Date sqlDate = new
java.sql.Date(date.getTime());


Should
I use any formatters?


itemprop="text">
class="normal">Answer





java.sql.Date
represents a date, not a date and time. From href="http://docs.oracle.com/javase/7/docs/api/java/sql/Date.html"
rel="noreferrer">the
docs:




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.




If you
want to store a date and time, you should look for another type - e.g. href="http://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html"
rel="noreferrer">java.sql.Timestamp. EDIT: That's
not suggesting you use a TIMESTAMP column type - as paulsm4 says in the comments, that's
a different thing. However, as far as I can see, JDBC only
supports:




  • Date
    (no, you want a time
    too)


  • Time (no, you
    want a date too)

  • Timestamp
    (includes a date and time, but you don't want TIMESTAMP SQL
    semantics)



I would
expect using the Java Timestamp type with
a DATETIME column to work, although without the level of precision that
Timestamp
provides.



EDIT: After a bit more research, it
looks like you may want to use the
java.sql.Time type, but with special driver parameters - at
least if you're using the Microsoft driver. See these docs on href="http://msdn.microsoft.com/en-us/library/ff427224.aspx"
rel="noreferrer">configuring JDBC for more
information.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...