Skip to content

Commit d2ebaed

Browse files
adinauerclaude
andcommitted
perf(core): Replace Calendar with Date in DateUtils
Avoid constructing Calendar instances when DateUtils only needs the current epoch millis or a Date for an existing millis value. Date stores epoch millis without timezone state, so the returned values are unchanged while avoiding unnecessary Calendar allocation and field computation. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 06fb38b commit d2ebaed

3 files changed

Lines changed: 6 additions & 10 deletions

File tree

sentry/src/main/java/io/sentry/Breadcrumb.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ public Breadcrumb(@Nullable String message) {
555555
if (timestamp != null) {
556556
return (Date) timestamp.clone();
557557
} else if (timestampMs != null) {
558-
// we memoize it here into timestamp to avoid instantiating Calendar again and again
558+
// we memoize it here into timestamp to avoid creating a Date again and again
559559
timestamp = DateUtils.getDateTime(timestampMs);
560560
return timestamp;
561561
}

sentry/src/main/java/io/sentry/DateUtils.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package io.sentry;
22

3-
import static io.sentry.vendor.gson.internal.bind.util.ISO8601Utils.TIMEZONE_UTC;
4-
53
import io.sentry.vendor.gson.internal.bind.util.ISO8601Utils;
64
import java.math.BigDecimal;
75
import java.math.RoundingMode;
86
import java.text.ParseException;
97
import java.text.ParsePosition;
10-
import java.util.Calendar;
118
import java.util.Date;
129
import org.jetbrains.annotations.ApiStatus;
1310
import org.jetbrains.annotations.NotNull;
@@ -24,10 +21,9 @@ private DateUtils() {}
2421
*
2522
* @return the UTC Date
2623
*/
27-
@SuppressWarnings("JdkObsolete")
24+
@SuppressWarnings("JavaUtilDate")
2825
public static @NotNull Date getCurrentDateTime() {
29-
final Calendar calendar = Calendar.getInstance(TIMEZONE_UTC);
30-
return calendar.getTime();
26+
return new Date();
3127
}
3228

3329
/**
@@ -78,10 +74,9 @@ private DateUtils() {}
7874
* @param millis the UTC millis from the epoch
7975
* @return the UTC Date
8076
*/
77+
@SuppressWarnings("JavaUtilDate")
8178
public static @NotNull Date getDateTime(final long millis) {
82-
final Calendar calendar = Calendar.getInstance(TIMEZONE_UTC);
83-
calendar.setTimeInMillis(millis);
84-
return calendar.getTime();
79+
return new Date(millis);
8580
}
8681

8782
/**

sentry/src/test/java/io/sentry/DateUtilsTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class DateUtilsTest {
8686
val utcActual = convertDate(actual)
8787
val timestamp = utcActual.format(isoFormat)
8888

89+
assertEquals(millis, actual.time)
8990
assertEquals("2020-06-07T12:38:12.631Z", timestamp)
9091
}
9192

0 commit comments

Comments
 (0)