Time conversion rule of DateTime item

I have an Item that gets populated with a DateTime in UTC, and I have it displayed in OH UI in local time - all is fine.
I however wish to send the displayed value, in local time via MQTT, but when i try this, the value that is sent is the raw DateTime value, ie. UTC, and not local time. Since I live in a country with daylight savings time, i cannot simply add a certain amount of hours.

I have tried 2 solutions but failed:

  1. Perfom some transformation on the outgoing value - I cannot find any way to edit the timezone used in the state formatter - if this is a possibility it would probably be the best/easiest

  2. The alternate method I tried was to make a proxy item that is updated along with the original item, but I have been unable to find a way to apply the correct time corrections to the proxy item - here is the rule i have tried:

val localtime = (cheapestPriceTime.state as DateTimeType).toLocalDateTime()
cheapestPriceTime_ProxyItem.sendCommand(localtime)

but this only adds the time zone difference, but does not change the time itself to reflect the local time. Due to DST I cannot just put a fixed value.

I worked it out after some 8 hours of :exploding_head:

val localTimeItem = (UTCtimeItem.state as DateTimeType).getZonedDateTime().withZoneSameInstant(ZoneId.systemDefault())
val DateTimeType newDateTimeItem = new DateTimeType(localTimeItem) // conversion from ZonedDateTime Object to OH DateTime Object
MyDateTimeItem.postUpdate(newDateTimeItem)

What you did certainly works. Another approach that might work would be to get the LocalDateTime from the ZonedDateTime. I think that will convert the time and strip off the timezone.

val localTimeItem = (UTCtimeItem.state as DateTimeType).zonedDateTime.toLocalDateTime()

I tried this combination, and this does strip off the timezone, but does not change it to the local timezone, ie. it just leaves the hours and minutes as they are, so if the time was 12:00 UTC, the new time would become 12:00 local instead, which really is a time-shift rather than conversion.