Skip to content

Commit 1a4d28d

Browse files
committed
Avoid reuse of not thread-safe SimpleDateFormat
SimpleDateFormat.format(new Date()); returns same string as immutable and thread-safe DateTimeFormatter.format(new Date().toInstant())
1 parent a409782 commit 1a4d28d

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

examples/org.eclipse.swt.examples.watchdog/src/org/eclipse/swt/examples/watchdog/TimedEventWatchdog.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import org.eclipse.swt.widgets.Synchronizer;
2323

2424
import java.text.SimpleDateFormat;
25+
import java.time.Instant;
26+
import java.time.ZoneId;
27+
import java.time.format.DateTimeFormatter;
2528
import java.util.Date;
2629
import java.util.Timer;
2730
import java.util.TimerTask;
@@ -109,7 +112,7 @@ private class StackNode {
109112
public long startTime = -1;
110113
}
111114

112-
private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss.SSS");
115+
private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss.SSS").withZone(ZoneId.systemDefault());
113116

114117
private static final String EVENT_STR_FORMAT =
115118
"Event #%1$d-#%2$d: %3$dms from %4$s [depth = %5$d, max = %6$d]";
@@ -276,12 +279,12 @@ public void onLongEvent(LongEventInfo event) {
276279
if (trace != null) {
277280
String msg = String.format(EVENT_STR_FORMAT, event.startingSequenceNumber,
278281
event.endingSequenceNumber, event.duration,
279-
TIME_FORMAT.format(new Date(event.start)), event.depth, event.maxDepth);
282+
TIME_FORMAT.format(Instant.ofEpochMilli(event.start)), event.depth, event.maxDepth);
280283

281284
StringBuilder str = new StringBuilder(msg);
282285

283286
str.append('\n');
284-
str.append('\t').append("Trace ").append(TIME_FORMAT.format(trace.captureTime));
287+
str.append('\t').append("Trace ").append(TIME_FORMAT.format(trace.captureTime.toInstant()));
285288

286289
// Calculate when the stack trace happened relative to the start of the dispatch.
287290
double deltaTimeFromEventStart = trace.captureTime.getTime() - event.start;

0 commit comments

Comments
 (0)