From 62bfa859bce4865121bd07f613c7e22a79a1a607 Mon Sep 17 00:00:00 2001 From: lingkang <910405656@qq.com> Date: Sat, 16 Mar 2024 22:36:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=B6=E9=97=B4=E8=A7=A3?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/noear/snack/core/utils/DateUtil.java | 11 +++++---- snack3_demo/src/test/java/test2/TestDate.java | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 snack3_demo/src/test/java/test2/TestDate.java diff --git a/snack3/src/main/java/org/noear/snack/core/utils/DateUtil.java b/snack3/src/main/java/org/noear/snack/core/utils/DateUtil.java index a87c795..b294471 100644 --- a/snack3/src/main/java/org/noear/snack/core/utils/DateUtil.java +++ b/snack3/src/main/java/org/noear/snack/core/utils/DateUtil.java @@ -16,6 +16,7 @@ import java.util.TimeZone; */ public class DateUtil { public static final String FORMAT_29 = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; + public static final String FORMAT_25 = "yyyy-MM-dd'T'HH:mm:ss+HH:mm"; public static final String FORMAT_24_ISO08601 = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; public static final String FORMAT_23_a = "yyyy-MM-dd HH:mm:ss,SSS"; public static final String FORMAT_23_b = "yyyy-MM-dd HH:mm:ss.SSS"; @@ -38,9 +39,9 @@ public class DateUtil { /** * 解析时间 - * */ + */ public static Date parse(String val) throws ParseException { - if(val == null){ + if (val == null) { return null; } @@ -52,6 +53,8 @@ public class DateUtil { if (val.charAt(26) == ':' && val.charAt(28) == '0') { ft = FORMAT_29; } + } else if (len == 25) { + ft = FORMAT_25; } else if (len == 24) { if (val.charAt(10) == 'T') { ft = FORMAT_24_ISO08601; @@ -139,14 +142,14 @@ public class DateUtil { /** * 格式化时间 - * */ + */ public static String format(Date date, String dateFormat) { return format(date, dateFormat, null); } /** * 格式化时间 - * */ + */ public static String format(Date date, String dateFormat, TimeZone timeZone) { DateFormat df = new SimpleDateFormat(dateFormat, DEFAULTS.DEF_LOCALE); if (timeZone != null) { diff --git a/snack3_demo/src/test/java/test2/TestDate.java b/snack3_demo/src/test/java/test2/TestDate.java new file mode 100644 index 0000000..579a3bc --- /dev/null +++ b/snack3_demo/src/test/java/test2/TestDate.java @@ -0,0 +1,23 @@ +package test2; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; +import org.junit.jupiter.api.Test; +import org.noear.snack.ONode; + +/** + * @author lingkang + * Created by 2024/3/16 + */ +public class TestDate { + @Test + public void test01() { + String payContent = "{\"mchid\":\"1623051071\",\"appid\":\"wx530baf52565c2828\",\"out_trade_no\":\"WX1710597479999990\",\"transaction_id\":\"4200002188202403160282491796\",\"trade_type\":\"NATIVE\",\"trade_state\":\"SUCCESS\",\"trade_state_desc\":\"支付成功\",\"bank_type\":\"OTHERS\",\"attach\":\"test\",\"success_time\":\"2024-03-16T21:58:25+08:00\",\"payer\":{\"openid\":\"oavVX6iIjDGt3Jpt6f7S3GoNjWJM\"},\"amount\":{\"total\":1,\"payer_total\":1,\"currency\":\"CNY\",\"payer_currency\":\"CNY\"}}"; + ONode node = ONode.loadStr(payContent); + System.out.println(node.get("success_time").getDate()); + if (node.get("success_time").getDate() == null) + throw new IllegalArgumentException("时间解析失败: " + "2024-03-16T21:58:25+08:00"); + JSONObject parse = (JSONObject) JSON.parse(payContent); + System.out.println(parse.getDate("success_time")); + } +} -- Gitee