开发者

Parse A Java Date

How can I parse the开发者_JS百科 following date in Java from its string representation to a java.util.Date?

2011-07-12T16:45:56

I tried the following:

private Date getDateTime(String aDateString) {
    Date result = new java.util.Date();
    SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
    try
    {
        result = sf.parse(aDateString);
    }
    catch (Exception ex)
    {
        Log.e(this.getClass().getName(), "Unable to parse date: " + aDateString);
    }
    return result;
}


You are not using the right date format pattern. The year/month/day separators are clearly wrong, and you need a literal 'T'. Try this:

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

For the record, this is an ISO 8601 combined date and time format.


Your date format pattern is incorrect. It should be yyyy-MM-dd'T'HH:mm:ss .

To parse a date like "2011-07-12T16:45:56" you need to use:-

SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");


Check the pattern you are feeding your SimpleDateFormat against the string you are feeding in.

See any potential discrepancies? I see 3.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜