First of all I should say that my Java/Xtend programming experience is very limited.
I try to reuse a time variable in my rules. Thus, I initially defined it:
var DateTime Joda_Sunrise_Time
Now I want to assign a value to it later on in my rule file that comes from a string. But I only found examples where a string is called with the constructor, i.e.
Joda_Sunrise_Time = new DateTime(Sunrise_Time.state.toString)
Is there no method that I can call? The parse method could do the job but complains it is static. How can I proceed?
Thanks!
Thanks. But I took my first steps from this example and it always uses the “new…” approach which is a constructor - right? I want to adress the variable that I already created… something like
var DateTime Joda_Sunrise_Time
...
...
Joda_Sunrise_Time.ThisReadsTheStringAnConvertsToDateTime(Sunrise_Time.state.toString)
So I create a DateTime variable in the beginning, than at some point I get a string, and that string needs to change the variable to the date given in it. As I want to use the original DateTime variable - and not a new one - I cannot call the constructor (at least no in my C programming experience…)
Say
var DateTime Joda_Sunrise_Time
...
bla
I create a string myString here based on some calculations
bla
...
Joda_Sunrise_Time.ThisReadsTheStringAndConvertsToDateTime(myString)
That isn’t how “modern” garbage collected languages work.
You can reassign the original DateTime variable with a new one. The old one will be destroyed and removed from memory at some point because there is no more variable pointing at it.
The correct thing to do is ti create a new DateTime object using your String and assign it to your variable.
DateTime::parse(dateString)
However, note that this is functionally equivalent to calling new DateTime(dateString). It returns a new DateTime object and you would need to assign that to the variable too.