Skip to content

Commit

Permalink
parse date support more format, for issue #2199
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 18, 2024
1 parent a979db4 commit 202dc46
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ private Object readDate(JSONReader jsonReader) {
}
}
} else {
if (str.length() == 19 && (yyyyMMddhhmm16 || jsonReader.isEnabled(JSONReader.Feature.SupportSmartMatch))) {
if (str.length() == 19
&& (yyyyMMddhhmm16
|| jsonReader.isEnabled(JSONReader.Feature.SupportSmartMatch)
|| "yyyy-MM-dd hh:mm:ss".equals(format))) {
int length = yyyyMMddhhmm16 ? 16 : 19;
ldt = DateUtils.parseLocalDateTime(str, 0, length);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.alibaba.fastjson2.issues_2100;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2199 {
@Data
public static class Obj1 {
@JSONField(format = "yyyy-MM-dd hh:mm:ss")
private Date startTime;
}

@Test
public void test() throws Exception {
Obj1 orderBO = new Obj1();
orderBO.setStartTime(new Date());
String s = JSONObject.toJSONString(orderBO);
Obj1 obj1 = JSONObject.parseObject(s, Obj1.class);
assertEquals(s, JSON.toJSONString(obj1));
}
}

0 comments on commit 202dc46

Please sign in to comment.