My question is simple
If I do:
var start = System.currentTimeMillis
I get:
start: Long = 1542717303659
How should I do to get a string looking to something readable for a human eye?:
ex: "20/11/2018 13:30:10"
You can use the java.time
library like:
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")
formatter.format(LocalDateTime.now)
If you only have the timestamp, this solution gets a bit more complex:
formatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.of("UTC")))
Then I would take java.text.SimpleDateFormat
:
new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(System.currentTimeMillis())
To get back to the Timestamp:
new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse( "02/12/2012 12:23:44" ).getTime
Don't overthink it: nothing wrong with just new Date(start).toString
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