Piyush Kumar
Piyush Kumar

Reputation: 113

Matching java time stamp to sql timestamp?

I am trying to pass a date field into a table. The column is of sql timestamp and I keep getting the runtime exception

java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff] at 
java.sql.Timestamp.valueOf(Timestamp.java:235)
at kafkaTest.TruckScheme.deserialize(TruckScheme.java:51) 
at backtype.storm.spout.SchemeAsMultiScheme.deserialize(SchemeAsMultiScheme.java:33) 
at storm.kafka.KafkaUtils.generateTuples(KafkaUtils.java:189) 
at storm.kafka.PartitionManager.next(PartitionManager.java:121) at storm.kafka.KafkaSpout.nextTuple(KafkaSpout.java:124) 
at backtype.storm.daemon.executor$fn__4949$fn__4964$fn__4993.invoke(executor.clj:585) 
at backtype.storm.util$async_loop$fn__452.invoke(util.clj:465) 
at clojure.lang.AFn.run(AFn.java:24) 
at java.lang.Thread.run(Thread.java:745)

I tried to format it using SimpleFateFormat but I am still getting the same error. Is there any way to get the java date format to comply with the exact format that of the sql timestamp type?

Thanks. Piyush

Upvotes: 1

Views: 7798

Answers (2)

Tolga ÇELİK
Tolga ÇELİK

Reputation: 49

my problem was because of format , thank you

SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");

Upvotes: 1

Semih Eker
Semih Eker

Reputation: 2409

With using milisecond level SimpleDateFormatter, you can match them. Please try like this;

 public static void main(String args[]) throws IOException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
    String DateToStoreInDataBase= sdf.format(new Date()); // java.util.Date
    System.out.println(DateToStoreInDataBase);

    Timestamp ts = Timestamp.valueOf(DateToStoreInDataBase); // java.sql.Timestamp
    System.out.println(ts);
  }

And the result is;

2015-05-12 09:02:02.645
2015-05-12 09:02:02.645

Upvotes: 1

Related Questions