NONA
NONA

Reputation: 3

How can I write a Timestamp in java so I can insert in mysql

My table, chat_users, contains an attribute last_activity that is a Timestamp.

What is the Java code to write that value?

I tried Timestamp last_activity ; but i don't know how to pass parameter to it.

Upvotes: 0

Views: 691

Answers (2)

Francisco Spaeth
Francisco Spaeth

Reputation: 23913

In order to use Timestamp, you could use it like this:

Date dateObject = new Date(); // your date object
new Timestamp(dateObject.getTime());

Another possibility, depending on how you are implementing it: System.currentTimeMillis().

Basically System.currentTimeMillis() returns the number of milliseconds since January 1, 1970, 00:00:00 GMT until current time. The same thing does Date.getTime().

Upvotes: 3

Garis M Suero
Garis M Suero

Reputation: 8169

Just use:

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

Reference: Timestamp

Upvotes: 1

Related Questions