How to save current date and time to database using java?
Solution 1
You need to use sql timestamp for saving to the database. Convert your java.util.Date to java.sql.Timestamp:
ps.setTimestamp(new java.sql.Timestamp(myDate.getTime()));
Solution 2
You don't need to parse before formatting:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String frmtdDate = dateFormat.format(date);
System.out.println("frmtdDate: " + frmtdDate);
However, if you are trying to fit the date into some DB statement, you should not do it in the form of text, instead use one of the JDBC setters that utilize java.sql.Date
or java.sql.Timestamp
Solution 3
format
takes a Date
and returns a formatted String
. parse
takes a formatted String
and returns a Date
object. When you do parseFormat.parse(dateFormat.format(date))
you are converting Date
to String
and to Date
again. The value that is printed is the default representation provided by Date.toString()
instead of the formatted string.
System.out.println("current: " +dateFormat.format(date));
Daniel Morgan
Updated on March 05, 2020Comments
-
Daniel Morgan over 4 years
I am using the following code to get the current date and time, but the output is not what I am expecting and I cant save it into database.
Output >> current: Tue Mar 05 09:58:26 EST 2013
Expected output >> current: 2013-03-05 9:58:26
.....{ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); try { System.out.println("current: " +parseFormat.parse(dateFormat.format(date))); return parseFormat.parse(dateFormat.format(date)); } catch (ParseException ex) { Logger.getLogger(ConstructionModel.class.getName()).log(Level.SEVERE, null, ex); } return date; } ...... ps.setDate(....) <<< failed
Database
name type mydate Date
-
Kamil over 11 yearsWhy you are using client PC time instead of database server time?Using
CURRENT_TIME
orNOW
SQL function is better for many reasons including that all data is based one , server system clock.
-
-
JB Nizet over 11 yearsThat wouldn't save the time part of the date.
-
Igor Rodriguez over 11 years@Daniel: You'll need to set it as Timestamp, to save the date and time. See my answer.
-
amphibient over 11 yearsdocs.oracle.com/javase/7/docs/api/java/sql/…, java.sql.Date)
-
amphibient over 11 years@IgorRodriguez, with most JDBC APIs I know of, it can be done as a Date, does not have to be a Timestamp
-
Daniel Morgan over 11 yearsI did it but it gives error which says, no suitable method found for setTimestamp(int,Date) method PreparedStatement.setTimestamp(int,Timestamp,Calendar) is not applicable method (int,Timestamp) is not applicable ....
-
Igor Rodriguez over 11 years@Daniel: You need to create a new Timestamp with your date, as in the example.
-
Daniel Morgan over 11 yearsI changed it to your code and used the setTimestamp but gives me an error.
-
Igor Rodriguez over 11 years@foampile: If I don't have bad memory, I had problems with Date with some in-memory database. That's the reason I prefer now to use timestamps.
-
Igor Rodriguez over 11 years@Daniel: There are 2 different Timestamp. Are you using the one in the java.sql package? Note that your IDE may have imported java.security.Timestamp, that is a different one.