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)
peter sze
  • 3
  • 2

2 Answers2

1

I think it is a problem with your locale.

Try:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
Jens
  • 60,806
  • 15
  • 81
  • 95
  • @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 – Jens Nov 11 '14 at 14:29
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);
sponge
  • 7,923
  • 13
  • 43
  • 77