Nathan Smith
Nathan Smith

Reputation: 8347

Joda Time - String to DateTime conversion

I require some help converting the following Thu, 13 Feb 2014 16:43:58 +0000 string to type DateTime. I have a stream of tweets being stored in an ElasticSearch cluster, currently the timestamp of each tweet is mapped as a string. I wish to parse these to type DateTime.

I tried EEE, dd MMM yyyy HH:mm:ss ZZZZZ but it failed. Any help would be great.

Thanks.

Upvotes: 2

Views: 1220

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503290

You only want a single Z to represent "offset without a colon".

Also note that you should ensure that your DateTimeFormatter is using English month/day names.

For example:

import java.util.*;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

class Test {
    public static void main(String[] args) throws Exception {
        DateTimeFormatter format =
            DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z")
                .withLocale(Locale.US);
        String text = "Thu, 13 Feb 2014 16:43:58 +0000";
        System.out.println(format.parseDateTime(text));
    }
}

Upvotes: 6

Related Questions