Ahamed
Ahamed

Reputation: 39675

How to parse date String containing Locale

I have a date String "Sat Jan 28 00:00:00 IST 2012" and I am trying to parse it using DateTimeFormatter of Joda. I have the following code, dont know where, it went wrong.

DateTimeFormatter dateFmt = DateTimeFormat
            .forPattern("EEE MMM dd HH:mm:SS ZZZ yyyy");
DateTime dateTime = dateFmt.parseDateTime(dateString);

Exception : java.lang.IllegalArgumentException: Invalid format: "Sat Jan 28 00:00:00 IST 2012" is malformed at "IST 2012". Please help me to get thro this. Thanks for any help.

Upvotes: 0

Views: 930

Answers (3)

Ahamed
Ahamed

Reputation: 39675

I don't know why, but it is working if I use SimpleDateFormat instead of DateTimeFormatter.

CODE:

public static void main(String[] args) throws ParseException {
        String FORMAT = "EEE MMM dd HH:mm:SS zzz yyyy";
        String dateString = "Sat Jan 28 00:00:00 IST 2012";
        SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT);
        Date date = dateFormat.parse(dateString);
        System.out.println(new DateTime(date));
        DateTimeFormatter dateFmt = DateTimeFormat.forPattern(FORMAT);
//      System.out.println(dateFmt.parseLocalDateTime(dateString));
//      System.out.println(dateFmt.parseDateTime(dateString));
        System.out.println(dateFmt.parseLocalTime(dateString));
    }

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359776

Use zzz (lowercase), not ZZZ (uppercase). From the API docs:

 z   time zone              text      Pacific Standard Time; PST
 Z   time zone offset/id    zone      -0800; -08:00; America/Los_Angeles

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240860

IST is not recognized timezone by API, It can recognize only one of the timezone from getAvailableIds()

Upvotes: 2

Related Questions