DateTime Conversion (openHAB 3.x)

@ljsquare You are right, thanks. I looked up the code of DateTimeType , and it turns out it’s only a thin wrapper around ZonedDateTime. So there is an easy conversion between the two, without the need for any String parsing or formatting.

I’ve rewritten my rule as follows:

  val morning_start = ZonedDateTime.now().with(LocalTime.MIDNIGHT).plusHours(7) // 7:00

  // Java time to DateTimeType:
  vMorning_Time.postUpdate(new DateTimeType(morning_start)) 

  // DateTimeType to Java time:
  val day_start = (vSunrise_Time.state as DateTimeType).getZonedDateTime()

Much cleaner and less error prone.