@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)使用介绍

11 篇文章 0 订阅
<el-form-item label="比赛时间">
  <el-date-picker v-model="time" type="daterange" range-separator="至"
	placeholder="选择日期" start-placeholder="开始日期" end-placeholder="结束日期"
	@change="handleTable">
  </el-date-picker>
</el-form-item>

一、前台处理代码

data() {
	time:[],	
	param: { 
		startTime: null,
		endTime: null,
	},
}

handleTable () {
  if(this.time != null){
	this.param.startTime = this.time[0],
	this.param.endTime = this.time[1]
  }else{
	this.param.startTime = null;
	this.param.endTime = null;
  }
  xxxxxxx 请求后台方法
  
}

二、后台接收处理

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

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


xml中查询
<trim prefix="WHERE (" suffix=")" prefixOverrides="AND |OR ">
    <if test="startTime != null">
	    and t1.start_time &gt;= #{startTime,jdbcType=TIMESTAMP}
    </if>
    <if test="endTime != null ">
	    and t1.end_time &lt;= #{endTime,jdbcType=TIMESTAMP}
    </if>
</trim>

或者
<if test="startTime != null">
    <![CDATA[ and t1.start_time >= #{startTime,jdbcType=TIMESTAMP} ]]>
</if>

自己记录
name like concat('%',#{name,jdbcType=VARCHAR},'%') 

三、页面数据,准备测试

前台请求, 应该查出两条数据,实际只查出一条数据,时间格式是有问题的

 四、在vue组件中加 :default-time

<el-date-picker v-model="time" type="daterange" range-separator="至"
	placeholder="选择日期" start-placeholder="开始日期" end-placeholder="结束日期"
	:default-time="['00:00:00', '23:59:59']"
	@change="handleTable">
</el-date-picker>

请求的参数有些变化,但后台接收到的格式没变化,查询依然存在问题,应该是接收格式有问题。

再去看看@DateTimeFormat注解,

 修改接收的对象

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private Date startTime;

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private Date endTime;

重启后台,完美解决

备注: 时间字段在数据库中使用的是 datetime 类型

五、前端默认查一周

let startTime = new Date();
let endTime = new Date();
console.log(start+ "====" + end) 
// Tue Oct 22 2023 11:54:38 GMT+0800 (中国标准时间)====Tue Oct 22 2023 11:54:38 GMT+0800 (中国标准时间)

startTime.setHours(0,0,0);
endTime.setHours(23,59,59);
console.log(start+ "====" + end) 
// Tue Oct 22 2023 00:00:00 GMT+0800 (中国标准时间)====Tue Oct 22 2023 23:59:59 GMT+0800 (中国标准时间)

// 7天前的数据
start.setTime(start.getTime() - 3600 * 1000 * 24 * 6);
console.log(start+ "====" + end) 
// Wed Oct 16 2023 00:00:00 GMT+0800 (中国标准时间)====Tue Oct 22 2023 23:59:59 GMT+0800 (中国标准时间)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值