Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Losing time when manipulating a date time and sending it to database (java.sql.Date) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Losing time when manipulating a date time and sending it to database (java.sql.Date)" Watch "Losing time when manipulating a date time and sending it to database (java.sql.Date)" New topic
Author

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

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

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


True wisdom is in knowing you know nothing - Socrates
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Great, thanks very much for the help
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Losing time when manipulating a date time and sending it to database (java.sql.Date)
 
Similar Threads
java.sql.date
URGENT!!! Date class in java.sql and java.util packages
why get different result in different java version
Difference between sql.Date and util.Date
unable to retrieve timestamp in my servlet