Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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);                      

I get this exception from the first line:

java.lang.IllegalArgumentException: Illegal pattern character 'T'

like image 487
Haris Dautović Avatar asked Mar 15 '13 13:03

Haris Dautović


People also ask

How do you parse a string to a date?

parse() The Date. parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31). Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.

How do I convert a string from one date to another?

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale. ENGLISH); String dateInString = "7-Jun-2013"; Date date = formatter. parse(dateInString); In the above example, we first need to construct a SimpleDateFormat object by passing the pattern describing the date and time format.

What is SSSZ in date format?

Dates are formatted using the following format: "yyyy-MM-dd'T'hh:mm:ss'Z'" if in UTC or "yyyy-MM-dd'T'hh:mm:ss[+|-]hh:mm" otherwise. On the contrary to the time zone, by default the number of milliseconds is not displayed. However, when displayed, the format is: "yyyy-MM-dd'T'hh:mm:ss.


1 Answers

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 
like image 53
Nermeen Avatar answered Sep 21 '22 17:09

Nermeen