How to get back a Date stored in state?

I'm having trouble getting back a Date object stored in the state of an app.
Code:

presenceInfo.atTime = new Date()
Date time = toDateTime(presenceInfo.atTime)

Error:
groovy.lang.MissingMethodException: No signature of method: toDateTime() is applicable for argument types: (java.util.Date) values: [Tue Jul 07 17:51:48 EDT 2020]

My response: great, it's recognizing it as a date already. So, I change the code to this:
Date time = presenceInfo.atTime

But this gives this error:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '2020-07-07T18:48:00+0000' with class 'java.lang.String' to class 'java.util.Date' on line 1100

Ugh. So which is it, a date or a string stored in state? And how do I get back a Date object?

                def varname = Date.parse("E MMM dd H:m:s z yyyy", state.FinishTime)

That gives me
java.text.ParseException: Unparseable date: "2020-07-07T18:48:00+0000"

So does using "yyyy-MM-dd'T'HH:mm:ss'Z'"

So here is what my date looks like in the settings under App state:

here is how I set it:

def TimeToRun = 60*60*RunTime.toInteger()
 
use (groovy.time.TimeCategory) {  
    futureTime = now + TimeToRun.seconds
}
debuglog "TimeToRun: ${TimeToRun}"
debuglog "future: ${futureTime}"
state.FinishTime = futureTime.toString()

Here is how I get it back and use it:

                def now = new Date()
                def previousFinish = Date.parse("E MMM dd H:m:s z yyyy", state.FinishTime)
                    
                 
                if (now.before(previousFinish) && GotoPreviousFinish)
                {
                    debuglog "turning on pump will finish at ${state.FinishTime}"
                    runOnce(previousFinish, TurnOffPoolSwitch)

I am definitely not an expert on groove date/time stuff though.

state can only store particular types of objects, and I'm not sure Date is one of them (or at least this is the case on ST; again, I'm not 100% about Hubitat or if this is documented anywhere, though the above seems consistent with this assumption). Have you considered using epoch time instead of a Date? This will get you a long, which you can store and retrieve in/from state directly (or, again, at least you can on ST). Here is some documentation: Groovy - getTime() - Tutorialspoint, but the general idea is that if you have a date, you can call .getTime() on it and you'll get a long.

I am also not an expert and always find things like dates/times to be messy, so I'm not saying this is necessarily a better approach. :slight_smile: It does seem a bit less awkward than storing a Date as a string and needing to parse one back out of it later, though, plus if you need to do math on it, sometimes it's easier to add/subtract milliseconds instead of figuring out which date/time methods are available, not deprecated, and actually do what you're looking for.

2 Likes

As @bertabcd1234 mentioned, look at using epoch time formats. Its just one big number that represents the number of seconds since a specific date/time. I usually find it much easier for doing things like time comparisons in my apps.

1 Like