MySQL使用时间戳比较大小
使用 unix_timestamp(start_time)将datetime格式化成时间戳,之后即可进行比较
示例:
<!-- 截取mybaits xml片段 -->
SELECT e.true_name, a.start_time, a.end_time, a.remark, a.create_time
FROM record a
join student s on a.student_id = s.student_id
join student e on e.user_id = s.user_id
join teacher t on t.teacher_id = a.teacher_id
WHERE
t.teacher_id = #{teacherId}
<if test="trueName != null and trueName != ''">and e.true_name like concat('%', #{trueName}, '%')</if>
<if test="accompanyStatus == '1'.toString()">
<!-- 在此处使用时间戳进行比较 -->
and unix_timestamp(a.start_time) > unix_timestamp(now())
</if>
<if test="accompanyStatus == '2'.toString()">
and unix_timestamp(now())
between unix_timestamp(a.start_time) and unix_timestamp(a.end_time)
</if>
<if test="accompanyStatus == '3'.toString()">
and unix_timestamp(now()) > unix_timestamp(a.end_time)
</if>
Java 叠加时间
使用 Calendar 类对datetime类型的时间进行修改;
示例:
// 时间加上23时59分59秒
Calendar cal = Calendar.getInstance();
// 从实体类获取时间 使用setTime()方法进行设置
cal.setTime(accompanyRecord.getEndTime());
// 添加 第一个属性是天机的单位,第二个是添加的数字
cal.add(Calendar.HOUR,23);
cal.add(Calendar.MINUTE,59);
cal.add(Calendar.SECOND,59);
// 添加完后使用 getTime() 方法获取添加完成的时间
accompanyRecord.setEndTime(cal.getTime());