To sum up, Joda-Time and a headache

Hi,

I search between old openhab and new one forum but don’t have a solution.

Generally I can find a solution by reading on the wiki or forum but there I can’t.

My problem is around the time of an item. Exactly datetime type.

I need to only get hour or minute from an item.

For others who wants to know, the only thing I’m able to do is:

use a date time from an item which is called example_item:

example_item.state or
example_item.state as DateTimeType
That give a format like that: YYYY-MM-DDTHH:MM:SS.ZZ

Get a time in milliseconds from EPOCH until now:

new LocalTime().getLocalMillis()
That give a number in miliseconds

I can give a choice time in miliseconds:

new LocalTime(23, 0, 0, 0).getLocalMillis()
That give:
a number in miliseconds.

And that’s all.

So to sum up again, I want to only get minutes or hour or what ever from an item which format is Datetime.

I think it will be usefull to have more details in wiki about that.

Gilles.

It’s not clear if you want to format a DateTimeType with only specific fields or if you want to access those fields in code. If you are asking for the latter, here’s a simple rule that works for that purpose.

import org.openhab.core.library.types.DateTimeType
import java.util.Calendar

rule "date test"
when
    System started
then
    var ts = TestTimestamp.state as DateTimeType
    logInfo("TEST", "Timestamp={}", ts)
    var cal = ts.getCalendar()
    logInfo("TEST", "Hour={}", cal.get(Calendar::HOUR_OF_DAY))
    logInfo("TEST", "Minute={}", cal.get(Calendar::MINUTE))
    logInfo("TEST", "Second={}", cal.get(Calendar::SECOND))

Example output:

2015-12-04 10:52:44.130 [INFO ] [org.openhab.model.script.TEST ] - Timestamp=2015-12-04T01:02:03
2015-12-04 10:52:44.139 [INFO ] [org.openhab.model.script.TEST ] - Hour=1
2015-12-04 10:52:44.143 [INFO ] [org.openhab.model.script.TEST ] - Minute=2
2015-12-04 10:52:44.150 [INFO ] [org.openhab.model.script.TEST ] - Second=3

Note: I use Jython (JSR223) rules rather than the DSL, so there may be a better way to do this.

It’s more clear for me now, from your example. First, I want to access those fields.

With your example I can use hours and minutes to do some code for actions. Example do some additions to calculate the end of an action.

And I need to send time to users to know when an action begin or ended. It’s doable with you code too.

For my information, you know how formating DateTimeType with specific fields ?

Thank you for your information.

The easiest ways to discover the fields and methods available on a type is to use Designer, type in the name of a variable of that type and press <ctrl><space>. This will bring up a list of valid ways to complete the statement.

Hi,

You’re right, I never used the designner and It’s time for me to have a look as I wrote all my rules in command line directly.

Thank you for the tip.