0

I am getting date in this format "Sat May 02 2015 00:00:00 GMT+0500 (Pakistan Standard Time)"

When i parse this using format

SimpleDateFormat sdf = new SimpleDateFormat(EEE MMM dd yyyy HH:mm:ss zZ (zzzz))

i get unparsable date exception which obviously means my format is wrong. Can some tell me what could be the right format for this . What to write for "(Pakistan Standard Time)" .

Syed Muhammad Oan
  • 687
  • 2
  • 15
  • 39

1 Answers1

0

This is one way to parse your date:

public class Test {

    static final String sample_date = "Sat May 02 2015 00:00:00 GMT+0500 (Pakistan Standard Time)".replace("GMT", "")
            .replace(" (Pakistan Standard Time)", "");

    public static void main(String[] args) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z");
        ParsePosition position = new ParsePosition(0);
        Date d = sdf.parse(sample_date, position);
        System.out.println(d);
        System.out.println(position);
        if (position.getErrorIndex() != -1) {
            System.out.println(sample_date.substring(position.getErrorIndex()));
        }
        System.out.println(sdf.parse(sample_date));
    }
}

Reference: Java - unparseable date, need format to match "GMT-0400"

Community
  • 1
  • 1
user1933888
  • 2,897
  • 3
  • 27
  • 36
  • I already got the right answer in the comments but you're answer is the only one i put accept so i accpt this @Sanjeev and unknown had also given the right answer – Syed Muhammad Oan May 17 '16 at 12:23