Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

final class RemoteConfigUtil {
Expand Down Expand Up @@ -53,15 +54,15 @@ static long convertToMilliseconds(String dateString) throws ParseException {
static String convertToUtcZuluFormat(long millis) {
// sample output date string: 2020-11-12T22:12:02.000000000Z
checkArgument(millis >= 0, "Milliseconds duration must not be negative");
SimpleDateFormat dateFormat = new SimpleDateFormat(ZULU_DATE_PATTERN);
SimpleDateFormat dateFormat = new SimpleDateFormat(ZULU_DATE_PATTERN, Locale.ENGLISH);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While adding Locale.ENGLISH fixes the immediate bug, SimpleDateFormat is an outdated and mutable API. Since this project supports Java 8+, it's highly recommended to migrate to the modern java.time API for date and time operations.

java.time.format.DateTimeFormatter is immutable and thread-safe. This means you can define formatters as static final constants and reuse them without needing ThreadLocal or synchronization. This makes the code cleaner, safer, and more performant.

For example, this method could be rewritten as:

// At class level:
private static final DateTimeFormatter ZULU_DATE_FORMATTER =
    DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'")
        .withZone(ZoneOffset.UTC);

// In convertToUtcZuluFormat method:
static String convertToUtcZuluFormat(long millis) {
    checkArgument(millis >= 0, "Milliseconds duration must not be negative");
    return ZULU_DATE_FORMATTER.format(Instant.ofEpochMilli(millis));
}

The original pattern yyyy-MM-dd'T'HH:mm:ss.SSS000000'Z' is correctly implemented with java.time's yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'. A similar refactoring can be applied to the other date utility methods in this class. For UTC_DATE_PATTERN, you can even use the pre-defined DateTimeFormatter.RFC_1123_DATE_TIME.

dateFormat.setTimeZone(UTC_TIME_ZONE);
return dateFormat.format(new Date(millis));
}

static String convertToUtcDateFormat(long millis) {
// sample output date string: Tue, 08 Dec 2020 15:49:51 GMT
checkArgument(millis >= 0, "Milliseconds duration must not be negative");
SimpleDateFormat dateFormat = new SimpleDateFormat(UTC_DATE_PATTERN);
SimpleDateFormat dateFormat = new SimpleDateFormat(UTC_DATE_PATTERN, Locale.ENGLISH);
dateFormat.setTimeZone(UTC_TIME_ZONE);
return dateFormat.format(new Date(millis));
}
Expand All @@ -78,15 +79,16 @@ static long convertFromUtcZuluFormat(String dateString) throws ParseException {
if (indexOfPeriod != -1) {
dateString = dateString.substring(0, indexOfPeriod);
}
SimpleDateFormat dateFormat = new SimpleDateFormat(ZULU_DATE_NO_FRAC_SECS_PATTERN);
SimpleDateFormat dateFormat =
new SimpleDateFormat(ZULU_DATE_NO_FRAC_SECS_PATTERN, Locale.ENGLISH);
dateFormat.setTimeZone(UTC_TIME_ZONE);
return dateFormat.parse(dateString).getTime();
}

static long convertFromUtcDateFormat(String dateString) throws ParseException {
// sample input date string: Tue, 08 Dec 2020 15:49:51 GMT
checkArgument(!Strings.isNullOrEmpty(dateString), "Date string must not be null or empty");
SimpleDateFormat dateFormat = new SimpleDateFormat(UTC_DATE_PATTERN);
SimpleDateFormat dateFormat = new SimpleDateFormat(UTC_DATE_PATTERN, Locale.ENGLISH);
dateFormat.setTimeZone(UTC_TIME_ZONE);
return dateFormat.parse(dateString).getTime();
}
Expand Down