问题
I have a SimpleDateFormat like this :
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
and trying to parse such 2012-Jul-29 17:14:39
but I`m getting
java.text.ParseException: Unparseable date: "2012-Jul-29 17:14:39" at java.text.DateFormat.parse(Unknown Source) at com.sysplan.visixd.blastgauge.BGParser.main(Parser.java:396)
Any idea why ?
回答1:
It appears to be a locale problem, I tried this without any error
new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss").parse("2012-Jul-29 17:14:39");
However this failed:
new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss", Locale.TAIWAN)
.parse("2012-Jul-29 17:14:39");
So it appears to be a locale problem, you need to specify your locale to ENGLISH
new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss", Locale.ENGLISH)
.parse("2012-Jul-29 17:14:39");
That is:
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
"yyyy-MMM-dd HH:mm:ss", Locale.ENGLISH);
回答2:
Try this
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss", Locale.ENGLISH);
回答3:
try this
new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss", Locale.ENGLISH);
回答4:
You can use next code:
import java.text.DateFormat;
import java.util.Calendar;
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar now = Calendar.getInstance();
DateFormat formateadorFechaMedia = DateFormat.getDateInstance(DateFormat.MEDIUM);
System.out.println(formateadorFechaMedia.format(now.getTime()));
}
}
来源:https://stackoverflow.com/questions/16606397/can-not-parse-string-with-simple-date-format