-1

I'm parsing my ISO8601 string format to milliseconds, so far I'm having an error

also is there a better way because it requires API 26

Text '2019-02-15T00:00:00' could not be parsed at index 19

        val millisecondsSinceUnixEpoch = instant.toEpochMilli()
        println("EPOCH $millisecondsSinceUnixEpoch")
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Welcome to Stack Overflow. I think I understand the question, but it would be clearer if you could please add [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) in the question (use the edit link under the line of tags). – Ole V.V. Sep 10 '19 at 07:55

2 Answers2

1

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

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

You have to add the format at the end. Try '2019-02-15T00:00:00Z'. The Z means Zulu time which is +0 Offset

Lenin
  • 500
  • 3
  • 11