This is an automated email from the ASF dual-hosted git repository.
caishunfeng pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git
The following commit(s) were added to refs/heads/dev by this push:
new 062146eecd [Fix] Fix the JSONUtils tool class time zone problem #10282
(#10284)
062146eecd is described below
commit 062146eecd9ceabbd6d1fd32747372802749d6bd
Author: juzimao <[email protected]>
AuthorDate: Fri Jun 17 11:08:38 2022 +0800
[Fix] Fix the JSONUtils tool class time zone problem #10282 (#10284)
* [Fix] Fix the JSONUtils tool class time zone problem #10282
* [Fix] Fix the JSONUtils tool class time zone problem #10282
* Update JSONUtils.java
remove unnessnary log
* Update JSONUtilsTest.java
add timezone import
Co-authored-by: xiangzihao <[email protected]>
---
.../apache/dolphinscheduler/common/utils/JSONUtils.java | 4 ++++
.../dolphinscheduler/common/utils/JSONUtilsTest.java | 15 +++++++++++++--
2 files changed, 17 insertions(+), 2 deletions(-)
diff --git
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java
index dbd7d833b5..1a09ae11ef 100644
---
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java
+++
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java
@@ -82,6 +82,10 @@ public class JSONUtils {
throw new UnsupportedOperationException("Construct JSONUtils");
}
+ public static synchronized void setTimeZone(TimeZone timeZone) {
+ objectMapper.setTimeZone(timeZone);
+ }
+
public static ArrayNode createArrayNode() {
return objectMapper.createArrayNode();
}
diff --git
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java
index 6ff3c7dba5..472ae8783a 100644
---
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java
+++
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java
@@ -23,6 +23,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.TimeZone;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -260,10 +261,14 @@ public class JSONUtilsTest {
@Test
public void dateToString() {
+ TimeZone timeZone = TimeZone.getTimeZone("UTC");
+ TimeZone.setDefault(timeZone);
+ JSONUtils.setTimeZone(timeZone);
+
String time = "2022-02-22 13:38:24";
Date date = DateUtils.stringToDate(time);
String json = JSONUtils.toJsonString(date);
- Assert.assertEquals(json, "\"" + time + "\"");
+ Assert.assertEquals("\"" + time + "\"", json);
String errorFormatTime = "Tue Feb 22 03:50:00 UTC 2022";
Assert.assertNull(DateUtils.stringToDate(errorFormatTime));
@@ -271,9 +276,15 @@ public class JSONUtilsTest {
@Test
public void stringToDate() {
+ TimeZone timeZone = TimeZone.getTimeZone("UTC");
+ TimeZone.setDefault(timeZone);
+ JSONUtils.setTimeZone(timeZone);
+
String json = "\"2022-02-22 13:38:24\"";
Date date = JSONUtils.parseObject(json, Date.class);
- Assert.assertEquals(date, DateUtils.stringToDate("2022-02-22
13:38:24"));
+ Assert.assertEquals(DateUtils.stringToDate("2022-02-22 13:38:24"),
date);
+
}
+
}