From 689278e511803497b2733ddc498009868ad94c6a Mon Sep 17 00:00:00 2001 From: faysalmehmood Date: Sun, 18 Sep 2022 21:58:59 +0500 Subject: [PATCH 1/3] =?UTF-8?q?#309=20=E4=BF=AE=E5=A4=8D=E6=97=B6=E5=8C=BA?= =?UTF-8?q?=E4=B8=AD=E6=96=AD=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E7=9A=84?= =?UTF-8?q?=E5=B7=AE=E5=BC=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/SaTokenListenerForConsolePrint.java | 9 ++++++--- .../main/java/cn/dev33/satoken/util/SaFoxUtil.java | 11 +++++++++++ .../cn/dev33/satoken/core/util/SaFoxUtilTest.java | 8 ++++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/listener/SaTokenListenerForConsolePrint.java b/sa-token-core/src/main/java/cn/dev33/satoken/listener/SaTokenListenerForConsolePrint.java index fc52dac8..4a7362a3 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/listener/SaTokenListenerForConsolePrint.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/listener/SaTokenListenerForConsolePrint.java @@ -1,6 +1,8 @@ package cn.dev33.satoken.listener; -import java.util.Date; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; import cn.dev33.satoken.SaManager; import cn.dev33.satoken.stp.SaLoginModel; @@ -50,8 +52,9 @@ public class SaTokenListenerForConsolePrint implements SaTokenListener { */ @Override public void doDisable(String loginType, Object loginId, String service, int level, long disableTime) { - Date date = new Date(System.currentTimeMillis() + disableTime * 1000); - println("账号[" + loginId + "] " + service + " 服务被封禁,封禁等级=" + level + " (解封时间: " + SaFoxUtil.formatDate(date) + ")"); + Instant instant = Instant.ofEpochMilli(System.currentTimeMillis() + disableTime * 1000); + ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()); + println("账号[" + loginId + "] " + service + " 服务被封禁,封禁等级=" + level + " (解封时间: " + SaFoxUtil.formatDate(zonedDateTime) + ")"); } /** diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java b/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java index ae2bf386..b17ac13a 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java @@ -4,6 +4,8 @@ import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.text.SimpleDateFormat; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -106,6 +108,15 @@ public class SaFoxUtil { public static String formatDate(Date date){ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); } + + /** + * 将日期格式化 (yyyy-MM-dd HH:mm:ss) + * @param zonedDateTime 日期 + * @return 格式化后的时间 + */ + public static String formatDate(ZonedDateTime zonedDateTime) { + return zonedDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + } /** * 从集合里查询数据 diff --git a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java index a25175f3..e39abdf3 100644 --- a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java +++ b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java @@ -1,8 +1,10 @@ package cn.dev33.satoken.core.util; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Arrays; -import java.util.Date; import java.util.List; import org.junit.jupiter.api.Assertions; @@ -51,7 +53,9 @@ public class SaFoxUtilTest { @Test public void formatDate() { - String formatDate = SaFoxUtil.formatDate(new Date(1644328600364L)); + Instant instant = Instant.ofEpochMilli(1644328600364L); + ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai")); + String formatDate = SaFoxUtil.formatDate(zonedDateTime); Assertions.assertEquals(formatDate, "2022-02-08 21:56:40"); } From 874f1d15e95d6e2dff228e558fa3bbb5a6ba21a2 Mon Sep 17 00:00:00 2001 From: faysalmehmood Date: Sun, 18 Sep 2022 22:06:32 +0500 Subject: [PATCH 2/3] #309 indentation fix. --- .../java/cn/dev33/satoken/core/util/SaFoxUtilTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java index e39abdf3..2c5f980c 100644 --- a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java +++ b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java @@ -51,13 +51,13 @@ public class SaFoxUtilTest { Assertions.assertNotEquals(SaFoxUtil.getMarking28(), SaFoxUtil.getMarking28()); } - @Test - public void formatDate() { + @Test + public void formatDate() { Instant instant = Instant.ofEpochMilli(1644328600364L); ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai")); String formatDate = SaFoxUtil.formatDate(zonedDateTime); - Assertions.assertEquals(formatDate, "2022-02-08 21:56:40"); - } + Assertions.assertEquals(formatDate, "2022-02-08 21:56:40"); + } @Test public void searchList() { From 6e4baec90f9801bbd88166c85b36af6bc259b4d6 Mon Sep 17 00:00:00 2001 From: Faisal Mehmood Date: Sun, 18 Sep 2022 22:09:43 +0500 Subject: [PATCH 3/3] Fixing again, the indentation issue. --- .../cn/dev33/satoken/core/util/SaFoxUtilTest.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java index 2c5f980c..4c2f9404 100644 --- a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java +++ b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java @@ -51,13 +51,13 @@ public class SaFoxUtilTest { Assertions.assertNotEquals(SaFoxUtil.getMarking28(), SaFoxUtil.getMarking28()); } - @Test - public void formatDate() { - Instant instant = Instant.ofEpochMilli(1644328600364L); - ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai")); - String formatDate = SaFoxUtil.formatDate(zonedDateTime); - Assertions.assertEquals(formatDate, "2022-02-08 21:56:40"); - } + @Test + public void formatDate() { + Instant instant = Instant.ofEpochMilli(1644328600364L); + ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai")); + String formatDate = SaFoxUtil.formatDate(zonedDateTime); + Assertions.assertEquals(formatDate, "2022-02-08 21:56:40"); + } @Test public void searchList() {