This question already has an answer here:
I'm facing some problems in Eclipse with dates. I'm showing a date in a TextField with the format
dd/MM/yyyy
and I need to get the text from this textfield and parse it as a date to insert it in my database into a column of type DATE
I'm using MySQL and it accepts dates as
yyyy-MM-dd
I tried really many options, both with LocalDate
or the older Date
, but didn't find a solution. Can someone please help? Thank you!
Try this:
java.time
The other Answers are correct but use outdated classes. The old date-time classes such as java.util.Date/.Calendar have proven to be poorly designed, confusing, and troublesome. Avoid them. They have been supplanted by the java.time framework built into Java 8 and later.
Define a pattern by which to parse your input string.
Parse the input string as a
LocalDate
object, representing a date-only value without time-of-day nor time zone.Someday the JDBC drivers will be updated to work directly with the java.time types. But until then we must convert to the java.sql types for transferring data to/from the database.
Then use a
PreparedStatement
, callingsetDate
and passing your sqlDate to write the value into the database.To retrieve from the database, call
getDate
. Immediately convert to java.time by callingjava.sql.Date::toLocalDate
. Minimize your use of the java.sql types. Do your business logic in java.time.If your JDBC driver complies with JDBC 4.2 and later, you can skip the java.sql types and use the java.time types directly.
PreparedStatement::setObject
myPrepStmt.setObject( … , myLocalDate )
ResultSet::getObject
LocalDate ld = myResultSet.getObject( … , LocalDate.class );
This Question is really a duplicate. Search Stack Overflow for many more examples.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.Prior Java 8 approach
Firstly, after retrieving your string from your
TextField
, you should parse it tojava.util.Date
:Afterwards, you can convert your
java.util.Date
into ajava.sql.Date
, to store it into your MySQL database, like this:Java 8 approach
As mentioned by @BasilBourque, the Java date and time mechanism was provided by java.util.Date, java.util.Calendar, and java.util.TimeZone classes which are now legacy.
Therefore, in order to accomplish using Java 8 what was previously mentioned, one can do the following:
Afterwards, in order to convert the
LocalDate
into ajava.sql.Date
, to store it into your MySQL database, it's possible to do the following:You want to use the
SimpleDateFormat
classOnce you have extracted it from the TextField, you can use this: