DateTime conversion & calculation not working after moving from OH2 to OH3

Hi,

I need some help with time calculations in OH3.
I had a garbage calendar rule running for some time in OH2 but I can’t get it to work in OH3.
This rule always generates error messages and I cannot figure out the right format.

logWarn("muellabfuhr","pruefung gestartet")

    var String muelltonne_1
    muelltonne_1 = Muell_1.state.toString

    var String muelltonne_2
    muelltonne_2 = Muell_2.state.toString

    val String muelltonne_2_datum  = Muell_2_Datum.state.format("%1$td.%1$tm.%1$tY")

    val telegramAction = getActions("telegram","telegram:telegramBot:d1dba6be")

    // Prüfung ob der Wert gefüllt ist (damit beim cast auf DateTimeType keine Fehlermeldung erscheint)
    if (muelltonne_1 != "UNDEF") {
        // Datum der Abholung wird mit dem aktuellen Datum verglichen
        // die erste Bedingung prüft, ob das aktuelle Datum vor dem Ablaufdatum + 24 Std. liegt
        // die zweite Bedingung prüft, ob das aktuelle Datum nach dem Datum der Abholung liegt
logWarn("muellabfuhr","pruefe condition")

if (
now.isBefore(new DateTime((Muell_1_Datum.state as DateTimeType).getZonedDateTime.toInstant().toEpochMilli).plusHours(18))
&&
now.isAfter(new DateTime((Muell_1_Datum.state as DateTimeType).getZonedDateTime.toInstant().toEpochMilli).minusHours(18))
){

            // Benachrichtigung per Telegram an Bot senden
            if(muelltonne_2 == "UNDEF") {
                telegramAction.sendTelegram("♻️ Muellabfuhr: Morgen nicht vergessen, die *%s* rauszustellen!", muelltonne_1)
            }
                else {
                telegramAction.sendTelegram("♻️ Muellabfuhr: Morgen nicht vergessen, die *%s* rauszustellen! 🚚 Die nächste Abfuhr ist die *%s* am %s", muelltonne_1, muelltonne_2, muelltonne_2_datum)
    }
        }
    }

I have read DateTime Conversion (openHAB 3.x) and ZonedDateTime (Java SE 11 & JDK 11 ) and spent several hours now looking for a solution but just cannot figure that out.
I am not a Java person so any hints would be appreciated!!!

The problem:

now.isBefore(new DateTime((Muell_1_Datum.state as DateTimeType).getZonedDateTime.toInstant().toEpochMilli).plusHours(18))
&&
now.isAfter(new DateTime((Muell_1_Datum.state as DateTimeType).getZonedDateTime.toInstant().toEpochMilli).minusHours(18))

causes
1. DateTime cannot be resolved.; line 19, column 774, length 8
2. DateTime cannot be resolved.; line 21, column 898, length 8

Changing to DateTimeType:
1. The method plusHours(int) is undefined for the type DateTimeType; line 19, column 868, length 9
2. The method minusHours(int) is undefined for the type DateTimeType; line 21, column 996, length 10
3. Type mismatch: cannot convert from long to ZonedDateTime; line 19, column 787, length 79
4. Type mismatch: cannot convert from long to ZonedDateTime; line 21, column 915, length 79

and so on…

As was stated in the release notes and list of breaking changes, Joda DateTime was replaced with ZonedDateTime. So DateTime no longer exists. You need to witch to using ZonedDateTime instead.

Luckily, now already is a ZonedDateTime. Any you already show how to get a ZonedDateTime from a DateTimeType. So all you really have to do is get rid of all the new DateTime stuff.

So your if would change to something like

if(now.isBefore((Muell_1_Datum.state as DateTimeType).getZonedDateTime.plusHours(18)
1 Like

Oh, so blind…

Thank you @rlkoshak !!!