How to extract the value of TIME(7) field of MS SQL SERVER 2008 R2 in java?

154 Views Asked by At

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();
1

There are 1 best solutions below

5
On

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