0

I have a 14-digit timestamp (Format: "YYYYMMDDhhmmss") which i want to convert to Instant object.

What is correct way to do it?

Something like:

import java.time.Instant

val t1 = "20010531021254"

Instant.parse(t1) //Doesn't work
//What i'de like to receive:
res0: java.time.Instant = 2001-05-31T02:12:54.000
Aaron_ab
  • 3,450
  • 3
  • 28
  • 42

1 Answers1

2

In order for Instant.parse() to work, the string has to be in an acceptable format.

import java.time.{Instant, LocalDateTime}
import java.time.format.DateTimeFormatter

val t1 = "20010531021254"
Instant.parse(LocalDateTime.parse(t1, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
                           .format(DateTimeFormatter.ISO_DATE_TIME) + "Z")
// or                      .toString + "Z")

//res0: java.time.Instant = 2001-05-31T02:12:54Z

There are likely better ways to get this done, but this is how I got it to work.

jwvh
  • 50,871
  • 7
  • 38
  • 64