Hi there,
I need your help - I want to parse use a timestamp which is the state of a String Item (my next alarm) as a timer.
However, I constantly struggle with types and I don’t understand why it doesn’t work:
not-working
import java.util.Date
import java.text.SimpleDateFormat
var Timer timer = null
rule "set alarm"
when Item alarmtime received update
then
var sdf = new SimpleDateFormat("yyyy-M-d HH:mm")
This file has been truncated. show original
workaround
import java.util.Date
import java.text.SimpleDateFormat
var Timer timer = null
var actual_format = new SimpleDateFormat("yyyy-M-d HH:mm") // 2017-1-2 7:30
var iso = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") // for intermediary conversion
rule "set alarm"
when Item alarmtime received update
then
This file has been truncated. show original
Results in:
2017-02-09 08:47:30.714 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'set alarm': An error occured during the script execution: Could not invoke method: org.joda.time.base.AbstractDateTime.toString() on instance: Fri Feb 10 08:27:00 CET 2017
The time seems to be parsed correctly - can anyone tell me what I’m doing wrong?
rossko57
(Rossko57)
February 9, 2017, 12:35pm
2
Aren’t you trying .toString on something already a string?
No, this is needed apparently as openhab cannot infer the type.
As you can see in the error it also worked: Fri Feb 10 08:27:00 CET 2017
is the parsed input - input was “2017-02-10 8:27”
Mherwege
(Mark Herwege)
February 9, 2017, 1:21pm
4
@Christoph_B I don’t think you need sdf. Can you try this:
rule "set alarm"
when Item alarmtime received update
then
if(alarmtime.state != null) {
var DateTime mydate = parse(alarmtime.state.toString)
logInfo("test", mydate.toString)
if(timer == null) {
...
I need the initial format to define how the incoming string looks like.
I created a workaround:
parse timestamp in custom format using formatter
output in ISO using iso formatter
parse again with .parse
not-working
import java.util.Date
import java.text.SimpleDateFormat
var Timer timer = null
rule "set alarm"
when Item alarmtime received update
then
var sdf = new SimpleDateFormat("yyyy-M-d HH:mm")
This file has been truncated. show original
workaround
import java.util.Date
import java.text.SimpleDateFormat
var Timer timer = null
var actual_format = new SimpleDateFormat("yyyy-M-d HH:mm") // 2017-1-2 7:30
var iso = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") // for intermediary conversion
rule "set alarm"
when Item alarmtime received update
then
This file has been truncated. show original
So it seems to me that formatter.parse() returns something different than .parse()
Can anyone explain this?