Problem Converting DayTime

After searching and try & error for “hours” I would like to ask for help

I need to “produce” a string in a certain DateTime format and I need to manipulate the value e.g. minusHours(+2)

I need to produce something like that 2018-08-29T22:00:00.000Z
and not something like 2019-07-17T19:10:03.657+02:00

I have a DateTime Item:

DateTime Gardena_Water_Front_Paused

E.g. now minus 2 hours, this works fine

but trying to manipulate the value and get the format I want, I ended up in something complicated

**Gardena_Water_Front_Paused.state as DateTimeType).zonedDateTime.minusHours(2).toString.substring(0,19)+".000Z" **

Is there a way to get this more easily ?

thx for all hints :slight_smile:

There are several things to realize here:

  1. DateTimeType is not the same as DateTime. DateTimeType is the state a DateTime Item carries. DateTime is the type for now.

  2. In both cases, the actual date/time value is stored as epoch, the number of milliseconds since 1970-01-01 00:00. So how the date time looks when you convert it to a String is completely independent from the value of the date/time.

So a question to you is where do you want the date time String to look like 2019-07-17T19:10:03.657+02:00? In your logs? On your Sitemap? HABPanel?

If it’s the Sitemap or HABPanel, then you define the way the DateTime Item looks in the label. The label can be formatted using So to make a DateTime Item appear as you ask for on the Sitemap you would use

"Paused at [%1$tFT%1$tT.%1$tL.%1$tz]"

So you would have your Gardena_Water_Front_Paused Item defined as a DateTime Item using a label like the above. Then to populate it just like you are doing now using now.minusHours(2).toString.

Hi Rich

I’m always struggling with DateTime handling in OpenHab :frowning:

At a different line of the rule (or even by a different rule) I set the item e.g.

  • Gardena_Water_Front_Paused.postUpdate(now.withTimeAtStartOfDay.plusHours(8).plusDays(1).toString)

depending on different things, I want to execute a command line:

  • response = executeCommandLine(’/etc/openhab2/scripts/ ‘+location_id+’ ’ +token+’ ’ + Device_Id +’ ’ + smart_control_devices_settings_id.get(Device_No)+ ’ ’ + (Gardena_Water_Front_Paused.state as DateTimeType).zonedDateTime.minusHours(2).toString.substring(0,19)+".000Z" ,5000)
  • only the exact format “2018-08-29T22:00:00.000Z” is accepted by the api (must be .000Z at the end)

unfortunately the gardena API uses a different time zone, so I’ve to add 2h

=> I would prefer using the item and not adding a additional variable, but maybe this would be easier ?

If you need to pass the String to some Item in that format, then you will need to do some String manipulation of some sort. You will either have to define the format for the date time string or do the minor little bit of String manipulation to get the right time.

This might be as good as it gets.