Your error
Your string gives us a date (2019-02-15) and a time of day (00:00:00). It doesn’t give us any time zone or UTC offset. Without either of the latter we cannot know a unique point in time, which is required for converting to milliseconds since the epoch. If you know the time zone implied in your date-time string you may convert like the following. I am sorry that I can write only Java code.
ZoneId zone = ZoneId.of("America/St_Barthelemy");
String dateTimeText = "2019-02-15T00:00:00";
LocalDateTime dateTime = LocalDateTime.parse(dateTimeText);
long millisecondsSinceUnixEpoch = dateTime.atZone(zone)
.toInstant()
.toEpochMilli();
System.out.println("EPOCH " + millisecondsSinceUnixEpoch);
Now output is:
EPOCH 1550203200000
Substitute your time zone if it didn’t happen to be America/St_Barthelemy. A LocalDateTime
is a date and time witout tme zone or offset, so this class parses your string without error. Next we use the known time zone for converting.
Your exception message said:
Text '2019-02-15T00:00:00' could not be parsed at index 19
Index 19 is the end of your string, so the error message says that something was missing there. Assuming that you were using Instant.parse()
(your question doesn’t tell), @Lenin is correct in the other answer that Instant.parse()
expects that the parsed string ends in Z
for “Zulu time zone” or UTC or offset zero.
… it requires API 26
The good news is that it does not require API level 26. Instant
and the other classes we’ve been using belong to java.time, the modern Java date and time API. This API has been backported.
- In Java 8 and later and on newer Android devices (from API level 26, or Android 8.0 and later) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On Android earlier than API level 26 (Android 8.0) use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links