DateTime Conversion

Tags: #<Tag:0x00007fc3ed4dcb38> #<Tag:0x00007fc3ed4dca70> #<Tag:0x00007fc3ed4dc9a8>

@anfaenger This page is extremely useful. Thank you for writing it. However, I noticed a tiny error, which, if fixed would greatly help:

Please update the link to Joda DateTime class to http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html

thank you

2 Likes

Done.
Thanks a lot.

1 Like

Hi,
I had a lot of trouble with this yesterday and today and just wanted to share my solution in case anyone else has the problem:

Taking the Unix timestamp (Epoch) from a Number item and constructing a new DateTime variable.
Edit:
Preferrable solution, thanks to @rlkoshak:

var DateTime test = new DateTime((AlarmTime_Unix.state as Number).longValue * 1000)

My solution:

var DateTime test = new DateTime(Long::parseLong(AlarmTime_Unix.state.toString) * 1000)
logInfo("Test_Button", "Variable test: " + test.toString)
// AlarmTime_Unix.state: 1562909701
// Variable test: 2019-07-12T07:35:01.000+02:00

The initial post did not help me with this.

1 Like

Assuming AlarmTime_Unix is a Number Item like you say, you can

var DateTime test = new DateTime((AlarmTime_Unix.state as Number).longValue * 1000)

This avoids the unnecessary conversion from a Number to a String and then back to a Number.

Hi Rich,

Thanks, indeed it works. I could swear I’ve tried that aswell with the five other ways…
I’ve edited your code into my answer.

Hi @Chewie,

It shows how to convert from Epoch to JodaDateTime, but you want to convert from NumberItem to DateTime.
As you and Rich has shown, in this case it is first necessary to convert the NumberItem into an Epoch value.
I added your solution to my inital post:

Thank you for sharing!

1 Like

Great, happy to see it in the initial post. Maybe it helps somebody :slight_smile:

1 Like

Hi, please can someone help me with my problem? I want to convert a time in milliseconds (this is joda, right?) to a readable DateTime item:

I get the next alarmclock time from my android app (habdroid) in this format:

// provided from the phone in this format --> 1570744800000
Number Phone_AlarmClock   "Alarm Clock Phone [%d]"

// this item is for the rule
DateTime Phone_AlarmClock_DT   "Alarm Clock Phone DT [%1$tA, %1$td.%1$tm.%1$tY %1$tT]"

My rule which isn´t workin:

rule "Phone Alarm Clock to DateTime"
	when
		Item Phone_AlarmClock received update or
		Time cron "0/5 * * * * ?"   // only for testing
	then
		val alarmtime = Phone_AlarmClock.state
		val MyDateTimeTypeFromJoda = new DateTimeType(alarmtime.toString)
		Phone_AlarmClock_DT.postUpdate(MyDateTimeTypeFromJoda)
end

Error 1:
Error during the execution of rule ‘Phone Alarm Clock to DateTime umrechnen’: 1570744800000 is not in a valid format.

When i change

val alarmtime = Phone_AlarmClock.state

to

val alarmtime = Phone_AlarmClock.state as number

i get this error:
Error 2:
Error during the execution of rule ‘Phone Alarm Clock to DateTime’: Could not cast 1570744800000 to void; line 29, column 19, length 37

What´s wrong here?

Try that:

rule "Phone Alarm Clock to DateTime"
when
    Item Phone_AlarmClock received update or
    Time cron "0/5 * * * * ?"   // only for testing
then
    val alarmtime = Phone_AlarmClock.state.toString
    val MyDateTimeTypeFromJoda = new DateTimeType(alarmtime)
    Phone_AlarmClock_DT.postUpdate(MyDateTimeTypeFromJoda)
end

Can’t the Phone_AlarmClock be a DateTime Item?

I don´t know?

Here you can see it in the docs: https://www.openhab.org/docs/apps/android.html#alarm-clock

@vzorglub
With your changes i get this error: : String index out of range: 10

Ok, according to the first post we need to use method # 2

new DateTimeType(new DateTime(MyEpoch).toString)

So:

rule "Phone Alarm Clock to DateTime"
when
    Item Phone_AlarmClock received update or
    Time cron "0/5 * * * * ?"   // only for testing
then
    val alarmtime = Phone_AlarmClock.state.toString
    val MyDateTimeTypeFromJoda = new DateTimeType(new DateTime(alarmtime).toString)
    Phone_AlarmClock_DT.postUpdate(MyDateTimeTypeFromJoda)
end

BUT @5iver has a good point, it would be worth trying to set the item as a DateTime just to try

Thanks, it seems that my given time from android app is to long.

I get this error: Invalid format: “1570744800000” is malformed at “0000”


I will try the other hint with changing the item too.

EDIT: Then i get this error:
Tried to set invalid state 1570744800000 (DecimalType) on item PhoneBernd_AlarmClock of type DateTimeItem, ignoring it

Now i changed some things and it works:

rule "Alarm Clock"
when
    Item Phone_AlarmClock changed or
    Time cron "0/5 * * * * ?"     // for testing
then
    if (Phone_AlarmClock.state as Number != 0 && Phone_AlarmClock.state as Number != NULL) {
        	var alarmtime = new DateTime((Phone_AlarmClock.state as Number).longValue)
        	logInfo("RULES", "AlarmClock - alarmtime " +  alarmtime.toString)
		Phone_AlarmClock_DT.postUpdate(alarmtime.toString)
    }
end