问题
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