You should usually not care about the date format. If you do, then it means that you're treating dates as strings by storing them as String
in Java side and as VARCHAR
in DB side. This is wrong. They should in Java side be stored as java.util.Date
and in DB side as DATETIME
or TIMESTAMP
or maybe DATE
(if you don't care about the time part).
The normal way to store a date (with time) is to use PreparedStatement#setTimestamp()
:
preparedStatement = connection.prepareStatement("INSERT INTO person (birthdate) VALUES (?)");
preparedStatement.setTimestamp(1, new Timestamp(date.getTime()));
// ...
And the normal way to retrieve it back is to use ResultSet#getTimestamp()
:
Date date = resultSet.getTimestamp("birthdate");
// ...
This not only removes the need to worry about DB-specific default date string formats, but this also allows for more fine grained control over selecting dates using >
, <
, BETWEEN
, etc in SQL side and more fine grained date formatting using SimpleDateFormat
in Java side.
See also: