Timezone conversion

Hey there,

in my openHAB 1.8.2 configuration I have a String item named Date_Raw which gets updated with a UTC time/date string (from an MQTT source). I have another DateTime item named Date_Local which should be the “local time” version of the previously mentioned UTC item (currently UTC+2 as we have DST). The problem is I’m not sure how to do the conversion.

At the moment my rule is only converting from String to DateTime, without doing any time conversion at all:

rule "Date changed"
when
    Item Date_Raw changed
then
    var SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss")
    var DateTime dt = new DateTime(sdf.parse(Date_Raw.state.toString))
        
    var DateTimeType dtt = new DateTimeType()
    dtt.calendar.setTime(dt.toDate)
    Date_Local.postUpdate(dtt)
end

Well I know that it could be simplified by using Date instead of DateTime but I thought for the timezone conversion DateTimeZone could help. I’ve played around with it but e.g. using DateTimeZone::UTC results in a NullPointerException.

How to do it correctly?

Thanks in advance.

Never mind, I have finally figured it out. The NullPointerException happened due to a missing import.

rule "Date changed"
when
    Item Date_Raw changed
then
    var SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss")
    format.timeZone = TimeZone::getTimeZone("UTC")    
    var Date date = format.parse(Date_Raw.state.toString)
    
    var DateTimeType type = new DateTimeType()
    type.calendar.setTime(date)
    Date_Local.postUpdate(type)
end

I am having the same problem. @rfk could you please post the necessary imports. Thanks in advance.

Those are the ones I use in my rule set:

import java.text.SimpleDateFormat
import java.util.Date
import java.util.TimeZone
import org.joda.time.DateTime

Guess it was the 3rd one.