0

I have a Timestamp column in my source database which is getting read in my java program as a String.

String dateWithNano = "2011-10-02 18:48:05.123456";

I have to set the same Timestamp in the destination database column.

I am doing the following for the same:

java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS");
if(dateWithNano== null || dateWithNano.equalsIgnoreCase("")){
        ps.setTimestamp(1,null);
    }else{
        java.util.Date parsedDate = format1.parse(item.getUPDATED_TS());
        ps.setTimestamp(1, new java.sql.Timestamp(parsedDate.getTime()));
    }

But the nanosecond value in the destination database is getting compromised. Its not same as 2011-10-02 18:48:05.123456

What is the correct way for this so that the same nanosecond value persists across source and destination database ?

Vicky
  • 16,679
  • 54
  • 139
  • 232
  • What do you mean by "getting read in my Java program as a string"? Do you mean when you're fetching it from the database, or when you're trying to instead it *into* the database? – Jon Skeet Jan 18 '16 at 10:44
  • @JonSkeet: I am fetching from source database as a String (rs.getString(columnName)). The String is of the format 2011-10-02 18:48:05.123456. I want the same (including nanoseconds) to go into the Timestamp column of destination database. But what I did above is changing it. – Vicky Jan 18 '16 at 10:45
  • *Why* are you fetching from the database as a string? If it's a Timestamp in the database, why not use `getTimestamp`? – Jon Skeet Jan 18 '16 at 10:47
  • @JonSkeet: I have a generic spring batch program where all columns get read as String. I can not change that. – Vicky Jan 18 '16 at 10:49
  • 2
    Do you actually get nanoseconds from the database in the begining? What is the database and column type? Does it support nanoseconds? Maybe, show the update statement as well – vnov Jan 18 '16 at 10:52
  • So does `getString` only return the microsecond precision, as shown by your example? If so, you've clearly got conflicting requirements... (Do you actually need nanosecond precision, or is microsecond enough?) – Jon Skeet Jan 18 '16 at 10:52
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 24 '18 at 19:44

0 Answers0