Skip to content

Commit

Permalink
add testcase for issue #2426
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 13, 2024
1 parent bfa24a7 commit bc167b0
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.alibaba.fastjson2.JSONWriter;
import org.junit.jupiter.api.Test;

import java.sql.Timestamp;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -14,11 +15,19 @@ public class Issue2426 {
public void test() {
String defaultWriterFormat = JSONFactory.getDefaultWriterFormat();
try {
JSON.configWriterDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = new Date(1712988987882L);
String dateFormat = "yyyy-MM-dd'T'HH:mm:ss";
JSON.configWriterDateFormat(dateFormat);

JSONWriter.Context writeContext = JSONFactory.createWriteContext();
assertEquals(dateFormat, writeContext.getDateFormat());

Date date = new Date(1712988987882L);
String json = JSON.toJSONString(date, writeContext);
assertEquals("\"2024-04-13T14:16:27\"", json);

java.sql.Timestamp ts = new Timestamp(date.getTime());
String json1 = JSON.toJSONString(date, writeContext);
assertEquals("\"2024-04-13T14:16:27\"", json1);
} finally {
JSON.configWriterDateFormat(defaultWriterFormat);
}
Expand Down

0 comments on commit bc167b0

Please sign in to comment.