DateTime Calulation

I have an item with a date time stamp in this format:

I need to calculate de difference in seconds or minutes from now and put value in new item.
How can I perform this?

Many Thanks

Holy cow that’s a pain.

I don’t have time right now to code and test this but I can give you an outline of one way to go about it.

Assuming this is a big String you first need to break it apart. You can do this using the String.split method:

val dtParts = date.split(",")

This will create an array and each part of that big string will be a separate element of the array.

Then make sure you have imported org.joda.time.* so we are using the same DateTime classes.

Create a new MutableDateTime object.
val myMutableDT = new DateTime().toMutableDateTime

Then you will need to get each part of the date (year, month, day, hour, minute, seconds, etc) from the array and pull out the number. To do this you need to count, starting from zero, the number of fields until you get to the one you want (e.g. YEAR). Lets say it is 20. You then need to split is again on the ‘=’ and parse the second field into an Integer.

val int year = new Integer(dtParts[20].split("=")[1]).intValue

Do this for all the parts of the date time. Then you need to set that value on myMutableDT.


Do this for all the parts.

Put in lots of logging so you are sure you are getting the right parts and everything is parsing correctly.

Once all the fields are set it is easiest to then convert the DateTimes to epoc and subtract the milliseconds and then convert that to seconds and minutes.

val difference = myMutableDT.millis - now.millis
val diffSecs = difference / 1000 // total number of minutes
val minutes = diffSecs / 60 // number of minutes
val seconds = diffSecs % 60 // remainder seconds

Assuming your Item is a String called Difference you can then put the minutes and seconds into that Item with a postupdate:

Difference.postUpdate(minutes + ":" + seconds)

NOTE: The above may have errors. I did not type it into Designer to make sure the syntax is right.

I highly recommend using Designer and <ctrl><space> to complete your statements and learn what methods are available on the various objects. Particularly when doing stuff like this.

1 Like

Hi @rlkoshak,
and thanks for help.
I just look into split method, but I ask because of this.
Using this trasformation in items:

String SOULISS_TIME_PS004 "PowerSocket Fan Mansarda [%1$td.%1$tm.%1$tY %1$tk:%1$tM:%1$tS]"

I get my string formatted into sitemap like 10/11/2015 10:10:55
For this reason I asked if there is a way to calculate diff, without using split.

Probably. But split is how I would approach the problem, given the limited amount of information I have. There are always more than one way to do things but the only information you provided was that you get that big long String so that is how I solved it. If you are getting the data as a DateTimeType, which could be the case given that you can use the %1t transforms then that is something else entirely. I don’t have nor have I ever used the Souliss binding so I have no idea.

hi @rlkoshak
this value is passed by a binding, Souliss binding.
I try to ask developer what kind of value is the big string and if is possible to manipulate.
Thanks for help. In case I ask here I I need more help

OH item receive this update from Souliss binding:

Into item file:
String SOULISS_TIMESTAMP “Updated [%1$td.%1$tm.%1$tY %1$tk:%1$tM:%1$tS]” {souliss=“D99:2:999”}

and sitemap:

Let me know.

hi @rlkoshak
@fazioa is the developer of the binding.
Can you help US on this?


I really have no idea what is going on here.

Where did that initial screen grab timestamp string come from?

What are the types of the data that is coming back? Is SOULISS_TIMESTAMP a String or a DateTime?

I really have no idea how Souliss works and from looking at the wiki I have no better understanding what the data types are coming back.

If in a rule you put:

logInfo("rules", "SOULISS_TIMESTAMP = " + SOULISS_TIMESTAMP.state.toString)

what gets printed into the logs?

Hi @rlkoshak
the result is:

SOULISS_TIMESTAMP = 2015-11-25T10:49:52

it is possible to calculate difference from now in seconds or minutes?


If you’re working in a rule, something like this might work:

var t1 = (SOULISS_TIMESTAMP.state as DateTimeType).calendar.timeInMillis
var DateTime t2 = new DateTime()
var DateTime diff = t2.minus(t1)
// process the diff value here...

I’ve not tested this; it’s based on another conversation about datetime processing…

Hi all,
and thenks for help.
Here is my working code for future reference. I added a try-catch to grab conversion error if string is not a DateTime:

var DateTime adesso = now //Power Socket 004 var Integer pws004_diff = 0 try { var DateTime dt_pws004 = new DateTime(SOULISS_TIME_PS004.state.toString) pws004_diff = (adesso.millis - dt_pws004.millis) / 1000 }catch (Exception exc) { pws004_diff = -1 } SOULISS_UPD_SINCE_PS004.postUpdate(ing_diff)

1 Like