DateTime Conversion (openHAB 2.x)

You can try #1, but you have to watch the string-format.

For me this works for DateTimeType:

val MyString = "2018-10-27T18:39:00.105+0200"
val DateTimeType MyDateTimeType = DateTimeType.valueOf(MyString) // Format "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
logInfo("Demo", "      #1 MyDateTimeType = " + MyDateTimeType.toString)

Edit: @halloween sorry, my mistake. You talk about Joda DateTime. Right?
So it should be #5.

val MyString = "2018-10-27T18:39:00.105+0200"
val DateTime MyJodaFromString = new DateTime(MyString)
logInfo("Demo", "      #5 MyJodaFromString = " + MyJodaFromString.toString)

I never knew about withTime. That can eliminate a lot of the withTimeAtStartOfDay.plusHours stuff. I’m surprised there isn’t a with date that works the same.

Thought I brought it up in a DST conversation in the ToD DP thread as an alternative to withTimeAtStartOfDay, since it is DST safe. Sorry for being OT, but this is very handy with Intervals

import org.joda.time.Interval
import org.joda.time.Days

if (new Interval(now.withTime(9,0,0,0), now.withTime(17,0,0,0)).contains(now)) {
    // do stuff between 9am and 5pm

Another use of Interval with a DateTime Item…

if (new Interval(Days.days(7), new DateTime(Sun_Eclipse_Total.toString)).contains(now)) {
    // do eclipse stuff

I want to get the time into a datetime item, so it would be #1 i think? No variable with joda-datetime.

But my given string is in the format “2018-10-25 11:00”. So there is no “T” and no “+02:00” inside it… So how can i change this given string-item into a datetime-item?

Try something like that:

val String MyDate = DWD_Pollen_last_update.state.toString().substring(0, 10)
val String MyTime = DWD_Pollen_last_update.state.toString().substring(11, 16)
val String MyString = MyDate + "T" + MyTime + ":00.000+0200"
val DateTimeType MyDateTimeTypeFromString = DateTimeType.valueOf(MyString)
PollenLastUpdateDateTime.postUpdate(MyDateTimeTypeFromString) 

Edit: This worked for me after I solved an error in the suggested code.

Is this tutorial affected by the recent breaking change discussed here?

Looks like it will be affected. Thank you for that information.
I would not update this threat until the new version 2.4 is released.
What do you think?

1 Like

Because the breakage is bigger than expected there is discussion of going back to joda for the time being or figuring out a way to get those missing methods back. Watch the pr thread for details. I wouldn’t do anything until that gets resolved.

1 Like

There is a method withDate:

Returns a copy of this datetime with the specified date, retaining the time fields.

I have searched and searched, but need some help. Maybe somebody ( @rlkoshak? ) knows the trick I need or what I’m doing wrong…

Trying to instantiate a DateTime with the epoch milliseconds not from calendar or now.millis but from a number item (set by Android HAAS app). I am multiplying by 1000 to get milliseconds first and storing result as a Number. When I try to use DateTime constructor though I get an Error. I have tried to explicitly set the ‘alarmTime’ variable as a long and other things, but nothing seems to work. Here is the relevant code:

        val Number milliseconds = (Bedside_Light_Alarm_DateTime.state as Number) * 1000
        logInfo("Debugging DateTime crap...", "The milliseconds is: " + milliseconds)
        val DateTime alarmTime = new DateTime(milliseconds)
        logInfo("Debugging DateTime crap...", "The alarmTime Calender/DateTime is: " + alarmTime.toString)
2019-01-14 10:26:38.786 [INFO ] [el.script.Debugging DateTime crap...] - The milliseconds is: 1547506800000

2019-01-14 10:26:38.789 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Bedroom Light Alarm Updated': No instant converter found for type: java.math.BigDecimal
new DateTime(milliseconds.longValue)

The constructor requires a primitive (I think long) but milliseconds is a Number.

1 Like

Glad I summoned you, that did the job. Thanks!

Awesome post, helped me out.

Thanks
Paul

Number AlarmClock
DateTime Alarmzeit "Alarmzeit [%1$tH:%1$tM]"

This rule…

rule "Alarmzeit"
when
    Item AlarmClock changed
then
    val DateTimeType MyDateTimeTypeFromEpoch = new DateTimeType(new DateTime(AlarmClock).toString)
    logInfo("Algemein.rules",MyDateTimeTypeFromEpoch.toString)
end

causes this error…

2019-05-01 19:47:28.139 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Alarmzeit': No instant converter found for type: org.eclipse.smarthome.core.library.items.NumberItem

What is wrong in my converting? Maybe someone can help.

new DateTime(AlarmClock.state.toString)

1 Like
rule "Alarmzeit"
when
    Item AlarmClock changed
then
    val DateTimeType MyDateTimeTypeFromEpoch = new DateTime(AlarmClock.state.toString)
    logInfo("Algemein.rules",MyDateTimeTypeFromEpoch.toString)
end

Now there is a differnt error:

2019-05-01 22:03:56.828 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Alarmzeit': Invalid format: "1556769600000" is malformed at "0000"

AlarmClock isn’t a DateTimeType nut a Number. You need to convert the Number to a long.

new DateTime((AlarmClock.state as Number).longValue)

or something like that.

I missed that in the OP.

Sorry Rich,
still not working. How hard could it be to convert a ordinary number.

rule "Alarmzeit"
when
    Item AlarmClock changed
then
    val DateTimeType MyDateTimeTypeFromEpoch = new DateTime((AlarmClock.state as Number).longValue)
    logInfo("Algemein.rules",MyDateTimeTypeFromEpoch.toString)
end
2019-05-04 14:10:14.611 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Alarmzeit': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.core.library.types.DateTimeType.toString() on instance: 2019-05-04T14:20:17.795+02:00

DateTime and DateTimeType are not the same thing. You’ve declared the available to be DateTimeType but you are creating a DateTime to store in the variable.

1 Like

Too obvious I guess. Thanks a lot. Problem solved.:grinning: