This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Losing time when manipulating a date time and sending it to database (java.sql.Date)

 
Matt Kohanek
Village Idiot
Ranch Hand
Posts: 484
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am sending a date and time to a Jax WS. Then the WS is connecting to a SQL database and inserting this data into a table. When I send the date and time in, it has the correct date and time. Then I perform the following before sending it to the database:

The secureUserTrans object is storing the date in java.util.Date format. When this date makes it to the database, it is getting stored in a truncated format without the time. No truncation is being done on the java or SQL side explicitly.

I did notice this when reading about java.sql.Date:
A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT.

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.

Could that be where this is occurring? I thought using .getTime() would deal with this.

I am not yet in a position to step through this code and see exactly where the date is being changed. I will be doing that sometime today I hope. But I thought I might ask here if I am right about that quote. But even if that were the case I would still expect the date and time in the database, only that the time would be all zeros. But maybe that is how it gets displayed/stored if the time is set to 0?


edit -
oh the main reason I opened this thread is to ask how I can deal with java.sql.Date setting my time to zero if that is in fact what is happening. Is there any way to deal with this since I need to have the correct time when I insert this into the database, short of sending a separate time parameter (and adding a column for time to my table ...)
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use java.sql.Timestamp instead. The java.sql package has three date/time classes: Date for only dates, Time for only times, and Timestamp for full time stamps (date+time).
 
Winston Gutkowski
Bartender
Pie
Posts: 10243
58
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Kohanek wrote:The secureUserTrans object is storing the date in java.util.Date format. When this date makes it to the database, it is getting stored in a truncated format without the time. No truncation is being done on the java or SQL side explicitly.
...
Could that be where this is occurring? I thought using .getTime() would deal with this.

Let's deal with that first.
An SQL DATE type, as far as I recall, does not store time (although there may be variants that do; SQL is awful when it comes to standardization).
What you need is a TIMESTAMP, and that you can store (oddly enough) with java.sql.Timestamp.

Winston

[Edit] Too slow
 
Matt Kohanek
Village Idiot
Ranch Hand
Posts: 484
Java jQuery Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, thanks very much for the help
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic