How parse 2013-03-13T20:59:31+0000 date string to Date

我们两清 提交于 2019-12-18 02:39:29

问题


How to parse this format date string 2013-03-13T20:59:31+0000 to Date object?

I'm trying on this way but it doesn't work.

DateFormat df = new SimpleDateFormat("YYYY-MM-DDThh:mm:ssTZD");
Date result =  df.parse(time);

回答1:


DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");

Year is lower case y. Any characters that are in the input which are not related to the date (like the 'T' in 2013-03-13T20:59:31+0000 should be quoted in ''.

For a list of the defined pattern letters see the documentation

Parse checks that the given date is in the format you specified. To print the date in a specific format after checking see below:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
Date result;
try {
    result = df.parse("2013-03-13T20:59:31+0000");
    System.out.println("date:"+result); //prints date in current locale
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    System.out.println(sdf.format(result)); //prints date in the format sdf
}



回答2:


Try:

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

check http://developer.android.com/reference/java/text/SimpleDateFormat.html

in specific:

                 yyyy-MM-dd 1969-12-31
                 yyyy-MM-dd 1970-01-01
           yyyy-MM-dd HH:mm 1969-12-31 16:00
           yyyy-MM-dd HH:mm 1970-01-01 00:00
          yyyy-MM-dd HH:mmZ 1969-12-31 16:00-0800
          yyyy-MM-dd HH:mmZ 1970-01-01 00:00+0000
   yyyy-MM-dd HH:mm:ss.SSSZ 1969-12-31 16:00:00.000-0800
   yyyy-MM-dd HH:mm:ss.SSSZ 1970-01-01 00:00:00.000+0000
 yyyy-MM-dd'T'HH:mm:ss.SSSZ 1969-12-31T16:00:00.000-0800
 yyyy-MM-dd'T'HH:mm:ss.SSSZ 1970-01-01T00:00:00.000+0000



回答3:


Please try this:

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



回答4:


Thanks to all people who tried to help me ... (+1) for all .......

This works: "yyyy-MM-dd'T'HH:mm:ss"




回答5:


For 2017-02-08 06:23:35 +0000 this kind of Date format, i use below format:

SimpleDateFormat formatDate;
formatDate= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ZZZ");

Its working for me. Other answers not working for me.



来源:https://stackoverflow.com/questions/15433377/how-parse-2013-03-13t2059310000-date-string-to-date

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