LocalDateTime运算案例,以及参考网站

@Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Result fixedTicketAutoRecovery() {
        boolean isLock = redisLockService.lock(Const.FIXED_TICKET, 20);
        try {
            if (isLock) {
                LocalDateTime nowTime = LocalDateTime.now();
                // 将固定码表当中数据全部进行查出,然后比较该固定码是否已经过期
                QueryWrapper fixedCodeQueryWrapper = new QueryWrapper<>();
                fixedCodeQueryWrapper.eq("status", STATUS);
                fixedCodeQueryWrapper.le("valid_end_time", nowTime);
                List fixedCodes = fixedCodeDao.selectList(fixedCodeQueryWrapper);
                // 对过期固定码进行过滤筛选处理
                if (!fixedCodes.isEmpty()) {
                    for (FixedCode fixedCode : fixedCodes) {
                        // 联查判断该商户的优惠券过期处理是不是可以进行过期回收(0:回收、1:不回收)
                        ShopTicket shopTicketBean = shopTicketService.getById(fixedCode.getShopTicketId());
                        // 根据商户ID进行查询出相关的过期回收属性
                        Shop shopBean = shopService.getById(shopTicketBean.getShopId());
                        if (shopBean.getTicketOverdueProcessingWay() == 0) {
                            // 条件满足则该固定二维码已经过期
                            Integer total = fixedCode.getTotal();
                            Integer balance = fixedCode.getBalance();

                            // 对固定码数据进行过期处理
                            Integer updateTotal = total - balance;
                            fixedCode.setTotal(updateTotal);
                            fixedCode.setBalance(0);
                            fixedCode.setStatus(1);
                            fixedCodeDao.updateById(fixedCode);

                            // 对商户优惠券进行处理
                            ShopTicket shopTicket = shopTicketService.getById(fixedCode.getShopTicketId());
                            Integer updateBalance = shopTicket.getBalance() + balance;
                            shopTicket.setBalance(updateBalance);
                            shopTicketService.updateById(shopTicket);
                        }
                    }
                }
                return Result.modifyReturn(true, "固定码过期处理成功");
            }
            return Result.error();
        } finally {
            boolean unlock = redisLockService.unlock(Const.FIXED_TICKET);
            if (!unlock) {
                ExceptionUtils.sendRequestInfo("2.0固定码优惠券自动回收解锁失败", new StringBuilder());
            }
        }
 
  

// 这一行代码是新增优化的代码
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);

// 将两个参数从字符串转化为LocalDateTime类型
LocalDateTime parseStopStartDate = LocalDateTime.parse(param.getStopStartDate(), dateTimeFormatter1);
LocalDateTime parseStopEndtDate = LocalDateTime.parse(param.getStopEndDate(),dateTimeFormatter1);
Duration duration = Duration.between(parseStopStartDate, parseStopEndtDate);
long toDays = duration.toDays();
// 将到期时间往后推toDays天
LocalDateTime endTime = LocalDateTime.parse(param.getEndTime(), dateTimeFormatter1);
endTime.plusDays(toDays);
// 再将endTime转化为字符串然后存到param里面
String endTimeStr = dateTimeFormatter1.format(endTime);
param.setEndTime(endTimeStr);

LocalDateTime :算数操作参考

public class Main {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2019, 10, 26, 20, 30, 59);
        System.out.println(dt);
        // 加5天减3小时:
        LocalDateTime dt2 = dt.plusDays(5).minusHours(3);
        System.out.println(dt2); // 2019-10-31T17:30:59
        // 减1月:
        LocalDateTime dt3 = dt2.minusMonths(1);
        System.out.println(dt3); // 2019-09-30T17:30:59
    }
}

参考网站:
https://www.liaoxuefeng.com/wiki/1252599548343744/1303871087444002

你可能感兴趣的:(JAVA)