java error(2)保存时间带时分秒,回显时分秒变成00:00:00

超简单,顺带记录一下

1.入参实体类上使用注释:@JsonFormat(pattern = “yyyy-MM-dd”) 导致舍弃了 时分秒的部分。
2.数据库字段对应的类型是 date。date就是日期,日期就不带时分秒。
3.返参实体类使用了@JsonFormat(pattern = “yyyy-MM-dd”) 导致舍弃了时分秒部分。

扩展
关于注解@JsonFormat

General-purpose annotation used for configuring details of how values of properties are to be serialized. Unlike most other Jackson annotations, annotation does not have specific universal interpretation: instead, effect depends on datatype of property being annotated (or more specifically, deserializer and serializer being used). <p> Common uses include choosing between alternate representations -- for example, whether {@link java.util.Date} is to be serialized as number (Java timestamp) or String (such as ISO-8601 compatible time value) -- as well as configuring exact details with {@link pattern} property. <p> As of Jackson 2.6, known special handling includes: <ul> <li>{@link java.util.Date}: Shape can be {@link ShapeSTRING} or {@link ShapeNUMBER}; pattern may contain {@link java.text.SimpleDateFormat}-compatible pattern definition. <li> <li>Can be used on Classes (types) as well, for modified default behavior, possibly overridden by per-property annotation <li> <li>{@link java.lang.Enum}s: Shapes {@link ShapeSTRING} and {@link ShapeNUMBER} can be used to change between numeric (index) and textual (name or <code>toString()<code>); but it is also possible to use {@link ShapeOBJECT} to serialize (but not deserialize) {@link java.lang.Enum}s as JSON Objects (as if they were POJOs). NOTE: serialization as JSON Object only works with class annotation; will not work as per-property annotation. <li> <li>{@link java.util.Collection}s can be serialized as (and deserialized from) JSON Objects, if {@link ShapeOBJECT} is used. NOTE: can ONLY be used as class annotation; will not work as per-property annotation. <li> <li>{@link java.lang.Number} subclasses can be serialized as full objects if {@link ShapeOBJECT} is used. Otherwise the default behavior of serializing to a scalar number value will be preferred. NOTE: can ONLY be used as class annotation; will not work as per-property annotation. <li> <ul>

机器翻译:通用注释,用于配置如何序列化属性值的详细信息。与大多数其他 Jackson 注释不同,注释没有特定的通用解释:相反,效果取决于被注释的属性的数据类型(或者更具体地说,使用的反序列化器和序列化器)。<p> 常见用途包括在替代表示形式之间进行选择——例如,{@link java.util.Date} 是序列化为数字(Java 时间戳)还是字符串(例如 ISO-8601 兼容的时间值)——以及使用 {@link pattern} 属性配置确切的详细信息。<p>Jackson 2.6 开始,已知的特殊处理包括: <ul> <li>{@link java.util.Date}:形状可以是 {@link ShapeSTRING}{@link ShapeNUMBER};pattern 可能包含 {@link java.text.SimpleDateFormat} 兼容的模式定义。<li> <li>也可以用于类(类型),用于修改默认行为,可能被每个属性的注释 <li> <li>{@link java.lang.Enum} 覆盖:形状 {@link ShapeSTRING}{@link ShapeNUMBER} 可用于在数字(索引)和文本(name 或 <code>toString())之间切换<code>;但也可以使用 {@link ShapeOBJECT}{@link java.lang.Enum} 序列化(但不能反序列化)为 JSON 对象(就像它们是 POJO 一样)。注意:序列化为 JSON Object 仅适用于类注释;将不用作每个属性的注释。<li> <li>如果使用 {@link ShapeOBJECT},则 {@link java.util.Collection} 可以序列化为 JSON 对象(并从中反序列化)。注意:只能用作类注释;将不用作每个属性的注释。<li> <li>如果使用 {@link ShapeOBJECT},则可以将 {@link java.lang.Number} 子类序列化为完整对象。否则,将首选序列化为标量数值的默认行为。注意:只能用作类注释;将不用作每个属性的注释。<li> <ul>

补充几个 @JsonFormat 的使用方式。

//实体类
@Data
@Accessors(chain = true)
public class TestJsonFormat {
    @JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
    private Integer sendDate;
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private String str;
    @JsonFormat(shape = JsonFormat.Shape.NUMBER)
    private Date dateNum;
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date getDate;
}
//接口定义
    @GetMapping("/testJsonFormat")
    public AjaxResult testJsonFormat(@RequestBody TestJsonFormat format) {
        format.setDateNum(new Date());
        return AjaxResult.success(format);
    }
//接口调用入参:
{
    "sendDate": 12.3,
    "str": 333,
    "receiveDate":"2023-10-21"
}
//打印结果:
TestJsonFormat(sendDate=12, str=333, dateNum=null, receiveDate=Sat Oct 21 00:00:00 CST 2023)
//返回参数:
{
	"msg": "操作成功",
	"code": 200,
	"data": {
		"sendDate": 12,
		"str": "333",
		"dateNum": 1734569603473,
		"receiveDate": "2023-10-21"
	}
}
可以看到:
1@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT):将接收数据转换成了整形
2@JsonFormat(shape = JsonFormat.Shape.STRING):将接收数据转换成了字符串类型
3@JsonFormat(shape = JsonFormat.Shape.NUMBER):将返回的数据转换成了数字类型时间戳
4@JsonFormat(pattern = "yyyy-MM-dd"):将字符串类型的时间转换成了对象,又以指定格式返回字符串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值