tl;dr
where can I find an open-source code for converting time strings into "Date" objects
Built into Java in the java.time classes.
LocalDate.parse( "2017-01-23" )
LocalTime.parse( "14:56:00" )
LocalDateTime.parse( "2017-01-23T07:56:00" )
OffsetDateTime.parse( "2017-01-23T07:56:00-07:00" )
ZonedDateTime.parse( "2017-01-23T07:56:00-07:00[America/Los_Angeles]" )
Using java.time
You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.
ZoneId
The java.util.TimeZone
class is replaced by java.time.ZoneId
and java.time.ZoneOffset
. A time zone is a history of changes to a particular region’s offset-from-UTC.
There are many poor design decisions in the legacy java.time classes. Using the 3-4 character pseudo time zone codes is one such poor choice. Never use the 3-4 letter abbreviation such as CDT
or EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
Instead, you should specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
.
ISO 8601
Avoid using badly designed awkward formats such as "10:16:12.523 CDT Thu Mar 21 2013" which are hard to read by humans, difficult to parse by machine, rely on English needlessly, and make use of ambiguous (worse than useless!) pseudo time zones. Instead use the standard ISO 8601 formats. The java.time classes use these formats by default. The ZonedDateTime
class extends the standard format by appending the continent/region time zone name in square brackets.
Examples:
- 2017-01-23
- 14:56:00
- 2017-01-23T14:56:00Z
- 2017-01-23T07:56:00-07:00[America/Los_Angeles]
For formats other than ISO 8601 use the DateTimeFormatter
class. Already covered many hundreds of times already, so search Stack Overflow for more info and 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.