String to Date conversion (everything seems good but fail)

元气小坏坏 提交于 2019-12-24 10:25:03

问题


How to parse this string?

"Mon Jul 02 13:49:16 CEST 2012"

String Date = "Mon Jul 02 13:11:38 CEST 2012";
DateFormat formatter;
Date convertedDate= new Date();
formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy");
try {
     convertedDate = (Date) formatter.parse(Date);
} catch (ParseException ex) {
    Logger.getLogger(ItemRecTestCases.class.getName()).log(Level.SEVERE, null, ex);
        }

Dont work..."java.text.ParseException: Unparseable date:"


回答1:


You need to set the locale :

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

Or else "Mon" cannot be parsed as "monday".




回答2:


The Locale needs to be specified:

formatter = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US);


来源:https://stackoverflow.com/questions/11293134/string-to-date-conversion-everything-seems-good-but-fail

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