aspose file tools
The moose likes JDBC and the fly likes get milliseconds from java.sql.Timestamp Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "get milliseconds from java.sql.Timestamp" Watch "get milliseconds from java.sql.Timestamp" New topic
Author

get milliseconds from java.sql.Timestamp

verduka fox
Ranch Hand

Joined: Jan 18, 2001
Posts: 178
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

Joined: Jan 10, 2002
Posts: 56150
    
  13

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


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
I agree. Here's the link: http://jrebel.com/download
 
subject: get milliseconds from java.sql.Timestamp
 
Similar Threads
please help with an example
Comparing Date and Timestamp: unexpected result
Time Difference
truncation errors storing a Date in MySQL table
java.util.Date - after function not considering time?