• 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

get milliseconds from java.sql.Timestamp

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic