I am getting date as string
in this format 2018-05-04T15:49:27+00:00
but cannot seem to be able to parse it with SimpleDateFormat
. The T
between year and time causing exception, I red about SimpleDateFormat
class from Java docs but there is no mention of T
. Now I am
replacing the T
with space and then parsing it but I wonder if this is the right solution. Just for the record T
is not Tuesday it's always T
no matter what date is.
Asked
Active
Viewed 1,551 times
0

Abdurakhmon
- 2,813
- 5
- 20
- 41
-
1No, replacing it is the wrong solution, you simply need to use the proper date time format for parsing, including a `'T'`. – luk2302 May 25 '18 at 11:12
-
1I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Its one-arg `OffsetDateTime.parse` method will even parse your string without any explicit formatter. – Ole V.V. May 25 '18 at 11:27
-
1Your format is ISO 8601. It uses `T` to denote the beginning of the time part, to separate it from the date part. – Ole V.V. May 25 '18 at 11:31
-
Search Stack Overflow thoroughly before posting. Tip: Look for `OffsetDateTime` class. `OffsetDateTime.parse( "2018-05-04T15:49:27+00:00" )` Never use the troublesome old legacy classes seen in both Answers on this page. – Basil Bourque May 26 '18 at 02:52
2 Answers
1
You can find everything you need for SimpleDateFormat
here.
T
is just plain text and therefore has no special meaning for SimpleDateFormat
. You should put it into quotes then the plaintext is read "as it is"
If you use the pattern yyyy-MM-dd'T'HH:mm:ss ...
instead of just yyyy-MM-ddTHH:mm:ss ...
you should be fine.

ParkerHalo
- 4,341
- 9
- 29
- 51
0
You can use the below formater to parse the string 2018-05-04T15:49:27+00:00
to time.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

sauumum
- 1,638
- 1
- 19
- 36
-
FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 26 '18 at 02:51