0

I am using Apache Meta-model to extract the values from MS SQL SERVER 2008 R2 database. There is a field in MS SQL SERVER 2008 R2 database of type TIME(7) whose java equivalent type is java.sql.Time but java.sql.Time is NOT showing the milliseconds. e.g.

Actual `TIME(7)` value in database: `12:28:16.9475678`
whereas I getting: `12:28:16`

How to get the Time in milliseconds?

Updated:

When I tried to get milliseconds using org.joda.time.LocalTime as you can see below:

   LocalTime localTime = new LocalTime(((Time)row.getValues()[pos]).getTime());
   localTime.toString(); 

Output: 12:28:16.947 (It showing the round value of 9475678)

After converting it to String, I am getting the round value of milliseconds 12:28:16.947. The millisecond precision is lost. Please suggest how to get the the exact value in millisecond without rounding value?

Updated:

I tried using getNanos() as suggested,but i am getting zero milliseconds except my exact millisecond value like:

actual value: 12:28:16.9475678 and i am getting 12:28:16.9470000

code:-

 long timestamp =new Timestamp(((Time)row.getValues()[pos]).getTime()).getNanos();
santosh
  • 435
  • 1
  • 7
  • 24

1 Answers1

2

java.sql.Time doesn't include milliseconds. Try using java.sql.Timestamp instead, as that will include the fractional seconds.

Rick
  • 4,575
  • 1
  • 26
  • 20
  • I tried it with `org.joda.time.LocalTime` and it automatic rounding the milliseconds value. – santosh Jun 13 '16 at 13:33
  • @shree using `LocalTime` is not going to help you, because you will still first get the `java.sql.Time`. Get it as timestamp (you may need to convert/cast to timestamp in your query first(!)), and also look at [`Timestamp.getNanos()`](http://docs.oracle.com/javase/8/docs/api/java/sql/Timestamp.html#getNanos--) which contains the fractional seconds in nanoseconds. – Mark Rotteveel Jun 13 '16 at 13:40
  • @MarkRotteveel. I will give it try and Let you know. – Ashish Pancholi Jun 13 '16 at 13:43
  • @MarkRotteveel.I tried Timestamp.getNanos(),it gives me lost value after three digits and append zero in place of actual values as mentioned above – santosh Jun 14 '16 at 13:09
  • I see you're still casting it to java.sql.Time before passing it to the Timestamp constructor. My knowledge of java.sql.Time is out of date, it does in fact support milliseconds, but not nanoseconds. This is why you are only getting milliseconds. Check the type being returned by "row.getValues()[pos]". If it's a Timestamp then cast it as that to maintain the nanoseconds. If it's a Time then there is nothing you can do, the nanoseconds are not available to you. – Rick Jun 14 '16 at 14:26