How can I parse a Unix timestamp to a date string in Kotlin?
For example 1532358895
to 2018-07-23T15:14:55Z
Example 2: Convert String to Date using pattern formatters So, we create a formatter of the given pattern. Check all DateTimeFormatter patterns, if you're interested. Now, we can parse the date using LocalDate. parse() function and get the LocalDate object.
You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.
The following should work. It's just using the Java libraries for handling this:
val sdf = java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
val date = java.util.Date(1532358895 * 1000)
sdf.format(date)
Or with the new Time API:
java.time.format.DateTimeFormatter.ISO_INSTANT
.format(java.time.Instant.ofEpochSecond(1532358895))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With