2
votes

I have written a func to convert Date String (Thu, 1 Mar 2012 13:57:06 -0600) to Date variable. I'm relying on SimpleDateFormat for the conversion. The conversion happens, but the resultant Date is "Thu Sep 01 11:00:06 GMT-08:00 2016" which I'm not able to understand. I tried different options for the Format string still invain. Any help appreciated. Thank you.

The options I have tried for the Format String are: 1) E, dd MMM yyyy HH:MM:ss Z 2) EEE, dd MMM yyyy HH:MM:ss ZZZZ

private Date Convert_To_Date() {

        Date dt = null;

        String str = "Thu, 1 Mar 2012 13:57:06 -0600";
        SimpleDateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:MM:ss Z");

        try {
            dt = formatter.parse(str);
        }
        catch ( ParseException pe) {
            System.out.println(pe.getMessage());
        }

        //String strDt = dt.toString(); 
        System.out.println(dt);

        return dt;
    }
3

3 Answers

4
votes

I think the problem was just that you were using "M" for minutes instead of "m". This works fine for me:

import java.text.*;
import java.util.*;

public class Test {
    public static void main(String[] args) throws Exception {
        String text = "Thu, 1 Mar 2012 13:57:06 -0600";
        // Note the explicit locale. Let's not leave it as the system default :)
        DateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", 
                                                    Locale.US);

        Date dt = formatter.parse(text);
        System.out.println(dt);
    }
}

(As noted by sgmorrison, you can use d instead of dd too, and that would be a more accurate description of your format - but dd works for parsing in this case.)

2
votes

A couple of small bugs in your date format string:

Your pattern for matching minutes in the hour should be "mm", not "MM" (which matches month of the year). See SimpleDateFormat for details.

Also, your pattern for matching days in the month should be "d" instead of "dd", as "the number of pattern letters is the minimum number of digits, and shorter numbers are zero-padded to this amount. For parsing, the number of pattern letters is ignored unless it's needed to separate two adjacent fields."

0
votes

tl;dr

ZonedDateTime.parse( 
    "Thu, 1 Mar 2012 13:57:06 -0600" ,    // Input complies with RFC 1123 & RFC 822.
    DateTimeFormatter.RFC_1123_DATE_TIME  // Hard-coded to English locale.
).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.