From 8a730ff630457b87d6367cf4259ad249cbfbd7da Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Mon, 25 Nov 2019 21:34:10 +0100 Subject: [PATCH] Use DateTimeFormatter in favor of SimpleDateFormat See gh-19142 --- .../boot/actuate/management/HeapDumpWebEndpoint.java | 6 +++--- .../management/PlainTextThreadDumpFormatter.java | 8 ++++---- .../org/springframework/boot/info/GitProperties.java | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/HeapDumpWebEndpoint.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/HeapDumpWebEndpoint.java index c2f9fee1fb..51d64b258c 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/HeapDumpWebEndpoint.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/HeapDumpWebEndpoint.java @@ -27,8 +27,8 @@ import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; import java.nio.file.Files; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -106,7 +106,7 @@ public class HeapDumpWebEndpoint { } private File createTempFile(boolean live) throws IOException { - String date = new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date()); + String date = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm").format(LocalDateTime.now()); File file = File.createTempFile("heapdump" + date + (live ? "-live" : ""), ".hprof"); file.delete(); return file; diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/PlainTextThreadDumpFormatter.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/PlainTextThreadDumpFormatter.java index 50e801c249..80569a7967 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/PlainTextThreadDumpFormatter.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/management/PlainTextThreadDumpFormatter.java @@ -23,8 +23,8 @@ import java.lang.management.ManagementFactory; import java.lang.management.MonitorInfo; import java.lang.management.RuntimeMXBean; import java.lang.management.ThreadInfo; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -47,8 +47,8 @@ class PlainTextThreadDumpFormatter { } private void writePreamble(PrintWriter writer) { - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - writer.println(dateFormat.format(new Date())); + DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + writer.println(dateFormat.format(LocalDateTime.now())); RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean(); writer.printf("Full thread dump %s (%s %s):%n", runtime.getVmName(), runtime.getVmVersion(), System.getProperty("java.vm.info")); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java index 2f92ade60b..d07f6cf88d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/info/GitProperties.java @@ -16,9 +16,9 @@ package org.springframework.boot.info; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.time.Instant; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Properties; /** @@ -108,11 +108,11 @@ public class GitProperties extends InfoProperties { if (epoch != null) { return String.valueOf(epoch); } - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); + DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"); try { - return String.valueOf(format.parse(s).getTime()); + return String.valueOf(format.parse(s, Instant::from).toEpochMilli()); } - catch (ParseException ex) { + catch (DateTimeParseException ex) { return s; } }