Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting java.sql.Timestamp to java.util.Calendar without losing precision

I'm trying to fetch timestamp values from a database, convert them to Calendar, and convert them back to Timestamp, but they lose their precision.

Here's the code to reproduce the problem

import java.sql.Timestamp; import java.util.Calendar;  public class Test {     public static void main(String[] args)     {         Timestamp timestamp = new Timestamp(112, 10, 5, 15, 39, 11, 801000000);         System.out.println("BEFORE "+timestamp.toString());         Calendar calendar = Calendar.getInstance();         calendar.setTimeInMillis(timestamp.getTime());         timestamp = new Timestamp(calendar.getTimeInMillis());         System.out.println("AFTER "+timestamp.toString());     } } 

Here is the sample result of conversion

BEFORE 2012-10-18 14:30:13.362001 AFTER 2012-10-18 14:30:13.362 

It loses its precision. What do I do to keep the decimal value as it is?

like image 485
William Wino Avatar asked Nov 06 '12 09:11

William Wino


People also ask

Does Java Util date have timestamp?

util. Date that allows the JDBC API to identify this as an SQL TIMESTAMP value. It adds the ability to hold the SQL TIMESTAMP fractional seconds value, by allowing the specification of fractional seconds to a precision of nanoseconds.

How do I save the current timestamp in Java?

Java Code:Timestamp ts = new Timestamp(date. getTime()); Above example command will return the current timestamp and save values in ts object variable.

What is Calendar getInstance () in Java?

The getInstance() method in Calendar class is used to get a calendar using the current time zone and locale of the system. Syntax: public static Calendar getInstance() Parameters: The method does not take any parameters. Return Value: The method returns the calendar.


2 Answers

You are setting the time in milliseconds, but your input precision is in microseconds, so of course you are going to lose any precision finer than milliseconds.

Calendar doesn't support finer granularity than milliseconds. There is no work-around.

like image 135
Bohemian Avatar answered Sep 30 '22 17:09

Bohemian


java.time

In Java 8 and later we now have a solution: the new java.time framework is built in. Defined by JSR 310, inspired by Joda-Time, extended by the ThreeTen-Extra project.

This new framework has nanosecond resolution. That's enough to handle the old java.util.Date values, Joda-Time values, both being milliseconds. And it's enough to handle the microseconds used by some databases such as Postgres. Some databases such as H2 use nanoseconds. So java.time can handle it all, or least all the mainstream situations. The only exceptions would be niche scientific or engineering situations.

Image

With Java 8, the old classes got new methods for converting to/from java.time. On java.sql.Timestamp use the new toInstant and fromInstant methods. The Instant class contains a count of nanoseconds from epoch, first moment of 1970 UTC (basically, see class doc for details).

From an Instant you can get a ZonedDateTime object.

java.sql.Timestamp ts = myResultSet.getTimestamp( 1 ); Instant instant = ts.toInstant(); ZoneId zoneId = ZoneId.of( "America/Montreal" ); ZonedDateTime zdt = instant.atZone( zoneId ); 
like image 35
Basil Bourque Avatar answered Sep 30 '22 17:09

Basil Bourque