Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Fix castToDate with format error #3309 #3311

Merged
merged 1 commit into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/alibaba/fastjson/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"))) {
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_3300/Issue3309.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}