tl;dr
I want it in the format yyyy-MM-dd to insert into a MySQL database
No need to generate or parse a String. With a Date
object in hand, convert to a java.time.Instant
, apply a time zone ZoneId
to generate a ZonedDateTime
, and extract a LocalDate
. Pass the LocalDate
via JDBC 4.2 and later.
myPreparedStatement.setObject(
… ,
myJavaUtilDate.toInstant()
.atZone( ZoneId.of( "Pacific/Auckland" ) )
.toLocalDate() ,
LocalDate.class
)
Details
jCalendar.getDate() returns a date in the following form: Mon Apr 06 11:10:00 PDT 2015
The old Date
& Calendar
classes have many flaws. One of the flaws is the poor design decision to have the Date::toString
method apply a time zone during the process of generating the string. This creates the illusion of an assigned time zone when actually the Date
value is always in UTC.
Avoid the troublesome old date-time classes. They are now legacy, supplanted by the java.time classes.
Convert that Date
to an Instant
. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant instant = myUtilDate.toInstant() ;
For a date-only value, you must decide on time zone. For example, a few minutes after midnight in Paris France is a new day while in Montréal Québec is still “yesterday”. So you must specify the context of a zone to determine the intended date.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
For a date-only value, without time-of-day and without time zone, extract a LocalDate
.
LocalDate ld = zdt.toLocalDate() ;
To exchange date-time values with a database, use date-time objects rather than mere strings. No need for you to generate or parse that problematic string.
If your JDBC driver complies with JDBC 4.2 and later, you can directly use the java.time types rather than the legacy java.sql types.
myPreparedStatement.setObject ( 1 , ld ); // Automatic detection and conversion of data type.
…and…
LocalDate ld = myResultSet.getObject ( "my_date_column_" , LocalDate.class );
For more discussion, see Insert & fetch java.time.LocalDate objects to/from an SQL database such as H2.
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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
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.