mirror of
https://gitee.com/dolphinscheduler/DolphinScheduler.git
synced 2024-12-02 04:08:31 +08:00
* [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 <460888207@qq.com>
This commit is contained in:
parent
a015aa8fdc
commit
ae4c3ab4e1
@ -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();
|
||||
}
|
||||
|
@ -262,11 +262,14 @@ public class JSONUtilsTest {
|
||||
|
||||
@Test
|
||||
public void dateToString() {
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
|
||||
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));
|
||||
@ -274,10 +277,13 @@ public class JSONUtilsTest {
|
||||
|
||||
@Test
|
||||
public void stringToDate() {
|
||||
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user