Skip to content

Commit fa8957d

Browse files
committed
Replace SimpleDateFormat with DateTimeFormatter
Replace usages of the non-thread-safe date formatter with java.time's DateTimeFormatter and related APIs to improve thread-safety and correctness when parsing/formatting dates and time zones. Update calendar parameter conversion to use ZonedDateTime/Instant and adapt log timestamp formatting to use DateTimeFormatter. Removes unused imports and modernizes date handling to avoid concurrency issues introduced by SimpleDateFormat. Collectively decided to use `DateTimeFormatter` instead of `FastDateTime`. Signed-off-by: Nico Piel <nico.piel@hotmail.de>
1 parent d268252 commit fa8957d

2 files changed

Lines changed: 33 additions & 16 deletions

File tree

‎server/src/com/mirth/connect/client/core/api/providers/CalendarParamConverterProvider.java‎

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@
99

1010
packagecom.mirth.connect.client.core.api.providers;
1111

12-
importjava.lang.annotation.Annotation;
13-
importjava.lang.reflect.Type;
14-
importjava.text.ParseException;
15-
importjava.text.SimpleDateFormat;
16-
importjava.util.Calendar;
17-
1812
importjavax.inject.Singleton;
1913
importjavax.ws.rs.ProcessingException;
2014
importjavax.ws.rs.ext.ParamConverter;
2115
importjavax.ws.rs.ext.ParamConverterProvider;
2216
importjavax.ws.rs.ext.Provider;
17+
importjava.lang.annotation.Annotation;
18+
importjava.lang.reflect.Type;
19+
importjava.time.Instant;
20+
importjava.time.ZoneId;
21+
importjava.time.ZonedDateTime;
22+
importjava.time.format.DateTimeFormatter;
23+
importjava.time.format.DateTimeParseException;
24+
importjava.util.Calendar;
2325

2426
@Provider
2527
@Singleton
2628
publicclassCalendarParamConverterProviderimplementsParamConverterProvider {
27-
28-
privatestaticfinalSimpleDateFormatformat = newSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
29+
privatestaticfinalDateTimeFormatterFORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
2930

3031
@Override
3132
public <T> ParamConverter<T> getConverter(Class<T> rawType, TypegenericType, Annotation[] annotations) {
@@ -38,10 +39,16 @@ public T fromString(String value) {
3839
}
3940

4041
try {
41-
Calendardate = Calendar.getInstance();
42-
date.setTime(format.parse(value));
43-
return (T) date;
44-
} catch (ParseExceptione) {
42+
// Parse the incoming string as a ZonedDateTime using the given pattern
43+
ZonedDateTimezdt = ZonedDateTime.parse(value, FORMATTER);
44+
45+
Calendarcalendar = Calendar.getInstance(java.util.TimeZone.getTimeZone(zdt.getZone()));
46+
calendar.setTimeInMillis(zdt.toInstant().toEpochMilli());
47+
48+
@SuppressWarnings("unchecked")
49+
Tresult = (T) calendar;
50+
returnresult;
51+
} catch (DateTimeParseExceptione) {
4552
thrownewProcessingException(e);
4653
}
4754
}
@@ -51,7 +58,15 @@ public String toString(T value) {
5158
if (value == null) {
5259
returnnull;
5360
}
54-
returnformat.format(((Calendar) value).getTime());
61+
62+
Calendarcalendar = (Calendar) value;
63+
64+
// Build a ZonedDateTime from the Calendar (date/time + time zone)
65+
Instantinstant = calendar.toInstant();
66+
ZoneIdzoneId = calendar.getTimeZone().toZoneId();
67+
ZonedDateTimezdt = ZonedDateTime.ofInstant(instant, zoneId);
68+
69+
returnFORMATTER.format(zdt);
5570
}
5671
};
5772
}

‎server/src/com/mirth/connect/plugins/serverlog/ServerLogItem.java‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
packagecom.mirth.connect.plugins.serverlog;
1111

1212
importjava.io.Serializable;
13-
importjava.text.SimpleDateFormat;
13+
14+
importjava.time.ZoneId;
15+
importjava.time.format.DateTimeFormatter;
1416
importjava.util.Date;
1517

1618
importorg.apache.commons.lang3.StringUtils;
1719

1820
publicclassServerLogItemimplementsSerializable {
1921

20-
publicstaticSimpleDateFormatDATE_FORMAT = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
22+
publicstaticDateTimeFormatterDATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").withZone(ZoneId.systemDefault());
2123

2224
privateStringserverId;
2325
privateLongid;
@@ -123,7 +125,7 @@ public void setThrowableInformation(String throwableInformation) {
123125
publicStringtoString() {
124126
if (id != null) {
125127
StringBuilderbuilder = newStringBuilder();
126-
builder.append('[').append(DATE_FORMAT.format(date)).append("] ");
128+
builder.append('[').append(DATE_FORMAT.format(date.toInstant())).append("] ");
127129
builder.append(level);
128130
builder.append(" (").append(category);
129131
if (StringUtils.isNotBlank(lineNumber)) {

0 commit comments

Comments
 (0)