Substrings from DateTime variable now

Hi,
I need to extract substrings from system variable now.
In openhab log I can see the wole string
example:2020-05-10T15:47:51.376+02:00

I can make left substring from now
val YearNow = now.toString.left(4) …OK

but no on
val MonthNow = now.toString.substring(6,2) …NOK
val DayNow = now.toString.substring(9,2) …NOK

Result format for two internal rules variables:
val dateNow YYYYMMDD
val timeNow hh:mm

val dateNow = now.toString.left(4) + **now.toString.substring(6, 2) + now.toString.substring(9, 2)**
val timeNow = now.toString.substring(12, 4)

Whats wrong with substrings please ?

This also doesn"t works>

val dateNow = now.left(0, 4) + now.left(6, 2) + now.left(9, 2)
val timeNow = now.left(12, 4)

Thanks.
Jozef

  • Platform information: Openhab2 2.5.4 on Buster
  • Hardware: RPi4 4GB

You do know that now has methods like now.getMonth ?

And you can format datetimes like such as now.toString("yyyy-MM-dd'T'HH:mm:ss.SSSZ")

EDIT - the problem with the code you attempting is incorrect use of substring(x,y)
In java, both x and y are position index, not character counts.
So substring(12, 4) is nonsense … “get the characters from position 12 to 4”
You’d want substring(12, 12+4)

Just remember getMonth() returns 0-11 (January is 0)

Try this.

import java.text.SimpleDateFormat
import java.util.Date

 var SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd")
 val String strDate = sdf.format(new Date())
 sdf = new SimpleDateFormat("HH:mm")
 val String strTime = sdf.format(new Date())

Please one question:
How to substitute val values in curl statement ?
for example: dateNow and dayEnergy ?

executeCommandLine("curl@@https://pvoutput.org/service/r2/addoutput.jsp?key=MyAPIKey&sid=Mysideid&d=dateNow&c=dayEnergy)

Just replace : with %3A
google for urlencode

executeCommandLine("curl@@https://pvoutput ... ")

executeCommandLine(" this part is just a string ")

executeCommandLine(" this part " + "is still one string" + " made from parts")

var someStuff = "is still one string"
executeCommandLine(" this part " + someStuff + " made from parts")

OK thanks, this is right statement

ITEMS:

Number Wirken24h
Number Garten_TempMin
Number GartenTempMax
val dateNow = now.toString("yyyyMMdd")
val APIstring = "curl@@https://pvoutput.org/service/r2/addoutput.jsp?key=MyAPIKey&sid=MyStationID" + "&d=" + dateNow + "&c=" + Wirken24h.state + "&tm=" + Garten_TempMin.state + "&tx=" + Garten_TempMax.state
executeCommandLine(APIstring)