support custom date format parse

This commit is contained in:
Looly
2021-06-25 23:05:54 +08:00
parent 2f4544bc23
commit 85e9bc11c2
5 changed files with 69 additions and 2 deletions

View File

@@ -429,6 +429,27 @@ public class JSONObjectTest {
Assert.assertEquals("{\"date\":[\"2020-06-05 11:16:11\"],\"bbb\":[\"222\"],\"aaa\":[\"123\"]}", json.toString());
}
@Test
public void setDateFormatTest2(){
JSONConfig jsonConfig = JSONConfig.create();
jsonConfig.setDateFormat("yyyy#MM#dd");
jsonConfig.setOrder(true);
Date date = DateUtil.parse("2020-06-05 11:16:11");
JSONObject json = new JSONObject(jsonConfig);
json.set("date", date);
json.set("bbb", "222");
json.set("aaa", "123");
String jsonStr = "{\"date\":\"2020#06#05\",\"bbb\":\"222\",\"aaa\":\"123\"}";
Assert.assertEquals(jsonStr, json.toString());
// 解析测试
final JSONObject parse = JSONUtil.parseObj(jsonStr, jsonConfig);
Assert.assertEquals(DateUtil.beginOfDay(date), parse.getDate("date"));
}
@Test
public void getTimestampTest(){
String timeStr = "1970-01-01 00:00:00";