Converting Unix Timestamp into DateTime item

Dear all,

I’m struggling with converting Unix timestamp (e.g. “1422616466”) into DateTime.

UX Timestamp I receive from OwnTracks and following rule handles it:

I’ve added following code to handle timestamp information:
var String tst = transform(“JSONPATH”, “$.tst”, json)

var DateTime time = new DateTime((tst as Long) * 1000L)

sendCommand(lastStatusUpdate, time)

The error message:
[ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘MqttPostionParse’: Cannot cast java.lang.String to java.lang.Long

Thank you very much for your help
Peter

Instead of (tst as Long), try Long::parseLong(tst) using the static parseLong method on java.lang.Long.

1 Like

Hello watou,

great, that worked. There were some other errors in the code, so pasting working solution for completeness:

    import org.joda.time.*
...
    var String tst = transform("JSONPATH", "$.tst", json)
    var DateTime time = new DateTime(Long::parseLong(tst) * 1000L)
    lastStatusUpdate.postUpdate(new DateTimeType(time.toString))

Thank you for your prompt help
Peter

3 Likes

I am wondering how I can create an Unix timestamp.
I want publish the position of my car that I received from VolvoOnCall in OwnTracks.

/Mike

Hi,

Do you have a solution for that , I have the sane problem, but I do not parse at the moment from JSON and date epoch is coming to ITEM, I would like to create similar rool for that, convert from epoch to date time OpenHab java rule code convention.

Rds,
RL