场景:
因为开始和结束的日期时间的时分秒都是00:00:00,所以要设置查询的结束日期要加上一天去查询,默认是开始时间为空,就设置查询开始时间为前7天的日期。
html请求:
<div class="formLine">
<label for="fltStartDate">航班落地开始日期</label>
<input th:value="${#dates.format(travelInfoVO?.fltStartDate, 'yyyy-MM-dd')}"
type="text" id="fltStartDate"
name="fltStartDate"
class="commonInput w120 datepicker" readonly/>
</div>
<div class="formLine">
<label for="fltEndDate">航班落地结束日期</label>
<input th:value="${#dates.format(travelInfoVO?.fltEndDate, 'yyyy-MM-dd')}"
type="text" id="fltEndDate"
name="fltEndDate"
class="commonInput w120 datepicker" readonly/>
</div>
controller
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmeport.framework.constant.CommonConstant;
import com.xmeport.project.common.utils.CustomDateUtil;
import com.xmeport.project.model.entity.ApiTravelInfo;
import com.xmeport.project.model.vo.ApiTravelInfoVO;
import com.xmeport.project.service.IApiTravelInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import com.xmeport.framework.web.controller.SuperController;
import java.util.Date;
/**
* <p>
* 旅客行程信息表 前端控制器
* </p>
*
* @author sunhr
* @since 2020-06-17
*/
@Slf4j
@Controller
@RequestMapping("/travel/info")
public class ApiTravelInfoController extends SuperController {
@Autowired
private IApiTravelInfoService apiTravelInfoService;
@RequestMapping("/list")
public String list(Model model, ApiTravelInfoVO travelInfoVO) {
log.info("旅客行程查询分页查询");
if (travelInfoVO.getFltStartDate() == null && travelInfoVO.getFltEndDate() == null){
Date date = new Date();
Date nowDate = CustomDateUtil.getDate(date);
travelInfoVO.setFltStartDate(DateUtils.addWeeks(nowDate,-1));
travelInfoVO.setFltEndDate(nowDate);
}
Page<ApiTravelInfo> page = super.getPage();
apiTravelInfoService.findPage(page, travelInfoVO);
model.addAttribute("travelInfoVO", travelInfoVO)
.addAttribute(CommonConstant.PAGE_KEY, page);
return "travel/travel_info_query_list";
}
}
其中的CustomDateUtil获取日期的凌晨时间
/**
* 获取日期
* @param date
* @return
*/
public static Date getDate(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
然后设置开始时间为当前前一周(nowDate - 7天)的日期
使用的api:
[Maven: org.apache.commons:commons-lang3:3.8.1] org.apache.commons.lang3.time public class DateUtils extends Object
public static Date addWeeks(final Date date, final int amount) {
return add(date, Calendar.WEEK_OF_YEAR, amount);
}
service:
package com.xmeport.project.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmeport.project.model.entity.ApiTravelInfo;
import com.xmeport.project.mapper.ApiTravelInfoMapper;
import com.xmeport.project.model.vo.ApiTravelInfoVO;
import com.xmeport.project.service.IApiTravelInfoService;
import com.xmeport.framework.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
/**
* <p>
* 旅客行程信息表 服务实现类
* </p>
*
* @author sunhr
* @since 2020-06-17
*/
@Service
public class ApiTravelInfoServiceImpl extends BaseServiceImpl<ApiTravelInfoMapper, ApiTravelInfo> implements IApiTravelInfoService {
/**
* @param page
* @param apiTravelInfoVO
* @author sunhr
* @date: 2020/6/17 10:14
*/
@Override
public IPage<ApiTravelInfo> findPage(Page<ApiTravelInfo> page, ApiTravelInfoVO apiTravelInfoVO) {
super.query()
.eq(StringUtils.isNotBlank(apiTravelInfoVO.getTkne()), ApiTravelInfo::getTkne, apiTravelInfoVO.getTkne())
.eq(StringUtils.isNotBlank(apiTravelInfoVO.getPsgType()), ApiTravelInfo::getPsgType, apiTravelInfoVO.getPsgType())
.ge(apiTravelInfoVO.getFltStartDate() != null, ApiTravelInfo::getFltDate, apiTravelInfoVO.getFltStartDate())
.lt(apiTravelInfoVO.getFltEndDate() != null, ApiTravelInfo::getFltDate, apiTravelInfoVO.getFltEndDate()!=null?
DateUtils.addDays(apiTravelInfoVO.getFltEndDate(),1):null)
.page(page);
return page;
}
}