convert String “30-MAR-07” to date object

假装没事ソ 提交于 2019-12-13 03:55:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!