How to parse a date string using DateFormat?

I have a date string in the following format:

Thu Oct 20 14:39:19 PST 2011

I would like to parse it using a DateFormat to get a Date object. I try it like this:

 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT); Date date = df.parse(dateString); 

This gives a ParseException ("unique date").

I also tried this:

 SimpleDateFormat df = new SimpleDateFormat("EEE-MMM-dd HH:mm:ss z yyyy"); 

with the same results.

Is this the correct SimpleDateFormat line? Is there a better way to analyze this date?

+4
source share
3 answers

The problem was that I was trying to parse the English date in French.

This was resolved using instead:

SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.CANADA);

+2
source

Are you sure that the date string is really set to Thu Oct 20 14:39:19 PST 2011 ? If this is not a problem, you can try using this code that works for me:

 import java.text.SimpleDateFormat; import java.util.Date; public class Test{ public static void main(String args[]){ String toParse = "Thu Oct 20 14:39:19 PST 2011"; String format = "EEE MMM dd HH:mm:ss z yyyy"; SimpleDateFormat formater = new SimpleDateFormat(format); try{ Date parsed = formater.parse(toParse); } catch(Exception e){ System.out.println(e.getMessage()); } } } 
+9
source

Try the following:

 SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); 
0
source

All Articles