Convert an epoch which is missing digits into friendly date format

Through an API, I am getting an epoch time number which is lacking 3 additional zeros at the end for the function I want to use. Multiplying by 1000 gives me a signed number. Forcing it to a string, I can add the 000 to the end. However, the function doesn't take a string. Trying to convert it into an integer gives an error from the function. If I manually enter the number into the sdf.format function, it works. How do I get this to work programmatically?

Is there another name for a time stamp that is 3 digits less than an epoch time? Are the different, more appropriate functions I can use for displaying a user friendly date?

SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM YYYY, HH:mm")
epoch = "${response.data.reset.toString()}" + "000"
sendEvent(name:"Reset Date", value: sdf.format(Integer.parseInt(epoch)))

I usually use .toInteger() to convert strings to integer.

Does this work?

epoch = ("${response.data.reset.toString()}" + "000").toInteger()

Not where I can test but maybe

SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM YYYY, HH:mm")
epoch = (long) response.data.reset
rDate = new Date(epoch)
sendEvent(name:"Reset Date", value: sdf.format(rDate))

This line doesn't throw an error. But the formatting function throws this.

Maybe also try

epoch = ("${response.data.reset.toString()}" + "000") as int

or maybe you need a type, or at least def

Integer epoch = ("${response.data.reset.toString()}" + "000") as int

(long) was the key. I had to multiply by 1000 to get the correct date/time.
epoch = (long) response.data.reset * 1000

Thank you everyone! I've learned a couple additional things even from the answers that didn't work for me.

1 Like