0

I have string with date in that format:

Oct 28, 2015, 05.15PM IST

So I want to parse it to Date object using SimpleDateFormat:

String date = "Oct 28, 2015, 05.15PM IST";
SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy, hh.mmaa zzz", Locale.US);
Date myDate = format.parse(date);

But I get exception:

java.text.ParseException: Unparseable date: "Oct 28, 2015, 05.15PM IST" (at offset 22)

What am I doing wrong?

udenfox
  • 1,594
  • 1
  • 15
  • 25
  • 2
    From the given code, It works [here](http://ideone.com/m9A6LL) – sam Oct 28 '15 at 12:05
  • @sam I'm confused. In my code it still trows an exception. – udenfox Oct 28 '15 at 12:08
  • 1
    Check [Java: unparseable date exception](http://stackoverflow.com/questions/2009207/java-unparseable-date-exception). It is a similar question – sam Oct 28 '15 at 12:09
  • @sam Okay, seems like I don't have "IST" id. But how can I deal with it so? – udenfox Oct 28 '15 at 12:19
  • 1
    This is caused by ambiguity in the timezone 'IST'. See [this question](http://stackoverflow.com/questions/11264697/java-timezone-strange-behavior-with-ist). – Adriaan Koster Oct 28 '15 at 12:19

2 Answers2

0

From my understanding of the SimpleDateFormatwhen specifying the AM/PM component, you need not use two a's nor three z's. You are trying to parse "Oct 28, 2015, 05.15PM IST" with more than one AM/PM specifier, as well as more than one timezone specifier. Change your format Object to SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy, hh.mma z", Locale.US);

MrPublic
  • 520
  • 5
  • 16
0

The problem is that SimpleDateFormat can't parse "IST" as timezone because it ambiguous.

I solved that problem according to Adriaan Koster's answer. But actually it worked for me only after I cutted " IST" from my date string. So complete solution for the problem looks like that:

String date = "Oct 28, 2015, 05.15PM IST";
date = date.substring(0, date.length()-4);

SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy, hh.mma", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
Date myDate = format.parse(date);
Community
  • 1
  • 1
udenfox
  • 1,594
  • 1
  • 15
  • 25