1

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

3
  • 1
    When you say it doesn't work, what error does it show? Commented Jun 30, 2011 at 17:08
  • Could you actually please post that locale-thing as a solution to this question and then accept your own answer? It'd help the rest of us. Commented Nov 6, 2012 at 13:11
  • I have done as you asked. Commented Jan 9, 2013 at 18:03

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.