12

I used the following code, but the DateTime field in SQL is represented as:

2005-04-08 00:00:00

I want to have the time too. what should I change?

Here is my code below:

// Get the system date and time.
java.util.Date utilDate = new Date();
// Convert it to java.sql.Date
java.sql.Date date = new java.sql.Date(utilDate.getTime());
...
...
...
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setDate(1, date);
1
  • 1
    On Microsoft SQL-Server the easiest way to insert current dates to datatables is to add a Default Value to DateTime (or SmallDateTime) field: getdate() Commented Jul 28, 2010 at 8:00

8 Answers 8

12

Try using setTimestamp instead of setDate, since a timestamp is the prefered format for data and time.

Sign up to request clarification or add additional context in comments.

1 Comment

java.sql.Date only supports the date, not time. From the javadoc - "To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated."
6

you can set Timestamp import java.sql.Timestamp;

stmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));

instead of

stmt.setDate(1, date);

o/p will be: 2014-05-16 08:34:52.977

set your DB --column as Timestamp.

Comments

4

I wouldn't bother retrieving the current time in Java, just let the database do the job:

String sql = "insert into some_table (some_column, timestamp_column) values (?, current_timestamp)";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setInt(1, 42);
stmt.executeUpdate();

Comments

2

Try this:

Connection con;
Statement stmt;
con = DriverManager.getConnection(url, username, password);
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO MYTABLE(time) " +
"VALUES ('" + new java.sql.Date(System.CurrentTimeMillis())"');");

Comments

1

Use the sql function now().

INSERT INTO table date_field VALUES(NOW());

Comments

0

Try using setDate(utilDate) instead of trying to set SQL date. In SQL, date and time are different data types.

Comments

0

I did as follows:

con = new BDConesion(); //Conexion with de DB
Connection cn = con.conexion()
PreparedStatement st = null;
st = cn.prepareStatement("INSERT INTO TABLE (Id,Fecha,contendido) VALUES(?,?,?)");
st.setString(1,String.valueOf(key)); //convert the int to string
st.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
st.setString(3,Cont);//Cont it is a varible string
st.executeUpdate();

I hope you serve

Comments

-2

Actually, the date in database is a string like "yyyy-mm-dd".

Why not to try:

String date = "2010-07-28";

stmt.executeUpdate("INSERT INTO MYTABLE(time) " + s);

It's just for the date which you want.

If you want insert now, use the command like:

INSERT INTO mytable(now());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.