diff --git a/src/main/java/com/alibaba/fastjson/util/TypeUtils.java b/src/main/java/com/alibaba/fastjson/util/TypeUtils.java index a9c26da9c5..c7a622855c 100644 --- a/src/main/java/com/alibaba/fastjson/util/TypeUtils.java +++ b/src/main/java/com/alibaba/fastjson/util/TypeUtils.java @@ -454,7 +454,7 @@ public static Date castToDate(Object value, String format){ strVal = strVal.substring(6, strVal.length() - 2); } - if (strVal.indexOf('-') > 0 || strVal.indexOf('+') > 0) { + if (strVal.indexOf('-') > 0 || strVal.indexOf('+') > 0 || format != null) { if (format == null) { if (strVal.length() == JSON.DEFFAULT_DATE_FORMAT.length() || (strVal.length() == 22 && JSON.DEFFAULT_DATE_FORMAT.equals("yyyyMMddHHmmssSSSZ"))) { diff --git a/src/test/java/com/alibaba/json/bvt/issue_3300/Issue3309.java b/src/test/java/com/alibaba/json/bvt/issue_3300/Issue3309.java new file mode 100644 index 0000000000..b0d8f67253 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/issue_3300/Issue3309.java @@ -0,0 +1,34 @@ +package com.alibaba.json.bvt.issue_3300; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.annotation.JSONField; +import junit.framework.TestCase; + +import java.util.Date; + +/** + * @Author :Nanqi + * @Date :Created in 16:27 2020/6/29 + */ +public class Issue3309 extends TestCase { + public void test_for_issue() throws Exception { + JSONObject jsonObj = new JSONObject(); + jsonObj.put("formatDate","20200623 15:20:01"); + DateFormatTest dateFormatTest = jsonObj.toJavaObject(DateFormatTest.class); + JSON.toJSONString(dateFormatTest); + } + + static class DateFormatTest { + @JSONField(format = "yyyyMMdd HH:mm:ss") + private Date formatDate; + + public Date getFormatDate() { + return formatDate; + } + + public void setFormatDate(Date formatDate) { + this.formatDate = formatDate; + } + } +}