• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...)
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great, thanks very much for the help
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic