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';
… 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… !?
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.
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.
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.
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
val int tm_year = (MyDateTimeItem.state as DateTimeType).zonedDateTime.getYear