Use DateTimeFormatter instead of SimpleDateFormat in RenderableDate - #3100
Conversation
| @Test | ||
| void writesToJsonInStringFormat() { | ||
| RenderableDate renderableDate = new RenderableDate(new Date(1729266504000L), null, null); | ||
| RenderableDate renderableDate = new RenderableDate(new Date(1729266504010L), null, null); |
There was a problem hiding this comment.
Seems a bit dodgy that it's necessary to change this for the test to continue passing. What's happening here?
There was a problem hiding this comment.
It's not necessary - added it to make the test more relevant. Default formatter truncates milliseconds, but the assertion won't show that because milliseconds part is just zeroes
There was a problem hiding this comment.
Another change I'd like to submit it so actually stop cutting milliseconds off by the default formatter
|
I'm concerned this is not a completely backwards-compatible change. To your knowledge, are there date/time patterns that will work differently post this change? |
|
DateTimeFormatter supports all the letter from the official reference: https://docs.wiremock.io/response-templating/dates-and-times#format-string-reference When it comes to parsing behaviour, generally, DateTimeFormatter is more strict by default For example:
SimpleDateFormat parses it correctly despite format expecting minimum two digits per day/month, the other throws To maximize compatibility, we could fall back to SimpleDateFormat if default DateTimeFormatter fails |
|
@leeturner WDYT? Seems like 4.x beta is a good place to make what probably amounts to a fairly minor set of breaking changes. |
I would say definitely 4.x beta |
|
A backward-compatible change that would mitigate the original issue would be to expand the default format to include milliseconds - no one would be forced to recreate ISO-8601 with milliseconds by hand (just like I did) |
Replaced
SimpleDateFormatwithDateTimeFormatterinRenderableDatefor the sake leveraging more user-friendly formatting.For example, if you use template like
{{now format="yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"}}to format"2025-07-07T15:23:11.123000Z", this is what you get:DateTimeFormatter:2025-07-07T15:23:11.123000Z(as expected)SimpleDateFormat:2025-07-07T15:23:11.000123Z(with milliseconds converted into microseconds)The new test fails when run against the SimpleDateFormat-based implementation:
Submitter checklist
#help-contributingor a project-specific channel like#wiremock-java