tl;dr
ZonedDateTime.parse (
"Mon Apr 03 16:49:56 PDT 2017" ,
DateTimeFormatter.ofPattern ( "EEE MMM dd HH:mm:ss z uuuu", Locale.US )
)
Syntax
Sorry, I don’t know Groovy syntax, so this is in Java syntax.
java.util.Date::toString
Are you saying that values were saved to your database as a textual data type by calling java.util.Date::toString
?
So now you need to parse the strings in the default format used by java.util.Date::toString
such as Mon Apr 03 16:49:56 PDT 2017
to get back to a date-time object.
Firstly, you must already know that was a dreadful way to store a date-time value. That format assumes English, and lops off any fractional second. That format is difficult to read by humans, and difficult to parse by machines. It uses the 3-4 letter abbreviations of pseudo time zones like PDT
or IST
which are not true time zones, are not standardized, and are not even unique(!). Proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
.
ISO 8601
Instead, when serializing date-time values, use standard ISO 8601 formats.
The java.time classes use standard ISO 8601 formats by default when parsing and generating strings.
Avoid legacy date-time classes
Know that java.util.Date
is one of the troublesome poorly-designed confusing old date-time classes bundled with the earliest versions of Java. Those classes are now legacy, supplanted by the java.time classes. Avoid the legacy classes, use java.time instead.
Using java.time
The DateTimeFormatter
class parses strings. We must define a formatting pattern to match the output of Date::toString
.
String input = "Mon Apr 03 16:49:56 PDT 2017" ;
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "EEE MMM dd HH:mm:ss z uuuu", Locale.US ) ;
Now we can parse that string as a ZonedDateTime
.
ZonedDateTime zdt = ZonedDateTime.parse ( input, f ) ;
zdt.toString(): 2017-04-03T16:49:56-07:00[America/Los_Angeles]
Generally the best practice for serializing date-time values for data storage and exchange is to keep the value in UTC and then make a string in standard ISO 8601 format. The convention for UTC in ISO 8601 is to append a Z
, short for Zulu
and meaning UTC.
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 = zdt.toInstant() ;
String output = instant.toString() ;
instant.toString(): 2017-04-03T23:49:56Z
JDBC
Your Question and Comments are not clear. If your values are stored in a database using proper date-time types, then there is no need to mess about with strings as discussed above. If properly stored, you should be letting your JDBC driver do the work of fetching data in and out of the database while converting as necessary.
Drivers compliant with JDBC 4.2 and later can directly use the java.time types via PreparedStatement::setObject
and ResultSet::getObject
.
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.