DateTime Conversion (openHAB 3.x)

Tags: #<Tag:0x00007fe741e84ec0> #<Tag:0x00007fe741e84b00> #<Tag:0x00007fe741e846f0>

ToDo: #2, #4, #5, #6, #7

Because openHAB 2.x rules use Jodatime instead of Java Time API, there is still the old DateTime Conversion thread for openHAB 2.x.

Overview

In openHab 3.x there are different ways to handle Date/Time values.

  • DateTimeType
    A DateTime Item carries a DateTimeType.

  • Java Time
    By default openHAB 3.x-Rules use the Java Time class to represent time, most notably now.

  • Epoch
    The lowest common denominator when working with time is to get at the epoc value. Epoc is the number of milliseconds that has passed since 1 January 1970 GMT and stored in a long. With epoc, one can compare two dates together, convert a Joda DateTime to a DateTimeType and visa versa. With epoc you can represent a duration.

  • String
    In certain cases it is needed to convert date/time values to a human readable.

This flow chart is intended to help you easily find the code needed for conversion.
The numbers of the flow chart refer to the numbers of the following list.

#1 Get DateTimeType from String

String must be ISO 8601 formatted like this: “yyyy-MM-dd’T’HH:mm:ss.SSSZ”

val DateTimeType MyDateTimeTypeFromString = DateTimeType.valueOf(MyString) 

#2 Get DateTimeType from Epoch

Regarding to the flow chart this code converts Epoch => #6 => to Java Time => #11 => to String => #1 => to DateTimeType

ToDo …

#3 Get DateTimeType from Java Time

Because JavaTime and DateTimeType both accept ISO 8601 formatted date/time strings it is easy to convert from one to the other using their toString.
Regarding to the flow chart this code converts JavaTime => #11 => to String => #1 => to DateTimeType

Variant A

val DateTimeType MyDateTimeTypeFromJavaTime = DateTimeType.valueOf(MyJavaTime.toLocalDateTime().toString())

Timestamp (DateTimeType)

val DateTimeType MyDateTimeTypeTimestamp = DateTimeType.valueOf(now.toLocalDateTime().toString())

#4 Get Java Time from DateTimeType

ToDo …

#5 Get Java Time from String

ToDo …

#6 Get Java Time from Epoch

ToDo …

#7 Get Epoch from Java Time

ToDo …

#8 Get Epoch from DateTimeType

Varinat A

val Number MyEpochFromDateTimeTypeItem_VariantA = (MyDateTimeTypeItem.state as DateTimeType).zonedDateTime.toInstant.toEpochMilli

Varinat B

val Number MyEpochFromDateTimeTypeItem_VariantB = new DateTime(MyDateTimeTypeItem.state.toString).millis

#9 Get Epoch from String

String must be ISO 8601 formatted like this: “yyyy-MM-dd’T’HH:mm:ss.SSSZ”

val Number MyEpochFromString = new DateTime(MyDateTimeString).millis

#10 Get String from Epoch

Variant A
Regarding to the flow chart this code converts from Epoch => #6 => to Java Time => #11 => to String. This means formatting can be done like shown in #11

ToDo …

Variant B

Here is an option utilizing SimpleDateFormat:

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

val SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
val String MyStringFromEpoch_VariantB = sdf.format(new Date(MyEpoch))

#11 Get String from Java Time

Variant A

val String MyStringFromJavaTime = MyJavaTime.toLocalDateTime().toString()

Timestamp (String)

val String MyStringTimestamp = now.toLocalDateTime().toString()

#12 Get String from DateTimeType

Formatting can be done like shown here.
For more details look at this.

val String MyStringFromDateTimeType = MyDateTimeType_item.state.format("%1$td.%1$tm.%1$ty %1$tH:%1$tM")
5 Likes

I used the following two steps in 3.0.0.M2 to convert a Java Time (now) to a DateTimeType (no imports needed):
# 11 Get String from Java Time
val String dts = now.toLocalDateTime().toString()

Then, I converted the string to a DateTimeType:
# 3 Get DateTimeType from Java Time (using String)
val DateTimeType dt = DateTimeType.valueOf(dts)

1 Like

Because I’m still on oh 2.4 yet, I can’t test it. But I think you did.
Thanks a lot!