tl;dr
ZonedDateTime.parse(
"Thu, 1 Mar 2012 13:57:06 -0600" ,
DateTimeFormatter.RFC_1123_DATE_TIME
).toString()
2012-03-01T13:57:06-06:00
java.time
The modern approach uses the java.time classes that supplant the troublesome old legacy date-time classes.
RFC 1123 / RFC 822
Your input string happens to comply with RFC 1123 / RFC 822. The java.time.DateTimeFormatter
class defines a constant for that format, DateTimeFormatter.RFC_1123_DATE_TIME
.
String input = "Thu, 1 Mar 2012 13:57:06 -0600" ;
DateTimeFormatter f = DateTimeFormatter.RFC_1123_DATE_TIME ;
ZonedDateTime zdt = ZonedDateTime.parse( input , f ) ;
2012-03-01T13:57:06-06:00
Those RFCs are old. The format is outmoded now by the ISO 8601 standard. The old format is terrible: assuming English, tolerating a wide-variety of content, and is difficult to parse.
Most every modern protocol and standard uses ISO 8601 format for exchanging date-time values as text. The formats defined by this standard are practical, unambiguous even across cultures, easy to parse by machine, and easy to read by humans for debugging/logging.
The java.time classes use ISO 8601 formats by default when parsing/generating strings, as seen in examples above.
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.
With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings or 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.