Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

get milliseconds from java.sql.Timestamp

 
verduka fox
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am calling a stored procedure in Sybase via JDBC. One of the columns in the resultset is a date time field. I am using a java.sql.Timestamp object to retrieve the data:

However, this value stops at the "seconds" place; it does not have the milliseconds. The milliseconds are included in the database. I looked in the API and found the following note in the java.sql.Timestamp class:

This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the java.util.Date component. The fractional seconds - the nanos - are separate. The getTime method will return only integral seconds. If a time value that includes the fractional seconds is desired, you must convert nanos to milliseconds (nanos/1000000) and add this to the getTime value.

I then tried the following code based on the suggestion:

The code for Converter.convertDateToGregorianCalendar:

However, when I try to compile this code, I get an error saying that get/setTimeInMillis in not visible. I looked; it is protected not public.
I don't understand what it means by "you must convert nanos to milliseconds (nanos/1000000) and add this to the getTime value". Can anyone help me get the milliseconds stored in the GregorianCalendar object? It is important for my application. I appreciate your comments.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65337
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, don't forget to do the division to convert the nanos to millis before adding.
Then create a java.util.Date object from the sum of the millis and the converted nanos, and use the Date to set the GregorianCalendar.
hth,
bear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic