This question solves the case for seconds: How to convert a date time string to long (UNIX Epoch Time) in Java 8 (Scala)
But if I want milliseconds it seems I have to use
def dateTimeStringToEpoch(s: String, pattern: String): Long =
LocalDateTime.parse(s, DateTimeFormatter.ofPattern(pattern))
.atZone(ZoneId.ofOffset("UTC", ZoneOffset.ofHours(0)))
.toInstant().toEpochMilli
which is ugly for the 4 issues I detail in the other question (main things I don't like is the magic literal "UTC"
and the magic number 0
).
Unfortunately the following does not compile
def dateTimeStringToEpoch(s: String, pattern: String): Long =
LocalDateTime.parse(s, DateTimeFormatter.ofPattern(pattern))
.toEpochMilliSecond(ZoneOffset.UTC)
as toEpochMilliSecond
does not exist