Reputation: 3471
I'm using JDBC to export data from postgreSql database to text file. I need to export / import all the possible types to String. Most of types serialize perfectly except java.sqlTime. When I create both Time and TimeStamp using SQL NOW() it creates records on my database:
time | timestamp
--------------------------------------------
13:17:03.121719 | 2017-10-13 13:17:03.121719
Object time is mapped by JDBC into java.sql.Time. To write it I simply use:
String time = value.toString();
When I want to map it from text file back to java.sql.Time and import to database I use:
java.sql.Time.valueOf(value.toString());
It works perfectly with java.sql.TimeStamp, but not with java.sql.Time. After importing to database (using preparedStatement.setTime()) i get:
13:17:03
instead of 13:17:03.121719
I tried also export like this:
String time = String.valueOf(value.getTime());
and import:
new java.sql.Time(Long.parseLong(value.toString()));
Result is better, but I get:
13:17:03.121
instead of 13:17:03.121719
So I'm still losing milliseconds precision. Is there any better way to do it ?
Upvotes: 0
Views: 211
Reputation: 73578
Time.toString()
returns the value in hh:mm:ss
format, meaning that any milliseconds are discarded. The milliseconds aren't absent from the object, but they're not visible by default. The getTime()
method returns the most accurate state of the object, as can be observed from the following code
Time t = new Time(System.currentTimeMillis()+1345);
System.out.println(t);
System.out.println(new Timestamp(t.getTime()));
Note that you still get only millisecond precision.
Your real issue is in exporting the data in text format through your own code. Unless there's a valid reason for it, you shouldn't even bother. You can take binary dumps that are a lot easier and accurate, or use any other ready made tools.
Upvotes: 1