(solved) 'plusHours' is not a member of

Tags: #<Tag:0x00007f616955e908>

I’m trying to add some hours (and later on even days) to a DateTime item, but it won’t work. I do have same issues with any other ‘plus…’ and ‘minus…’ methods.

PV_Graf_Ertrag_Datum.postUpdate((PV_Graf_Ertrag_Datum.state as DateTimeType).plusHours(2))

This is the error message:

'plusHours' is not a member of 'org.eclipse.smarthome.core.library.types.DateTimeType';

Maybe I’ve done something wrong in my imports !?

import org.openhab.model.script.actions.Timer
import org.joda.datetime.DateTime
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatter
import org.openhab.library.types.DateTimeType
import java.text.SimpleDateFormat
import java.util.Date

Any idea?

I think you shouldn’t need any of those imports at all. The relevant stuff is already imported. What happens if you simply remove them?

… the issue keep staying. Plus additional errors appear… You still need to import some stuff like for SimpleDateFormat.
Sorry, this wasn’t the solution, but thanks anyway!

Yep, I stand corrected. SimpleDateFormat needs to be imported. I still claim that you don’t need the others though. I use lots of date stuff in my rules but the only thing I import is java.text.SimpleDateFormat.

Ok, I droped every import statement except of java.text.SimpleDateFormat. Still the same problem:

08:21:09.173 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'SMA_WL_Begrenzung_Changed': 'plusHours' is not a member of 'org.eclipse.smarthome.core.library.types.DateTimeType'; line ...

What surprises me is that when I look to the Eclipse Documentation I can’t find any time calculation methods, like plusSeconds, minusSeconds, plusMinutes… !?

Any idea?

Nope. Lost me. I think I’ll throw in @rlkoshak here, I think he’s the man for this :wink:

PV_Graf_Ertrag_Datum.postUpdate(new DateTimeType(new DateTime((PV_Graf_Ertrag_Datum.state as DateTimeType).calendar.timeInMillis).plusHours(2).toString))
PV_Graf_Ertrag_Datum.postUpdate(new DateTime((PV_Graf_Ertrag_Datum.state as DateTimeType).zonedDateTime.toInstant().toEpochMilli).plusHours(2).toString)

Example 1 or better 2 because

The method getCalendar() from the type DateTimeType is deprecated

@n_c, many thanks. It works!
I still do not understand why my way did not work, because copied it from a past conversation done by @rlkoshak, but anyway I’m happy now.

Maybe it’s a old example and the systax has changed.

OK, let’s try to get to the root of the problem. These are two example’s … 1st doesn’t work, 2nd works! The question is why?

A_datatime_item.postUpdate((A_datatime_item.state as DateTimeType).plusHours(2))
A_datatime_item.postUpdate(new DateTime((A_datatime_item.state as DateTimeType).zonedDateTime.toInstant().toEpochMilli).plusHours(2).toString)

The 1st example is taken from this thread. So it worked in Jan 2016.

You need none of those imports. At least one of them is invalid anyway (there is no org.openhab.libaray.types package any longer).

You would need to import for SimpleDateFormat but you don’t need SimpleDateFormat as far as I can tell.

You need to convert the DateTimeType to a Joda DateTime. See https://docs.openhab.org/configuration/rules-dsl.html#datetime-item. Once you have converted it to a Joda DateTime you can call plusMinutes et al and postUpdate it back to your DateTime Item using toString.

n_c’s example does this all in one line.

That example has never been valid. I only did a quick scan but in all of my posts I use now which is a Joda DateTime Type and which has the plusHours method. If I had an example like your first one it was wrong and was a typo of some sort.

Your second example does what I said above, converts the DateTimeType to a DateTime to call plusHours and then passes it to the DateTime Item as a String using postUpdate.

2 Likes

Many thanks rlkoshak for this great explanation!
Pirx

I have to admit, being an experienced software developer (not in Java though, mind it) and having used OH for over a year I still think it’s confusing with all those different data types being used in the rules. I’ve never really understood for example why there is a need for multiple very similar data types for date and time. But maybe this is a general Java thing? Is there a good guide somewhere just to get the head around it?

Hi rlkoshak,
it drives me still crazy. I followed the link and tried this one:

// Get the hour in the day from a DateTimeType
val hours = (MyDateTimeItem.state as DateTimeType).calendar.get(Calendar::HOUR_OF_DAY)
// See the Calendar javadocs for the full set of parameters available

… and get the following Error:

[ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'xxxx': The name 'Calendar' cannot be resolved to an item or type;

Now, I found a solution. This here works fine :slight_smile:

val int tm_year = (MyDateTimeItem.state as DateTimeType).zonedDateTime.getYear

Looks like the docs still need to be updated