kevin
(kevin)
October 3, 2016, 4:19am
1
I would like to add 2 days to the current weekday and post it to a string, I’ve tried do something like
var String LastRunDay = now.getDayOfWeek
var String NextRunDay = now.getDayOfWeek.plusDays(2)
postUpdate(PreviousRunDay (LastRunDay.toString)
postUpdate(FutureRunDay (NextRunDay.toString)
The current day works, but the plus 2 day doesn’t. Is there another way apart from constructing a full DateTime Value and using parse?
Designer reports “Couldn’t resolve reference to JvmIdentifiableElement ‘plusDays’” and a quick google suggests that it is a valid function in Java assuming I’m understanding it correctly.
Another wavy is to extract the value from a full DateTime variable to give a display in the interface of something like.
LastRuntime was Friday @ 12:30
NextRuntime is Sunday @ 12:30
However it would be really helpful to know if there is a way of easily adding eeekdays
I can’t check the Joda DateTime API because it is blocked at work but I would expect the plusDays method to be called on the DateTime object and not the String return value of getDayOfWeek.
So maybe try:
var String LastRunDay = now.getDayOfWeek
var String NextRunDay = now.plusDays(2).getDayOfWeek
postUpdate(PreviousRunDay (LastRunDay.toString)
postUpdate(FutureRunDay (NextRunDay.toString)
var int NextRunDay = now.plusDays(2).getDayOfWeek
this is the correct way to calculate the day, but this
FutureRunDay.postUpdate(NextRunDay.toString)
would end in a Number, formatted as a string (afaik 1 to 7, where 1 is monday), so, if FutureRunDay is of type string, you could set up an array:
var String[] dayNames = new String[] {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
FutureRunDay.postUpdate(dayNames[now.plusDays(2).getDayOfWeek - 1]) // array is 0 to 6
It may be worth to try to use a DateTime item instead:
FutureRunDay.postUpdate(new DateTimeType(now.withStartOfDay.plusDays(2).plusHours(12).plusMinutes(30)))
with this item:
DateTime FutureRunDay "NextRuntime is [%1$tA @ %1$tT]"
kevin
(kevin)
October 4, 2016, 1:00am
4
Thanks for the replies. I couldn’t get the dayNames string to work so I ended up with
LastStartedTime = parse(LastRuntime.state.toString)
NextEnableTime = LastStartedTime.plusDays(2)
var int LastRunDayInt = LastStartedTime.getDayOfWeek
var int CurrentDay = now.getDayOfWeek
var String LastRunTime = (String::format("%02d:%02d", LastStartedTime.getHourOfDay(),LastStartedTime.getMinuteOfHour()))
var int NextRunDayInt = NextEnableTime.getDayOfWeek
var String NextRunTime = (String::format("%02d:%02d", NextEnableTime.getHourOfDay(),NextEnableTime.getMinuteOfHour()))
var String LastRunDay
var int ElapsedDays = CurrentDay - LastRunDayInt
if (ElapsedDays == 0) { LastRunDay = "Today"}
if (ElapsedDays == 1 || ElapsedDays == -6) { LastRunDay = "Yesterday"}
if (ElapsedDays > 1 || ElapsedDays < -6) {
if (LastRunDayInt == 1) { LastRunDay = "Monday" }
if (LastRunDayInt == 2) { LastRunDay = "Tuesday" }
if (LastRunDayInt == 3) { LastRunDay = "Wednesday"}
if (LastRunDayInt == 4) { LastRunDay = "Thursday" }
if (LastRunDayInt == 5) { LastRunDay = "Friday" }
if (LastRunDayInt == 6) { LastRunDay = "Saturday" }
if (LastRunDayInt == 7) { LastRunDay = "Sunday" }
}
var String FullLastRunTime = (LastRunDay+ " @ "+LastRunTime )
postUpdate(LastRun, (FullLastRunTime))
var String NextRunDay
var int DaysToGo = NextRunDayInt - CurrentDay
if (DaysToGo == 0) { NextRunDay = "Today"}
if (DaysToGo == 1 || DaysToGo == -6) { NextRunDay = "Tomorrow"}
if (DaysToGo > 1 || DaysToGo < -6) {
if (NextRunDayInt == 1) { NextRunDay = "Monday" }
if (NextRunDayInt == 2) { NextRunDay = "Tuesday" }
if (NextRunDayInt == 3) { NextRunDay = "Wednesday"}
if (NextRunDayInt == 4) { NextRunDay = "Thursday" }
if (NextRunDayInt == 5) { NextRunDay = "Friday" }
if (NextRunDayInt == 6) { NextRunDay = "Saturday" }
if (NextRunDayInt == 7) { NextRunDay = "Sunday" }
}
var String FullNextRunTime = (NextRunDay+ " @ "+NextRunTime )
logInfo("Testing", "Last runtime is " +FullLastRunTime)
logInfo("Testing", "Next runtime is " +FullNextRunTime)
postUpdate(NextRun, (FullNextRunTime))
This gives me as an easily readable Item similar to
Next Runtime Tomorrow @ 10:28
Last Runtime Yesterday @ 10:28
Argh… mixed up the languages…
val List<string> dayNames = newArrayList("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
should do it.
instead of typing if(condition)
over and over again, you could use switch case:
switch (NextRunDayInt) {
case 1 : NextRundDay = "Monday"
case 2 : NextRundDay = "Tuesday"
case 3 : NextRundDay = "Wednesday"
case 4 : NextRundDay = "Thursday"
case 5 : NextRundDay = "Friday"
case 6 : NextRundDay = "Saturday"
case 7 : NextRundDay = "Sunday"
}
kevin
(kevin)
October 5, 2016, 1:23am
6
Thanks, the case statement makes it a bit more readable.
Can “switch case” be used with anything or just numbers?
At least strings will work:
switch (myString) {
case "one" : {
// do something
}
case "two" : {
// do something
}
default : {
// if no case above matches, do this
}
}
As you can see, there is even a “do something in case of unexpected values” statement.
kevin
(kevin)
October 5, 2016, 7:37pm
8
Thanks, I’ll experiment where it makes sense. Its always good to expand my knowledge!