I will give you the good solution and the hack.
java.time
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ssX");
OffsetDateTime dateTime = OffsetDateTime.parse(dateTimeString, formatter);
System.out.println(dateTime);
This prints
2017-12-01T00:00+01:00
This is the good solution: java.time, the modern Java date and time API. There are at least two reasons why you would want to do this on Android.
- The
SimpleDateFormat class that you have been using is not only long outdated, it is also notoriously troublesome, so I would recommend you don’t use it anyway, and the modern API is so much nicer to work with.
- Your time zone offset of
+01 is an ISO 8601 offset. While only later vertsions of SimpleDateFormat can parse this, java.time has excelled at the ISO 8601 standard from when it came out, so this is a safe bet.
I should say you have a nice opportunity for going future-proof.
Your time of day of 00:00:00 seems to suggest you’re only interested in the date? If so, java.time has one more good offer for you, the LocalDate class represents a date without time of day:
LocalDate date = dateTime.toLocalDate();
This gives a LocalDate of 2017-12-01.
The hack
You may just append minutes of offset of 00 to your string, and the formatter in your question will parse it:
dateTimeString += "00";
Question: how do I use java.time on older Android?
If java.time is not built-in on your Android device, you get ThreeTenABP and add it to your project, and then import org.threeten.bp.OffsetDateTime, org.threeten.bp.LocalDate and org.threeten.bp.format.DateTimeFormatter. See the links below. The code is unchanged from above.
Links