Java前端如何发送date类型的参数给后端

@DateTimeFormat

第一次:Get方式传参-成功 这个时候是用的get请求方式,get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应。

/**
 * http://localhost:8080/intoParam?date=2019-01-18 11:11:11
*/
    @RequestMapping(value = "/intoParam",method = RequestMethod.GET)
    @ResponseBody
    public void intoParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date){
        System.out.println(date);//Fri Jan 18 08:00:00 CST 2019
    }

第二次:Post方式传参-失败

/**
     * http://localhost:8080/intoParam
     * 请求体
     * {
     *     "date":"2019-01-18 11:11:11"
     * }
     */
    @RequestMapping(value = "/intoParam",method = RequestMethod.POST)
    @ResponseBody
    public void intoParam2(@RequestBody DateVo dateVo){
        System.out.println(dateVo.getDate());//Fri Jan 18 08:00:00 CST 2019
    }

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DateVo {

    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date date;
}

错误信息

{
 "timestamp": "2021-10-19T07:05:22.407+0000",
 "status": 400,
 "error": "Bad Request",
 "message": "JSON parse error: Cannot deserialize value of type `java.util.Date` from String \"2019-01-18 11:11:11\": not a valid representation (error: Failed to parse Date value '2019-01-18 11:11:11': Cannot parse date \"2019-01-18 11:11:11\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String \"2019-01-18 11:11:11\": not a valid representation (error: Failed to parse Date value '2019-01-18 11:11:11': Cannot parse date \"2019-01-18 11:11:11\": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))\n at [Source: (PushbackInputStream); line: 2, column: 12] (through reference chain: com.mye.hl20springbootdataparam.vo.DateVo[\"date\"])",
 "path": "/intoParam"
}

第三次:post传参-成功

/**
     * http://localhost:8080/intoParam
     * 請求體是:
     * {
     *     "date":"2019-01-18"
     * }
     */
    @RequestMapping(value = "/intoParam",method = RequestMethod.POST)
    @ResponseBody
    public void intoParam2(@RequestBody DateVo dateVo){
        System.out.println(dateVo.getDate());//Fri Jan 18 08:00:00 CST 2019
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(dateVo.getDate());
        System.out.println(format);//2019-01-18
    }

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DateVo {

    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date date;
}

原因

springboot默认采用jackson,而jackson只能识别以下几种日期格式

"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
 
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
 
"yyyy-MM-dd";
 
"EEE, dd MMM yyyy HH:mm:ss zzz";
 
long类型的时间戳(毫秒时间戳)

解决方法

采用long时间戳,如:1537191968000

或者

在传参的对象上加上@JsonFormat注解并且指定时区。

@JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")

第四次:post传参-成功

/**
     * http://localhost:8080/intoParam
     * 請求體是:
     * {
     *     "date":"2019-01-18 11:11:11"
     * }
     */
    @RequestMapping(value = "/intoParam",method = RequestMethod.POST)
    @ResponseBody
    public void intoParam2(@RequestBody DateVo dateVo){
        System.out.println(dateVo.getDate());//Fri Jan 18 11:11:11 CST 2019
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(dateVo.getDate());
        System.out.println(format);//2019-01-18 11:11:11
    }

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DateVo {

    @DateTimeFormat(pattern="yyyy-MM-dd")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date date;
}

@jsonFormat

@JsonFormat(pattern=“yyyy-MM-dd”,timezone = “GMT+8”)

pattern:是你需要转换的时间日期的格式
timezone:是时间设置为东八区,避免时间在转换中有误差
@JsonFormat注解可以在属性的上方,同样可以在属性对应的get方法上,两种方式没有区别

展示结果

{
 "date": "2021-10-19 15:44:51"
}

总结 前端Content-Type 为application/json的请求时,我们使用@JsonFormat来进行转化,如果为表单,则应该使用@DateTimeFormat

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

剽悍一小兔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值