DateTime Conversion (openHAB 3.x)

yes, thanks this works! Perfect.

Hi, your suggestion does not work for me:

val LocalDateTime testtime1 = LocalDateTime.now() 
val DateTimeType testdtt1 = new DateTimeType(testtime1)

is throwing an error on the second code line, whereas:

val ZonedDateTime testtime2 = ZonedDateTime.now() 
val DateTimeType testdtt2 = new DateTimeType(testtime2)

works fine.

Hello, now Iā€™ve managed to get my holiday script to work. Now I get an error in the log
Script execution of rule with UID ā€˜holly-1ā€™ failed: Text ā€˜2021-4-4T00: 00: 00.000Zā€™ could not be parsed at index 5 in holly
Please help

Itā€™s not a standard datetime form. 4 / 04
Where does it come from? What is trying to parse it?

from the Holiday Script with the call
var LocalDate easterSunday = ZonedDateTime.parse(year + ā€œ-ā€ + month +"-" + day + ā€œT00:00:00.000Zā€).toLocalDate()

Thereā€™s a solution in this earlier post

Thanks, then the script died.
In the meantime, I would like to have the Joda time back.
a call, and not such a complicated conversion for a script

Joda time will not parse ā€˜2021-4-4T00: 00: 00.000Zā€™ either.

These variables are strings, yes?
@m4rk method -

var year = "2021"
var day = "4"
var month = "4"
//Day
if (day.length == 1) {
   day = "0" + day
}
//Month
if (month.length == 1) {
   month = "0" + month
}
var easterSunday = ZonedDateTime.parse(year + "-" + month +"-" + day + "T00:00:00.000Z").toLocalDate
logInfo("date", "date {}", easterSunday)
   //outputs date 2021-04-04

Script execution of rule with UID ā€˜holly-1ā€™ failed: ā€˜lengthā€™ is not a member of ā€˜intā€™; line 21, column 5, length 10 in holly

that was the call in OH 2 and the script ran
var org.joda.time.DateTime easterSunday = parse (year + ā€œ-ā€ + month + ā€œ-ā€ + day)

So they are not strings, but integers. Itā€™s quite hard to fix rules that we canā€™t see and you do not tell us what the moving parts are.

If you would like to go back to using OH2, you can. It hasnā€™t gone away and it will not stop working.

So, if your values are integers we can use a completely different approach.

var year = 2021
var day = 4
var month = 4
var easterSunday=LocalDate.now.withYear(year).withMonthOfYear(month).withDayOfMonth(day)
logInfo("date", "date {}", easterSunday)
  //outputs date 2021-04-04

holiday.script.txt (3.7 KB)

Yesā€¦ strings
My original code checks how many characters there are in the day and month strings. I wrote that code out of frustration. I could not get the formatter method I used in OH2 to work in OH3.

If there its a 1 character string then the string needs a 0 added as the lead character. Length function operates on strings and returns the number of characters in the stringā€¦

First step was to build a timestamp string into the iso format. This involved breaking apart the non iso format string I had and reassembling into iso format string. The variables used to do this have str or number appended to the name to make it clear if its a string or number.

After the iso timestamp string is built then you can convert it to other time types. Sometimes you might need an intermediary conversion as in my example.

withMonthOfYear (month) does not like int already it starts

Yes it does. Youā€™ll have to give us more meaningful messages to get help.

I uploaded the complete script to you as a txt file.

Yes, it contains this

var int year = now.getYear
...
var int month = (h + L - 7 * m + 114) / 31
var int day = ((h + L - 7 * m + 114) % 31) + 1

var easterSunday =  ZonedDateTime.parse(year+"-"+month+"-"+day).toLocalDate()

and we know that fails because of 4 / 04 difference.

Iā€™ve already suggested to use instead

var easterSunday=LocalDate.now.withYear(year).withMonthOfYear(month).withDayOfMonth(day)

which should work with the int types.

You said

but I donā€™t understand what is not to like or what starts.

EDIT - Iā€™ll take a guess that month is not an int at all anymore because of the division in the calculation.
Try this line instead, to make sure

var easterSunday=LocalDate.now.withYear(year.intValue).withMonthOfYear(month.intValue).withDayOfMonth(day.intValue)

The same error occurs with intValue
what I donā€™t understand, year and day are also int
and there I donā€™t get an error. only at
withMonthOfYear

If only we could see what the error was. This is hard work.

I guess by assembling the
The error comes months and days

So the first hurdle is over
var easterSunday = LocalDate.of (year, month, day)

So the script runs through no more errors
If interested, please provide information

1 Like