This question already has an answer here:
How do I can parse this date 2018-01-09T11:11:02.0+03:00
to dd.MM.yyyy hh:mm
format in Android?
And what does T
between 09
and 11
mean?
Thanks.
I don't know how the back-end developer got this format. I am using Java.
You can do this with SimpleDateFormat.
Here is a tested example in Java:
Here is a tested example in Kotlin:
If you are using java, you can use
SimpeDateFormat
with patterns:The output is :
EDIT : Thanks to @OleV.V., for API > 26, or using ThreeTenABP we can use
DateTimeFormatter, we can do something like that
This prints:
Please note: I am using uppercase
HH
in the format pattern string. This indicates hour of day from 00 through 23. In the question you used lowercasehh
, which in a format pattern string means hour with AM or PM from 01 through 12, so 00:33 would come out as 12:33 and 15:47 as 03:47. I didn’t think you intended this.The format that your backend developer got,
2018-01-09T11:11:02.0+03:00
, is ISO 8601. It’s widespread, and it’s the international standard, so it’s good that s/he got that. The funnyT
in the middle indicates the start of the time part to separate it from the date part. The one-argOffsetDateTime.parse
method parses ISO 8601, which is why we didn’t need any formatter for parsing.OffsetDateTime
andDateTimeFormatter
are classes fromjava.time
, the modern Java date and time API.Question: Can I use java.time on Android?
Yes,
java.time
works nicely on older and newer Android devices. It just requires at least Java 6.org.threeten.bp
with subpackages (my code was tested with these imports).Links
java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).