-9

How can I parse this timestamp

1594361788215

to data in String type in this format 2020-10-26T15:21:47.758+01:00

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
merc-angel
  • 394
  • 1
  • 5
  • 13
  • 1
    Is your timestamp a number of milliseconds? – Abra Oct 26 '20 at 15:06
  • 1
    Did you try anything yourself? If not, do it first and then come back when you encounter some specific problems. – Amongalen Oct 26 '20 at 15:07
  • Does this answer your question? [convert timestamp to ISO860 in java,](https://stackoverflow.com/questions/12326976/convert-timestamp-to-iso860-in-java) – dpr Oct 26 '20 at 15:09
  • 2
    `OffsetDateTime.ofInstant(Instant.ofEpochMilli(1594361788215L), ZoneOffset.ofHours(1)).toString()` – Oleg Oct 26 '20 at 15:15

1 Answers1

2

You can easily format an epoch timestamp to a string with the library java.time.

First, you'll have to convert the epoch timestamp you have into a ZonedDateTime:

ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(
    Instant.ofEpochMilli(1594361788215L), 
    ZoneId.of("Europe/London"));

And the format you described corresponds to ISO_OFFSET_DATE_TIME

String formatted = zonedDateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
Enrico Trentin
  • 272
  • 4
  • 7