问题
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy");
dt = formatter.parse(temp[0]);
gives me the error
java.text.ParseException: Unparseable date: "30-MAR-07"
Is there any way that I can format the given String to a Date object without having to write my own string splitting and translating-to-months-in-numerals methods? Thanks
回答1:
Use correct format, as follows,
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
回答2:
Your SimpleDateFormat
pattern is completely incorrect for "30-MAR-07". It's got the values in the wrong order, it uses the wrong separator, and the wrong month format. You want:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
You may also want to specify the locale, e.g.
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy", Locale.US);
I've checked that this works - at least on my machine :)
回答3:
Try this... (new SimpleDateFormat("dd-MMM-yy")
System.out.println(new SimpleDateFormat("dd-MMM-yy").format(new Date("30-MAR-07")));
来源:https://stackoverflow.com/questions/11527451/convert-string-30-mar-07-to-date-object