VERY simple date diff

Hi,

i am using OH3 and want to do a simple date comparison.

This was working in OH2 but in OH3 its not - which sucks.

  var DateTimeType prevOnState
  var jetzt = new DateTimeType()

  prevOnState = Praesenz_Innen_Zuletzt_Wann.state as DateTimeType
  diff = Seconds::secondsBetween(new DateTime(prevOnState.zonedDateTime.toInstant().toEpochMilli), now ).getSeconds()

Can someone explain me how to get the difference(in seconds, milliseconds or minutes).

Thank you so much.

Ther was a change in the used time library, maybe that helps:

unfortunately not - I am not a java developer.

A simple example would be nice.

Working with time data has its quirks. It is never simple.

Life’s no pony farm. Read the OH 3.0 release notes on relevant changes w.r.t. joda time classes.

rules DSL isn’t java. Time calculations can be nasty, true, but there’s no feasible other way than to sit down and fiddle with the code until you understand it. The link @Sascha_Billian gave has more or less all the information it takes.

Use

var long diff = jetzt.getZonedDateTime.toEpochSecond - prevOnState.getZonedDateTime.toEpochSecond

or

if (jetzt.getZonedDateTime.toEpochSecond > prevOnState.getZonedDateTime.toEpochSecond)
1 Like

isn’t that the part in the docs that is lost ?

yes but a forum search should get you a copy

A slightly more flexible variation is to use java.time.Duration.

var diff = Duration.between(prevOnState, jetzt.getZonedDateTime).getSeconds()

And depending on what is being done with diff it might make sense to keep it as a Duration which has all sorts of useful methods. Duration (Java SE 11 & JDK 11 ).