2

I want to convert date to timestamp for setting the variable. I have below code but seems invalid.

Date org_last_upd_ts=xxxxxxx;

SimpleDateFormat sdf=new SimpleDateFormat("SSSSSS");
Timestamp org_lastupdts=new Timestamp(org_last_upd_ts.getTime());
org_lastupdts.setNanos(Integer.valueOf(sdf.parse(o rg_last_upd_ts.toLocaleString()).toString()));

Any idea how to do so?

Ryan Fung
  • 2,069
  • 9
  • 38
  • 60
  • Possible duplicate of [convert date to timestamp UTC](http://stackoverflow.com/questions/25105816/convert-date-to-timestamp-utc) – Bobulous May 23 '16 at 11:56
  • 1
    Possible duplicate of [String-Date conversion with nanoseconds](http://stackoverflow.com/questions/12000673/string-date-conversion-with-nanoseconds) – piyushj May 23 '16 at 11:57

1 Answers1

1

You can convert it like

java.util.Date now = new Date();
java.sql.Timestamp timestamp = new Timestamp(now.getTime());

From the javadoc Timestamp constructor

Constructs a Timestamp object using a milliseconds time value. The integral seconds are stored in the underlying date value; the fractional seconds are stored in the nanos field of the Timestamp object.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69