Search code examples
javadate-formatsimpledateformatparseexception

String to date conversion having exception in parsing


Hi I need to convert string to date below is my code,

String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println("Date././."+date);

If i run my code am getting parse exception as like below,

java.text.ParseException: Unparseable date: "January 2,2010"


Solution

  • Your format needs to match the date you're trying to parse. E.g., for your usecase:

    String string = "January 2, 2010";
    DateFormat format = new SimpleDateFormat("MMMMM dd, yyyy", Locale.ENGLISH);