Epoch timestamp to date / time

I have an item

Number	 	VU_Start  				"Start Sendung [%s]"  		( VUPLUS )	{ http="<[http://192.168.1.6:80/web/getcurrent:3000:XPATH(//e2currentserviceinformation/e2eventlist/e2event/e2eventstart)]" }

and want to use the received timestamp (epoch) e.g. 1514484000 (=Thursday, 28. December 2017 18:00:00 UTC)
as Date Time (actually as HH:mm)

I tried the example rules

// Convert epoch to a human readable
val SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
val String timestampString = sdf.format(new Date(timestampEpoch))

from here

but I failed I always get something like this: 1970-01-18T13:41:24.000+0100

my rule:

import java.text.SimpleDateFormat
import java.util.Date

/* Epoch */
rule "Dauer"
when
		Item   VU_Reference changed
then

		val timestampEpoch = (VU_Start.state as Number).intValue
		val SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
		val String timestampString = sdf.format(new Date(timestampEpoch))
					
			
			logWarn("hilfe", "Start Epoch " +timestampEpoch)
			logWarn("hilfe", "Start  " +timestampString)

end

The timestampEpoch gives me the correct number but the timestampString is wrong

any help appreciated!

thanks

1 Like

Hi, I might be a bit slow understanding what you want, but are you looking for the current time in HH:MM format? If so, you could try :

val DateTime datetime = new DateTime(1514484000)
val String time = datetime.toLocalTime().toString("HH:mm")

If I misunderstood, then please explain a bit more what you want to achieve.

I could solve my issue now

The problem was that I have to deal with very long numbers and change the value from intValue to longValue

	val timestampEpoch = (VU_Start.state as Number).longValue

e.g. 1514484000 (=Thursday, 28. December 2017 18:00:00 UTC)
this must be multiplied with 1000 and then the rule


		val timestampEpoch = (VU_Start.state as Number).longValue
		val SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
		val String timestampString = sdf.format(new Date(timestampEpoch))

is working properly

the formatting to “HH:mm” was not the problem

thank you anyway
Markus

by the way you should think about to reviise the docs describing that.
The conversion from epoch to Date needs a multiplication ( * 1000) because it is calculating milliseconds