From 8f52d870563c5a29b0ba1a43d544674bdf642680 Mon Sep 17 00:00:00 2001 From: huangguojie2024 <503601315@qq.com> Date: Fri, 24 Jan 2025 20:52:31 +0800 Subject: [PATCH] Fixed the assertion failure caused by different system time zone. --- .../manager/common/utils/DateUtilsTest.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/utils/DateUtilsTest.java b/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/utils/DateUtilsTest.java index 1cf0012c..42b9a3a4 100644 --- a/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/utils/DateUtilsTest.java +++ b/bigtop-manager-common/src/test/java/org/apache/bigtop/manager/common/utils/DateUtilsTest.java @@ -21,7 +21,9 @@ import org.junit.jupiter.api.Test; import java.sql.Timestamp; +import java.text.SimpleDateFormat; import java.util.Date; +import java.util.TimeZone; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -41,13 +43,20 @@ public void testFormatTimestamp() { @Test public void testFormatDate() { - Date date = new Date(1672545600000L); // 2023-01-01 12:00:00 + Date date = new Date(1672545600000L); + TimeZone systemTimeZone = TimeZone.getDefault(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + sdf.setTimeZone(systemTimeZone); + String expectedFormatted = sdf.format(date); String formatted = DateUtils.format(date); - assertEquals("2023-01-01 12:00:00", formatted); + assertEquals(expectedFormatted, formatted); + SimpleDateFormat sdfSystem = new SimpleDateFormat("yyyy-MM-dd"); + sdfSystem.setTimeZone(systemTimeZone); + String expectedCustomFormatted = sdfSystem.format(date); String customFormatted = DateUtils.format(date, "yyyy-MM-dd"); - assertEquals("2023-01-01", customFormatted); + assertEquals(expectedCustomFormatted, customFormatted); } @Test