前端传过来时间,后端怎么用时间去数据库查询?

img


如图前端传过来的日期是这样的,但是数据库是2022-04-01 12:00:00这样的。应该怎么处理这个时间然后用mybatis查寻啊。

用数据库函数date_format格式化下

这个得前后端规定好统一的数据格式才行



```java
/**
 * 自定义类型转换器
 */
public class MyStringDataCovert implements Converter<String, Date> {

    private SimpleDateFormat[] sdfs = {
            new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"),
            new SimpleDateFormat("yyyy-MM-dd"),
            new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"),
            new SimpleDateFormat("yyyy/MM/dd")
    };

    @Override
    public Date convert(String source) {

        Date date = null;
        for (SimpleDateFormat sdf : sdfs) {
            try {
                date = sdf.parse(source);
            } catch (ParseException e) {

            }
        }
        if (date==null){
            throw new RuntimeException("日期格式转换异常!");
        }
        return date;
    }
}

然后在springMvc配置上加上这些

<mvc:annotation-driven conversion-service="MyStringDataCovert"/>

<bean id="MyStringDataCovert" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="com.java.covert.MyStringDataCovert" />
        </set>
    </property>
</bean>

```