字符串到日期的转换在解析时出现异常

String to date conversion having exception in parsing

嗨,我需要将字符串转换为日期,下面是我的代码,

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

如果我 运行 我的代码出现解析异常,如下所示,

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

您的格式需要与您尝试解析的日期相匹配。例如,对于您的用例:

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