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

Commit

Permalink
Merge pull request #3474 from fanzhongwei/master
Browse files Browse the repository at this point in the history
fix issue #3473
  • Loading branch information
wenshao authored Nov 3, 2020
2 parents 89f42e2 + 5ca2b3b commit 1ccd0f1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
}

Class<?> clazz = object.getClass();
if (clazz == java.sql.Date.class) {
if (clazz == java.sql.Date.class && !out.isEnabled(SerializerFeature.WriteDateUseDateFormat)) {
long millis = ((java.sql.Date) object).getTime();
TimeZone timeZone = serializer.timeZone;
int offset = timeZone.getOffset(millis);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.alibaba.fastjson.serializer.issue3473;

import java.sql.Date;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
* package com.alibaba.fastjson.serializer.issue3473 <br/>
* description: java.sql.Date序列化测试 <br/>
* Copyright 2019 thunisoft, Inc. All rights reserved.
*
* @author fanzhongwei
* @date 20-9-29
*/
public class SerializeWriterJavaSqlDateTest {

private Map<String, Object> data = new HashMap<String, Object>(1, 1);

@Before
public void before() throws ParseException {
data.put("sqlDate", new Date(DateUtils.parseDate("2020-09-29", "yyyy-MM-dd")
.getTime()));
}

@Test
public void yyyy_MM_dd_HH_mm_ss_test() {
String json = JSON.toJSONString(data, SerializerFeature.WriteDateUseDateFormat);
Assert.assertEquals("{\"sqlDate\":\"2020-09-29 00:00:00\"}", json);
}

@Test
public void yyyy_MM_dd_test() {
String json = JSON.toJSONString(data);
Assert.assertEquals("{\"sqlDate\":\"2020-09-29\"}", json);
}
}

0 comments on commit 1ccd0f1

Please sign in to comment.