Can not parse String with Simple Date format

给你一囗甜甜゛ 提交于 2019-12-13 01:07:42

问题


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

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