Should I handle timestamp as String in java or as long? How to convert timestamps from Java to MySQL? Is there any standard pattern followed?
-
1use http://stackoverflow.com/questions/17808875/to-store-a-date-in-mysql-as-dd-mm-yyyy-format-through-netbeans-but-mysql-genera – HackerGK Dec 10 '13 at 09:20
3 Answers
First of all, to persist a java.util.Date
into a database in Java, you will have to convert it to java.sql.Date
. The fortunate thing about JDBC SQL Date is that it's a subclass of Java Date.
Therefore, to create a java.sql.Date from java.util.Date, you will have to do this:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = formatter.parse(date);
java.sql.Date sqlDate = new java.sql.Date(myDate.getTime());
db.insert_date(sqlDate);
Make sure that db.insert_date accepts only java.sql.Date
.
also read about java.util.Date vs java.sql.Date

- 1
- 1

- 2,799
- 3
- 30
- 45
Should I handle timestamp as String in java or as long? you should not because timestamp class is already present in sql package
How to convert timestamps from Java to MySQL? use setTimestamp() and for example see this example

- 13,738
- 20
- 78
- 116
As such no standard pattern is followed. There is a method called setTimestamp available in PreparedStatement. You can use it to set timestamp values. More info is available at - http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setTimestamp(int, java.sql.Timestamp) and http://alvinalexander.com/java/java-timestamp-example-current-time-now

- 9,049
- 3
- 48
- 70