Error using SimpleDateFormat

你离开我真会死。 提交于 2019-12-23 16:19:48

问题


I'm trying to use the SimpleDateFormat class to parse a DateTime out of this string:

05-Jul-2012 11:38:02,442 UTC AM

I tried the following format string:

SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS z a");

Date temp = dateformatYYYYMMDD.parse(time);

But it generates the error:

Error: Unparseable date: ""

If I use zZ I get : Error: Unparseable date: "05-Jul-2012 11:38:02,442 UTC AM"

Any hints on how to get around this?


回答1:


That works fine for me when I try to run:

String time = "05-Jul-2012 11:38:02,442 UTC AM";
SimpleDateFormat dateformatYYYYMMDD = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS z a");
Date temp = dateformatYYYYMMDD.parse(time);

Are you sure time is set to something?




回答2:


The code you've given works fine for me. Perhaps the problem is your locale? Try specifying it when you create the SimpleDateFormat:

SimpleDateFormat format =new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS z a",
                                              Locale.US);

That way it will try to find US month and am/pm designator names.




回答3:


Try this...

System.out.println(new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss,SSS z a",
                                              Locale.US).format(new Date()));



回答4:


I'm not able to reproduce your error. Are you sure you are using:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


来源:https://stackoverflow.com/questions/11507845/error-using-simpledateformat

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