OH3 comparing date string to now

Hi,
I’m having quite a lot of trouble converting an old OH2 rule for OH3.
What I wanna do is compare a date in a string to the date now.
So I read this post, the one I need should be #9 Get Epoch from String.

This is the part of the code that is not working
In the variable line I have the date string:

logInfo("Trash", "Date:" + line+"T00:00:00.000Z" )
val Number MyEpochFromString = new DateTime(line+"T00:00:00.000Z").millis
//The line above crashes with "An error occurred during the script execution: null in trash"
logInfo("Trash", "Date:" + line +"===>"+now.compareTo(MyEpochFromString))
if (now.compareTo(MyEpochFromString) <=0){
     Restmuell.sendCommand(line)
}

The log info above the crash gives me:
Date:2021-01-19T00:00:00.000Z
Which looks correct to me. I don’t understand why the conversion line below crashes?
Can someone explain me where I’m going wrong?
Thanks and best regards
Daniel

Because openHAB not longer supports Joda DateTime. You need to use a Java ZonedDateTime.

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/ZonedDateTime.html

ZonedDateTime.parse(line+"T00:00:00.000Z")

might work. Or you might need to reformat the string to match the default format used by ZonedDateTime.

Thanks a lot Rich, that worked.
For my understanding: The post I linked is wrong then? Because it’s written for OH3?

Yes, that particular example is wrong. There is no DateTime anymore.

1 Like