Convert from String to Date throws Unparseable date exception [duplicate]

折月煮酒 提交于 2019-12-20 02:52:45

问题


I want to convert a Date to a String but I have some problems. My code is this:

SimpleDateFormat formato = new SimpleDateFormat(
            "EEE MMM dd HH:mm:ss z yyyy");

    String hacer = "Fri Nov 01 10:30:02 PDT 2013";
    Date test = null;
    test = formato.parse( hacer);
    System.out.println("prueba===>" + test);

But nothing something is wrong eclipse shows me this error:

Unparseable date: "Fri Nov 01 10:30:02 PDT 2013"
at java.text.DateFormat.parse(Unknown Source)

some help?


回答1:


Probably your default locale doesn't support English months in MMM. For example in Poland MMM supports "styczeń" but not "Jan" or "January"

To change this In SimpleDateFormat you need to set locale which supports months written in English, for example

new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);


来源:https://stackoverflow.com/questions/19731752/convert-from-string-to-date-throws-unparseable-date-exception

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