[SOLVED] Apply plusMinutes to String time

Dear Community,

I have string variables (String type) with specified times (for example: “04:15”, “10:35”), I read from file.
How can I apply plusMinues() plusSeconds() to these string variables.

I tried to convert using

val SimpleDateFormat sdf = new SimpleDateFormat("hh:mm")               
sdf.parse(time.get(2)).plusMinutes(15)

put I got error: ‘plusMinutes’ is not a member of ‘java.util.Date’

How can I make it ?

And if it is necessary to create DateTime from this time, how can I create it ?

I assume the time is the string containing “04:15”…

val hours = Integer::parseInt(time.split(":").get(0)
val minutes = Integer::parseInt(time.split(":").get(1)
val newTime = now.withTimeAtStartOfDay.plusDays(1).minurHours(24-hours).plusMinutes(minutes + 15)
val newTimeString = newTime.toString("HH:mm")

The third line seems complicated but we add a day and then go back in time to avoid Summer Time change problems

1 Like

Thanks, it works, and I want to ask how I get datetime from date String + time String ?
And is it better to work with String or DateTime ?

val hours = Integer::parseInt(time.split(":").get(0))
val minutes = Integer::parseInt(time.split(":").get(1))
val newTime = now.withTimeAtStartOfDay.plusDays(1).minusHours(24-hours).plusMinutes(minutes + 15)
val newTimeString = newTime.toString("HH:mm")
val new DateTimeType(newTime.toString)

Ask yourself. What does that data represents and apply the best type to it.
Your data represents a time so apply a DateTimeType, then you have all the available method included in the type available.

Stick to jodatime in the DSL. Then you can use 'plusMinutes` and others…

1 Like

No, actually I mean if I have two String variables (one for date and other for time), how to create valid datetime variable equal the combination of date String and time String ?

See: https://www.openhab.org/docs/configuration/rules-dsl.html#working-with-item-states-conversions