user2192774
user2192774

Reputation: 3907

How to insert current time in MySQL using Java

I need to insert the current date and time (milliseconds) into MySQL. I did the following:

long time = System.currentTimeMillis();
java.sql.Timestamp timestamp = new java.sql.Timestamp(time);
System.out.println("Time in milliseconds :" + timestamp);

And I got the time in milliseconds correctly (e.g. 2013-03-21 00:08:33.523). However, I need to insert this information into MySQL database. I tried the following using PreparedStatement

prepStmt.setTimestamp(2,timestamp);

But the time is inserted without milliseconds (e.g. 2013-03-21 00:08:33).

How can I insert the time with milliseconds.

EDIT: The column in the database is of DATETIME type.

Upvotes: 6

Views: 31136

Answers (4)

Chinmoy
Chinmoy

Reputation: 1476

You can create a method getCurrentTimeStamp() and call it from JDBC statement

preparedStatement.setTimestamp(4,getCurrentTimeStamp());

Method:

private static java.sql.Timestamp getCurrentTimeStamp() {
 
    java.util.Date today = new java.util.Date();
    return new java.sql.Timestamp(today.getTime());
 
}

Upvotes: 1

clav
clav

Reputation: 4251

java.util.Date date = new java.util.Date();
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.getTime());
preparedStatement.setTimestamp(1, timestamp);

Upvotes: 10

thébé
thébé

Reputation: 114

Since the question has been correctly answered by other member let me add some personal hindsight about the storage of date type in database.

On a personal level I prefer to store date information in the long format. I found it easier to both store and use afterward. Not only most language offers to create a new Date with a long parameter but you won't be stuck with a preset format, you will have the opportunity to format it differently. For example, I have to format date to either English or French format on some website I develop, it's then easier to have a if-else if on some language parameter and the apply a given format depending on the language preference.

However, this method have some drawback. You will need to format it back to a readable format for, let's say, automated reports done by a query that won't use Java to format it back.

-----------------------------ADD CODE-----------------------------

    // Stored as 0 if the date is not initialised
    long time = objInscription.getDatePremierVisionnement() != null ?
        objInscription.getDatePremierVisionnement().getTime() : 0;

-------------------------------GET CODE----------------------------
    // long is store as 0 if the date has not been initialised           
    long long = rs.rsReadColumnLong(1);
    if (long  == 0) {
        someobj.setSomeDate(null);
    } else {
        someobj.setSomeDate(new Date(long));
    }

You will be able to use SimpleFormat easily to display your data.

Upvotes: 0

Ozzie
Ozzie

Reputation: 11673

It the column is of type DATETIME use the setDate method of PreparedStatement.

java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
stmnt.setDate(1, date);

Upvotes: 2

Related Questions