Home > Back-end >  How to convert date in string to Long in java?
How to convert date in string to Long in java?

Time:02-06

I have date time in string format in one of my model. "startTime": "2022-10-19T14:31:22 00:00"

How can i convert it to Long format in java

I tried using Long.valueOf(startTime) and Long.parseLong(startTime)

but in both i am getting Exception

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

CodePudding user response:

You can parse your datetime format using builtin methods - also to convert it to a long value (assuming the number of seconds since the epoch).

Instant.parse("2022-10-19T14:31:22 00:00").getEpochSecond()

See https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/time/Instant.html

See code run at Ideone.com.

  • Related