Loss of Accuracy when Parsing Strings with Milliseconds to DateTimes

Hi everyone,
I have a strange situation where I convert a string i.e. "2025-09-02T13:05:07.832" to a java.util.date object and the resulting datetime is off + or - by a few minutes.

When I remove the milliseconds from the string before parsing, the resulting datetime is accurate.

I did some investigating into the matter. There is support for parsing milliseconds via SimpleDateFormat but it seems that Jython in Designer for Ignition Platform 8.1.48 does not handle it well.

Has anyone encountered this or something similar before?

How are you parsing it?

Are you using system.date.parse()?

2 Likes

I'm using SimpleDateFormat in the following manner:

from java.text import SimpleDateFormat

time_str = "2026-01-13T09:17:00.744"
sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
java_date = sdf.parse(time_str)

I'm using SimpleDateFormat because I need to accurately convert it to any timezone later with an arbitrary GMT offset. If there is some way to do this with system.date, I'm open to it.

Consider using DateTimeFormatter instead of SimpleDateFormat, as the latter interprets in the JVM's timezone. (java.util.Date is always UTC under the hood-it doesn't have a timezoneless form.)

The former's parse result can be fed to LocalDateTime.from(...) to instantiate a true timezoneless datetime object, which can then be fed to ZonedDateTime.of(..., ZoneId z) to get a zoned datetime that is independent of the JVM timezone.

You convert the ZonedDateTime back to Date via Instant (to use in Ignition APIs).

Works fine for me:

from java.text import SimpleDateFormat

time_str = "2026-01-13T09:17:00.744"
sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS")
java_date = sdf.parse(time_str)

print time_str
print sdf.format(java_date)
print java_date.time
>>> 
2026-01-13T09:17:00.744
2026-01-13T09:17:00.744
1768324620744

Note that, some argument handling boilerplate aside, this is literally what system.date.parse is doing internally:

2 Likes

You mean system.date.parse() correct?

1 Like