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 a87c7953d22e72eaa275499a98e68413bb7d01a4..b294471910feb44d6f94a368cd837e4d07369929 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 0000000000000000000000000000000000000000..579a3bc36222dd3f9efa46aa3b9ce95f610def8b --- /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")); + } +}