I'm trying to parse a date string I got out of a website in Java using the SimpleDateFormat class, but something goes wrong and I can't figure out why.
The date strings come in the following syntax:
"13:37 - Tue 28-Jun-2011"
So I tried doing the following:
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm - EEE dd-MMM-yyyy");
ParsePosition pos = new ParsePosition(0);
Date d = dateFormat.parse("13:37 - Tue 28-Jun-2011", pos);
As I said before, this doesn't work; when I print
System.out.println(pos.getErrorIndex());
it prints "8", which I assume means that the error is somewhere around the EEE part. I've tried different permutations but nothing worked. What am I doing wrong?
Thanks
bompf
If your trying to parse the date this will work. I dont know what you are trying to do with ParsePosition
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm - EEE dd-MMM-yyyy");
Date d = dateFormat.parse("13:37 - Tue 28-Jun-2011");
System.out.println(d);
It works fine for me...
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm - EEE dd-MMM-yyyy");
ParsePosition pos = new ParsePosition(0);
Date d = dateFormat.parse("13:37 - Tue 28-Jun-2011", pos);
System.out.println(pos.getErrorIndex());
System.out.println(d);
Output -
-1
Tue Jun 28 13:37:00 EDT 2011
I found the problem: I did not know I have to set a locale for the date format..
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm - EEE dd-MMM-yyyy", Locale.ENGLISH);
This works now!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.