0
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TesterA
{
    public static void main(String[] args)
    {
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
        String dateInString = "7-Jun-2013";

        try
        {
            Date date = formatter.parse(dateInString);
            System.out.println(date);
            System.out.println(formatter.format(date));
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
    }
}

I was trying to run this sample code copy from a web, but it doesn't work. How should I change it?

This is the error I got

java.text.ParseException: Unparseable date: "7-Jun-2013"
    at java.text.DateFormat.parse(DateFormat.java:366)
    at TesterA.main(TesterA.java:14)
4
  • possible duplicate of How to parse a date? Commented Nov 11, 2014 at 14:10
  • The code would work fine with JDK 7. Do you run under java 6? Commented Nov 11, 2014 at 14:29
  • I run it under JDK 8, and now after I add the locale, it works fine. Commented Nov 11, 2014 at 14:42
  • possible duplicate of java.text.ParseException: Unparseable date Commented Nov 11, 2014 at 17:13

2 Answers 2

1

I think it is a problem with your locale.

Try:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
Sign up to request clarification or add additional context in comments.

1 Comment

@petersze I have run your code and it works on my computer with locale german. If i change to france for instance I get the same error as you
0

java.text.ParseException means you provided a string which cannot be parsed using the current settings.

Just set the Locale.

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);

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.