aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorZipeng WU <zipeng.wu@sonarsource.com>2020-12-11 09:29:48 +0100
committersonartech <sonartech@sonarsource.com>2020-12-14 20:07:14 +0000
commit72b9d7e8614eb89c4d293c4e47177caeea2cd55a (patch)
treeaf90d0c1628ac6038a8d0282e9532bb878a9f3b3 /sonar-core
parent55beb4250989be32c957a61b80321f6d2c4528a6 (diff)
downloadsonarqube-72b9d7e8614eb89c4d293c4e47177caeea2cd55a.tar.gz
sonarqube-72b9d7e8614eb89c4d293c4e47177caeea2cd55a.zip
Use thread safe date time API
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/util/UtcDateUtils.java22
1 files changed, 8 insertions, 14 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/util/UtcDateUtils.java b/sonar-core/src/main/java/org/sonar/core/util/UtcDateUtils.java
index b1aaa816207..5c955ee5886 100644
--- a/sonar-core/src/main/java/org/sonar/core/util/UtcDateUtils.java
+++ b/sonar-core/src/main/java/org/sonar/core/util/UtcDateUtils.java
@@ -19,35 +19,29 @@
*/
package org.sonar.core.util;
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
+import java.time.OffsetDateTime;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
import java.util.Date;
-import java.util.TimeZone;
-
import org.sonar.api.utils.DateUtils;
public class UtcDateUtils {
- private static final ThreadLocal<DateFormat> format =
- ThreadLocal.withInitial(() -> {
- DateFormat f = new SimpleDateFormat(DateUtils.DATETIME_FORMAT);
- f.setTimeZone(TimeZone.getTimeZone("UTC"));
- return f;
- });
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.DATETIME_FORMAT).withZone(ZoneOffset.UTC);
private UtcDateUtils() {
// only static stuff
}
public static String formatDateTime(Date date) {
- return format.get().format(date);
+ return formatter.format(date.toInstant());
}
public static Date parseDateTime(String s) {
try {
- return format.get().parse(s);
- } catch (ParseException e) {
+ return Date.from(OffsetDateTime.parse(s, formatter).toInstant());
+ } catch (DateTimeParseException e) {
throw new IllegalArgumentException("Fail to parse date: " + s, e);
}
}