搞懂@DateTimeFormat 注解 和 对应的时间类型

通常而言,前端时间控件,一般情况下直接会传一个yyyy-MM-dd的日期字符串到后台。如果我们直接用java.util.Date类型来接收,是无法获取的。这是因为Date类型默认的格式为:Tue May 16 00:00:00 CST 2023这种。

举例

    @ApiOperation(value = "后端不限制格式,用Date接收")
    @GetMapping("t111")
    public Result t111(
            @RequestParam
            Date date
    ){
        System.out.println(date);
        return Result.success(date);
    }

2023-05-16 23:53:54.053  WARN 7844 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.util.Date] for value '2023-05-16'; nested exception is java.lang.IllegalArgumentException]

但是如果按Date的默认格式传递就ok。 

要想解决这个问题,那么我们就需要在接收参数上使用到@DateTimeFormat注解。然后pattern属性来定义我们接收参数的格式规则。只要前端传递的参数符合该规则,就能被接收,否则就会报错。

    @ApiOperation(value = "日期_1")
    @GetMapping("t22")
    public Result t22(
            @RequestParam
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            Date date
    ){
        System.out.println(date);
        return Result.success(date);
    }

注意:如果我们用String字符串来接收,那么@DateTimeFormat是无法限制的,因为@DateTimeFormat只对时间日期类型生效。

下面,我们再来结合不同参数类型,演示接收时间日期的效果。

代码演示

package com.hssy.swaggerdemo.controller;

import com.hssy.swaggerdemo.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;

@RestController
@Api(tags = "时间日期练习控制器")
@RequestMapping("/dateTime")
public class DateTimeController {

    @ApiOperation(value = "时间日期")
    @GetMapping("t1")
    public Result t1(
            @RequestParam
            @DateTimeFormat(pattern = "HH:mm:ss yyyy-MM-dd")// 限制前端传递的格式,格式必须带有日期和时间,其他可选
            LocalDateTime localDateTime
    ){
        System.out.println(localDateTime);
        return Result.success(localDateTime);
    }


    @ApiOperation(value = "日期")
    @GetMapping("t2")
    public Result t2(
            @RequestParam
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") // 限制前端传递的格式,格式一定要带有日期,其他可选
            LocalDate localDate
    ){
        System.out.println(localDate);
        return Result.success(localDate);
    }


    @ApiOperation(value = "时间")
    @GetMapping("t3")
    public Result t3(
            @RequestParam
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") // 限制前端传递的格式,格式一定要带有时间,其他可选
            LocalTime localTime
    ){
        System.out.println(localTime);
        return Result.success(localTime);
    }




    @ApiOperation(value = "时间日期_1")
    @GetMapping("t11")
    public Result t11(
            @RequestParam
            @DateTimeFormat(pattern = "HH:mm:ss yyyy-MM-dd")
            Date date
    ){
        System.out.println(date);
        return Result.success(date);
    }


    @ApiOperation(value = "日期_1")
    @GetMapping("t22")
    public Result t22(
            @RequestParam
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            Date date
    ){
        System.out.println(date);
        return Result.success(date);
    }


    @ApiOperation(value = "时间_1")
    @GetMapping("t33")
    public Result t33(
            @RequestParam
            @DateTimeFormat(pattern = "HH:mm:ss")
            Date date
    ){
        System.out.println(date);
        return Result.success(date);
    }
}

总结

@DateTimeFormat 用来规定前端传递的时间日期的格式。

@DateTimeFormat 注解的pattern规则,规定了前端传递是参数必须符合该规则,否则报错。

当使用java.util.Date类型接收参数时,无论@DateTimeFormat的格式是只带有时间还是只带有日    期,或者都带有,Date对象都能够解析到。遇到没有传递的部分会自动解析为默认值。

比如:     如果格式要求 yyyy-MM-dd,那么时间默认解析为 00:00:00

                如果格式要求 HH:mm:ss,那么日期默认解析为 1970-01-01

当使用java.time.LocalDateTime接收参数时,@DateTimeFormat的格式必须带有时间和日期,否则无法解析到。

当使用java.time.LocalDate接收参数时,@DateTimeFormat的格式必须包含日期,其他可选。

当使用java.time.LocalTime接收参数时,@DateTimeFormat的格式必须包含时间,其他可选。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

何苏三月

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

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

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

打赏作者

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

抵扣说明:

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

余额充值