From 9362e183409f7e8ff51e7b16ec8c9c081ea3e656 Mon Sep 17 00:00:00 2001 From: Felix Schumacher Date: Tue, 12 Apr 2022 20:51:18 +0200 Subject: [PATCH] Replace java.util.Date with java.time based objects Part of #708 --- .../java/org/apache/jorphan/util/HeapDumper.java | 10 +++++----- .../java/org/apache/jorphan/util/ThreadDumper.java | 10 +++++----- .../src/main/java/org/apache/jmeter/NewDriver.java | 14 +++++++------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/jorphan/src/main/java/org/apache/jorphan/util/HeapDumper.java b/src/jorphan/src/main/java/org/apache/jorphan/util/HeapDumper.java index fffaf7c9b57..eef6b52e463 100644 --- a/src/jorphan/src/main/java/org/apache/jorphan/util/HeapDumper.java +++ b/src/jorphan/src/main/java/org/apache/jorphan/util/HeapDumper.java @@ -19,8 +19,9 @@ import java.io.File; import java.lang.management.ManagementFactory; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import javax.management.InstanceNotFoundException; import javax.management.MBeanException; @@ -159,10 +160,9 @@ public static String dumpHeap(boolean live) throws Exception { * @return the name of the dump file that was created * @throws Exception if the MXBean cannot be found, or if there is a problem during invocation */ - @SuppressWarnings("JavaUtilDate") public static String dumpHeap(File basedir, boolean live) throws Exception { - SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyyMMdd_hhmmss_SSS"); - String stamp = timestampFormat.format(new Date()); + DateTimeFormatter timestampFormat = DateTimeFormatter.ofPattern("yyyyMMdd_hhmmss_SSS").withZone(ZoneId.systemDefault()); + String stamp = timestampFormat.format(Instant.now()); File temp = new File(basedir,"dump_"+stamp+".hprof"); final String path = temp.getPath(); dumpHeap(path, live); diff --git a/src/jorphan/src/main/java/org/apache/jorphan/util/ThreadDumper.java b/src/jorphan/src/main/java/org/apache/jorphan/util/ThreadDumper.java index 3f572a22e35..d97cb658706 100644 --- a/src/jorphan/src/main/java/org/apache/jorphan/util/ThreadDumper.java +++ b/src/jorphan/src/main/java/org/apache/jorphan/util/ThreadDumper.java @@ -27,8 +27,9 @@ import java.lang.management.ThreadInfo; import java.lang.management.ThreadMXBean; import java.nio.charset.StandardCharsets; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; /** * Utility class to create a Thread Dump @@ -56,10 +57,9 @@ public static String threadDump() throws Exception { * @return Name of file containing thread dump * @throws Exception if file cannot we written */ - @SuppressWarnings("JavaUtilDate") public static String threadDump(File basedir) throws Exception { - SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyyMMdd_hhmmss_SSS"); - String stamp = timestampFormat.format(new Date()); + DateTimeFormatter timestampFormat = DateTimeFormatter.ofPattern("yyyyMMdd_hhmmss_SSS").withZone(ZoneId.systemDefault()); + String stamp = timestampFormat.format(Instant.now()); File temp = new File(basedir,"thread_dump_"+stamp+".log"); final String path = temp.getPath(); try (FileOutputStream fos = new FileOutputStream(temp); diff --git a/src/launcher/src/main/java/org/apache/jmeter/NewDriver.java b/src/launcher/src/main/java/org/apache/jmeter/NewDriver.java index 91d609c5f7a..95a53501543 100644 --- a/src/launcher/src/main/java/org/apache/jmeter/NewDriver.java +++ b/src/launcher/src/main/java/org/apache/jmeter/NewDriver.java @@ -18,6 +18,7 @@ package org.apache.jmeter; // N.B. this must only use standard Java packages + import java.io.File; import java.io.IOException; import java.io.PrintWriter; @@ -25,10 +26,11 @@ import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; -import java.text.SimpleDateFormat; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; -import java.util.Date; import java.util.List; import java.util.StringTokenizer; @@ -344,19 +346,17 @@ private static String getCommandLineArgument(String[] args, int id, String name) /* * If the fileName contains at least one set of paired single-quotes, reformat using DateFormat */ - @SuppressWarnings("JavaUtilDate") private static String replaceDateFormatInFileName(String fileName) { try { StringBuilder builder = new StringBuilder(); - // TODO: replace with java.time.* - final Date date = new Date(); + final Instant date = Instant.now(); int fromIndex = 0; int begin = fileName.indexOf('\'', fromIndex);// $NON-NLS-1$ int end; String format; - SimpleDateFormat dateFormat; + DateTimeFormatter dateFormat; while (begin != -1) { builder.append(fileName.substring(fromIndex, begin)); @@ -368,7 +368,7 @@ private static String replaceDateFormatInFileName(String fileName) { } format = fileName.substring(begin + 1, end); - dateFormat = new SimpleDateFormat(format); + dateFormat = DateTimeFormatter.ofPattern(format).withZone(ZoneId.systemDefault()); builder.append(dateFormat.format(date)); fromIndex = end + 1;