Strugeling with time formating on string

Hello all,

I want to store in a String Item the last alarm which was received by OH Item updates

Item:
String LastAlarmEvent “Dummy text”

What I can do by a rule:
LastAlarmEvent.label = “Last alarm event tiggered: [10.05.2017 16:30 Uhr " + wugTemp_Min.state.format(”%.1f °C")]"
This will change the label of the string item perfectly.

But I want to add the current date/time as well. I tried a lot but nothing really works

LastAlarmEvent.labell = “Last alarm event tiggered: [” + now.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS") + wugTemp_Min.state.format("%.1f °C") + “]”

LastAlarmEvent.label = “Last alarm event tiggered: [” + String::format( “%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS”, now ) + wugTemp_Min.state.format("%.1f °C") + “]”

val DateTime MoveMoment = now
LastAlarmEvent.label = “Last alarm event tiggered: [” + String::format( “%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS”, MoveMoment ) + wugTemp_Min.state.format("%.1f °C") + “]”

var SimpleDateFormat df = new SimpleDateFormat( “dd.MM., HH:mm” )
does not work at all, because SimpleDateFormat not known (maybe I have to import anything in the rule file?)

Every time my date/time syntax seems to be wrong. Any idea?

It can be solved by using this one:
LastAlarmEvent.label = “Last alarm event tiggered: [” + String::format( “%1$td.%1$tm.%1$tY %1$tH:%1$tM “, new Date() ) + wugTemp_Min.state.format(”%.1f °C”) + “]”

But I have to add
import java.util.Date
to the rule file

How to use joda in rule?

What specifically doesn’t work. Does the date time not appear at all, doesn’t appear correctly, generates an error?

The more typical way to do what you are after is not to modify the label directly but to modify the state of the String Item.

Also, now is a Joda datetime, not a java.util.DateTime so you can’t use them inter-changeably. Below is one of many ways to do that and it avoids String::format as I don’t think that works with Joda DateTime.

Item

String LastAlarmEvent "Last alarm event triggered: [%s]"

Rule

    var alarmTime = now
    var alarm = alarmTime.year + "-" + alarmTime.monthOfYear + "-" + alarmTime.dayOfMonth + " " +
                     alarmTime.hourOfDay + "/" + alarmTime.minuteOfHour + "/" + alarmTime.secondOfMinute + ", " +
                     AlarmItem.state.toString // what ever you do to generate the last part of the text
    LastAlarmEvent.postUpdate(alarm)

I just strugeling with the syntax. There is no real documentation which syntax is the right one. But I’m learning every day.