java. text. Parseexception: unresolved date: “Thu Jan 19 2012 08:00 PM”
•
Java
I want to resolve an appointment My string date is "Thu Jan 19 2012 08:00 PM" The code I want to parse is:
format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa"); this.settDate(new Timestamp((format.parse(sDate)).getTime()));
But it doesn't work How do I parse this date?
The complete method is:
public void saveTask(int iDevice,String description,String sDate) throws ParseException { format = new SimpleDateFormat("EEE MMM dd yyyy hh:mm aaa"); this.setideviceid(iDevice); this.setsDescription(description); this.settDate(new Timestamp((format.parse(sDate)).getTime())); DatabaseManager.save(this); }
Exceptions:
java.text.ParseException: Unparseable date: "Thu Jan 19 2012 01:00 AM"
Debugging picture:
thank you!
Solution
Try the following code... Tested and working
String dateStr = "Thu Jan 19 2012 01:00 PM"; DateFormat readFormat = new SimpleDateFormat( "EEE MMM dd yyyy hh:mm aaa"); DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); Date date = null; try { date = readFormat.parse( dateStr ); } catch ( ParseException e ) { e.printStackTrace(); } String formattedDate = ""; if( date != null ) { formattedDate = writeFormat.format( date ); } System.out.println(formattedDate);
The output is 2012-01-19 13:00:00
Cheers! Happy to help!!!
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码