【JAVA基础】时间处理
时间处理
查询前台报表运单数据集
@ApiOperation(value = "查询前台报表运单数据集")
@Permission(permissionPublic = true)
@ProcessLovValue(targetField = BaseConstants.FIELD_BODY)
@GetMapping("/searchIfpShipment")
public ResponseEntity<Map<String, Object>> searchIfpShipment(
@RequestParam(required = false) Date beginTime, @RequestParam(required = false) Date endTime, @PathVariable Long organizationId) throws ParseException {
Map<String,Object> map = this.ifpFrontDeskService.searchIfpShipmentMap(beginTime, endTime, organizationId);
return Results.success(map);
}
/**
* 查找前台折线图展示运单数据返回map
* @param beginTime
* @param endTime
* @param organizationId
* @return
*/
Map<String, Object> searchIfpShipmentMap(Date beginTime, Date endTime, Long organizationId) throws ParseException;
</details>
public Map<String, Object> searchIfpShipmentMap(Date beginTime, Date endTime, Long organizationId) throws ParseException {
Map<String, Object> map = new HashMap<String, Object>();
// 查询已接单TO_LOAD状态的运单数据
List<IfpFrontDeskShipmentDTO> toLoadShipmentList = searchIfpShipment(beginTime, endTime, "TO_LOAD", organizationId);
map.put("TO_LOAD", toLoadShipmentList);
// 查询已装货TO_UNLOAD状态的运单数据
List<IfpFrontDeskShipmentDTO> toUnloadShipmentList = searchIfpShipment(beginTime, endTime, "TO_UNLOAD", organizationId);
map.put("TO_UNLOAD", toUnloadShipmentList);
// 查询已卸货TO_SIGN状态的运单数据
List<IfpFrontDeskShipmentDTO> toSignShipmentList = searchIfpShipment(beginTime, endTime, "TO_SIGN", organizationId);
map.put("TO_SIGN", toSignShipmentList);
// 查询已签收SIGN状态的运单数据
List<IfpFrontDeskShipmentDTO> signShipmentList = searchIfpShipment(beginTime, endTime, "SIGN", organizationId);
map.put("SIGN", signShipmentList);
// 查询已对账状态COMPLETED的运单数据
List<IfpFrontDeskShipmentDTO> completedShipmentList = searchIfpShipment(beginTime, endTime, "COMPLETED", organizationId);
map.put("COMPLETED", completedShipmentList);
// 查询已支付状态PAID的运单数据
List<IfpFrontDeskShipmentDTO> paidShipmentList = searchIfpShipment(beginTime, endTime, "PAID", organizationId);
map.put("PAID", paidShipmentList);
return map;
}
@Override
public List<IfpFrontDeskShipmentDTO> searchIfpShipment(Date beginTime, Date endTime, String status, Long organizationId) throws ParseException {
// 计算时间差
int day = differentDays(beginTime, endTime);
List<IfpFrontDeskShipmentDTO> ifpFrontDeskShipmentDTOList = new ArrayList<>();
Date tempTime = beginTime;
for (int i = 0; i <= day; i++) {
IfpFrontDeskShipmentDTO ifpFrontDeskShipmentDTO = new IfpFrontDeskShipmentDTO();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String s = sdf.format(tempTime);
Date beginDate = sdf.parse(s);
Date endDate = dateAddOne(beginDate);
ifpFrontDeskShipmentDTO.setTime(tempTime);
if (status.equals("TO_LOAD") || status.equals("TO_UNLOAD") || status.equals("TO_SIGN") || status.equals("SIGN")) {
List<IfpShipment> shipmentList = ifpShipmentPushMapper.selectShipment(status, beginDate, endDate, organizationId);
int quantity = shipmentList.size();
ifpFrontDeskShipmentDTO.setShipmentQuantity((long) quantity);
ifpFrontDeskShipmentDTOList.add(ifpFrontDeskShipmentDTO);
} else if (status.equals("COMPLETED")) {
List<IfpShipment> shipmentAccountList = ifpShipmentPushMapper.selectAccountList(status, beginDate, endDate, organizationId);
int quantity = shipmentAccountList.size();
ifpFrontDeskShipmentDTO.setShipmentQuantity((long) quantity);
ifpFrontDeskShipmentDTOList.add(ifpFrontDeskShipmentDTO);
} else if (status.equals("PAID")) {
List<IfpShipment> shipmentAccountList = ifpShipmentPushMapper.selectPaidList(status, beginDate, endDate, organizationId);
int quantity = shipmentAccountList.size();
ifpFrontDeskShipmentDTO.setShipmentQuantity((long) quantity);
ifpFrontDeskShipmentDTOList.add(ifpFrontDeskShipmentDTO);
}
tempTime = dateAddOne(tempTime);
}
return ifpFrontDeskShipmentDTOList;
}
/**
* date2比date1多的天数
*
* @param date1
* @param date2
* @return
*/
private static int differentDays(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
int day1 = cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
int year1 = cal1.get(Calendar.YEAR);
int year2 = cal2.get(Calendar.YEAR);
//同一年
if (year1 != year2) {
int timeDistance = 0;
for (int i = year1; i < year2; i++) {
//闰年
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
timeDistance += 366;
} else //不是闰年
{
timeDistance += 365;
}
}
return timeDistance + (day2 - day1);
} else {// 不同年
System.out.println("判断day2 - day1 : " + (day2 - day1));
return day2 - day1;
}
}
/*日期加+1天*/
public static Date dateAddOne(Date date) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
//把日期往后增加一天,整数 往后推,负数往前移动
calendar.add(Calendar.DATE, 1);
//这个时间就是日期往后推一天的结果
date = calendar.getTime();
return date;
}
/**
* 筛选满足运单状态条件的运单数量
* @param status
* @param beginDate
* @param endDate
* @param tenantId
* @return
*/
List<IfpShipment> selectShipment(String status, Date beginDate,Date endDate, Long tenantId);
<select id="selectShipment" resultType="com.hand.htms.ifp.entity.IfpShipment">
SELECT
*
FROM
ifp_shipment t1
WHERE (t1.creation_date > #{beginDate} and t1.creation_date<#{endDate})
<if test="status != null and status != ''">
and t1.shipment_status =#{status}
</if>
<if test="tenantId !=null and tenantId != 1">
and t1.tenant_id = #{tenantId}
</if>
</select>
相关资料
获取当前日期和时间
Date与String的相互转换
两个日期之间相差的天数
https://blog.csdn.net/nandao158/article/details/121905050
String、Date、LocalDate之间的互相转换
https://blog.csdn.net/weixin_43526092/article/details/103531391