使用时,SimpleDateFormat解析不返回任何内容

时间:2015-08-19 06:55:35

标签: java android datetime

尝试将SimpleDateFormat.parse字符串转换为日期,但{I}添加时间后,String dateInString = news.getDate(); Log.e(TAG, "Date in String: " + dateInString); Date in String: 2015-08-19T06:21:59+01:00 //Result 不返回任何内容。

这是日期字符串:

DateFormat format
            = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);        
Date date;
    try {

        date = format.parse(dateInString);
        Log.e(TAG, "Formatted date: " + date);


    } catch (ParseException e) {
        e.printStackTrace();
    }

当我这样做时:

Formatted date: Wed Aug 19 01:00:00 GMT+01:00 2015

我得到了结果:DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);

如果我将时间添加到SimpleDateFormat:

TimeZone

代码甚至没有通过尝试,也没有抛出任何异常。没有任何东西被打印出来。

我尝试添加TimeZone timeZone = TimeZone.getTimeZone("UTC+01"); //also tried GMT+1:00, UTC+1:00 and UTC DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); format.setTimeZone(timeZone); try { date = format.parse(dateInString); Log.e(TAG, "Formatted date: " + date); } catch (ParseException e) { e.printStackTrace(); } 但没有运气:

------------------------------------------------------------------------
| id |                        buying_history                           |
------------------------------------------------------------------------
| 1 | {'clothes':{'shoe':{'bob':1, 'jack':2}, 'pants':{'bob':2}}}      |
| 2 | {'clothes':{'shoe':{'mery':1, 'bob':2},'pants':{'bob':2, 'a':3}}}|
|   |   .....                                                          |
------------------------------------------------------------------------

任何指针?

3 个答案:

答案 0 :(得分:1)

SimpleDataFormat的模式sed不正确,使用这个:

 DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ", Locale.ENGLISH);

请注意,模式略有不同,我添加了一个分隔日期和时间的T文本,还添加了ZZZZZ,表示那里有时区。

答案 1 :(得分:0)

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX",Locale.ENGLISH);

' X' flag自1.7起可用,它代表Timezone。

此处描述了所有可用的标志 http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

答案 2 :(得分:0)

我想有几种方法可以做到这一点。除了阿图罗的答案,这也有效:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.English);