How convert a german date string to datetime format

  • Platform information:
    • Hardware: Pi3
    • OS: Raspbian Buster
    • Java Runtime Environment: Java™ SE Runtime Environment (build 1.8.0_201-b09)
    • openHAB version: 2.5.2-1

Hi,
i read via regex a date string in format “01.04.2020 07:39:48” (german date format) from a website.
How can i convert it to an openhab datetime?

thank you very much

This post might be helpful: DateTime Conversion

And this stackoverflow post shows how to work with SimpleDateFormat. Try the following in a rule:

val SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss")
val DateTime parsedDateTime = new DateTime(dateFormatter.parse(yourDateTimeStringGoesHere))

Please let me know if that did work - would be nice to know for the future :wink:

hi,
i also have read the thread DateTime Conversion but i dont find a solution for me. i only find soutions to convert from datetime to string or iso date string to datetime.
i have test the code and it gives me an error:
Rule 'Convert MAKinder_updateString to datetime': An error occurred during the script execution: null
the rule (i test with a fixed string):

rule "Convert MAKinder_updateString to datetime"
    when
            Item MAKinder_updateString received update
    then
    	val SimpleDateFormat dateFormatter = new SimpleDateFormat( "dd.MM.yyyy HH:mm:ss" )
    	val DateTime parsedDateTime = new DateTime(dateFormatter.parse("12.11.2001 20:37:47"))
    	//postUpdate(MAKinder_update, parsedDateTime)
    	//MAKinder_update.postUpdate( parsedDateTime )
    end

Hmm - i get the same error, even if I slightly modify the example like this:

rule "test time parsing"
when
    Item Scene_Test changed
then
    val SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss")
    val String dateTimeValue = dateFormatter.format(new Date("12.04.2019 14:09:07"))
    val DateTime parsedDateTime = new DateTime(dateFormatter.parse(dateTimeValue))
    logInfo("Test", "Test finished: " + parsedDateTime)
end

My first idea were missing imports but the following also doesn’t seem to help:

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

Also an additional Google search didn’t help…

Maybe we can invoke @rlkoshak, he is almost always very helpful with rule related problems (and many other problems :sweat_smile:) :thinking:

I’m really not all that great about DateTime conversions. But it looks like all you need to do is replace the . with - and the space with T to get to an ISO 8601 format which is what Joda DateTime likes. I’m not even certain the space needs to be replaced.

val parsedDateTime = new DateTime("12.11.2001 20:37:47".replace('.', '-').replace(' ', 'T'))

One thing to realize is SimpleDateFormat works with Java’s built in Date classes. DateTime comes from Joda, not Java’s internal classes so I doubt SimpleDateFormat would work.

1 Like

I am very happy for your help fex and rlkoshak. But

val parsedDateTime = new DateTime("12.11.2001 20:37:47".replace('.', '-').replace(' ', 'T'))

also gives me an error:
Invalid format: "12-11-2001T20:37:47" is malformed at "01T20:37:47"

Try without the replacing the space with the T. I don’t think it needs milliseconds but you can append “.000” to the end of the String to put some milliseconds on the end.

Tested a little bit more - the following works:

val String stringTime = "2019-04-12T14:09:07"
val DateTime dateTime = DateTime.parse(stringTime)

Means, if we have no luck with parsing a pattern of our choice we can manipulate the string to match existing patterns :thinking:

Means you have not only to replace dots with dashes and insert T for the space - you also have to rearrange your date to lead with the year and to finish with days (dd.MM.yyyy -> yyyy-MM-dd)

I totally missed the order.

OK, that’s easy enough.

val str = "12.11.2001 20:37:47"
val parts = str.split(" ")
val dateStr = parts.get(0)
val timeStr = parts.get(1)

val dateParts = dateStr.split(".")
val year = dateParts.get(2)
val month = dateParts.get(1) // it's ambiguous which is the month and which is the day
val day = dateParts.get(0)

val parsedDateTime = new DateTime(year+"-"+month+"-"+day+"T"+timeStr)
1 Like

Something like the following might get you the right format:

val String formattedTime = stringTime.substring(6, 9) + "-" + stringTime.substring(3, 4) + "-" + stringTime.substring(0, 1) + "T" + stringTime.substring(11)

Okay, that solution is less quick and dirty and much more readable - go with @rlkoshak’s code :sweat_smile:

Your’s is all on one line. In cases like this I prefer to be a little more verbose if it helps with clarity. Either solution is perfectly acceptable.

Yep, that’s why I prefer your solution. It has a clear structure and you can understand the code immediately :+1:
(Like you said - my solution should also do the trick, which is why I didn’t delete my post, but wouldn‘t be my preferred solution)

It works , i am happy. :slight_smile:
the code from rlkoshak gives me an error (i dont know why)
Error: Rule 'Convert MAKinder_updateString to datetime': 2
but the code from fex works.

	val String formattedTime = MAKinder_updateString.state.toString.substring(6, 10) + "-" + MAKinder_updateString.state.toString.substring(3, 5) + "-" + MAKinder_updateString.state.toString.substring(0, 2) + "T" + MAKinder_updateString.state.toString.substring(11)
	MAKinder_update.postUpdate( new DateTimeType(formattedTime) )

Big thanks to you fex and rlkoshak

1 Like

No problem, I had fun cracking that little nut :blush: