Assign value from string to Joda time format

Tags: #<Tag:0x00007f2fab2fdeb0>

Dear all,

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!

See:

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)

Is this possible?

What is the problem with the constructor? It creates a DateTime object from your string. It has to be “new” because it is constructed at that point.

And what do you mean by “addressing the variable”?

Sorry, I come from C…

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)

Did you try it?

var DateTime Joda_Sunrise_Time
Joda_Sunrise_Time = new DateTime(Sunrise_Time.state.toString)

Or if you use examples from that topic Vincent linked:

var DateTimeType MyDateTimeTypeFromString = DateTimeType.valueOf(MyString)

And yes, you are using that same variable. You just assing a new DateTime object to it.

Ok, I got it.

This is new for me - good to know!

Plus, I simply had no idea how to call a static method. Another solution is to call the parse method from the class, not the instance

var DateTime Joda_Sunrise_Time
...
bla
I create a string myString here based on some calculations
bla
...
Joda_Sunrise_Time= DateTime.parse(myString)

@vzorglub, @gitMiguel: Thanks a lot for helping also at this late hour… :slight_smile:

1 Like

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.

1 Like

Thanks, good to know!