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
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:
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.
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.
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
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)
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)
Yep, that’s why I prefer your solution. It has a clear structure and you can understand the code immediately
(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.
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.