获取当月1号到当前的日期时间
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime startTime = DateTime.parse(new SimpleDateFormat("yyyy-MM-01 00:00:00").format(new Date()), formatter);
DateTime endTime = DateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), formatter);
案例:查询数据库中当月新增的所有数据
StudentMapper.xml
<select id="select" resultType="java.lang.Integer">
select count(id) from student where times BETWEEN #{startTime} and #{endTime}
</select>
StudentMapper.java
Integer select(@Param("startTime") String startTime,@Param("endTime") String endTime);
StudentService.java
Integer select(DateTime startTime, DateTime endTime);
StudentServiceImpl.java
@Override
public Integer select(DateTime tempStartTime, DateTime TempEndTime) {
String startTime = tempStartTime.toString("yyyy-MM-dd 00:00:00");
String endTime = TempEndTime.toString("yyyy-MM-dd 23:59:59");
return studentMapper.select(startTime,endTime);
}
StudentController.java
@GetMapping("/number")
public AjaxResult countStudent(){
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime startTime = DateTime.parse(new SimpleDateFormat("yyyy-MM-01 00:00:00").format(new Date()), formatter);
DateTime endTime = DateTime.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), formatter);
Integer number = studentService.select(startTime, endTime);
return AjaxResult.success(number);
}