校验XX是否在有效期内

简单介绍:做删除的时候,需要判断XX的日期,如果在有效期内,则不能删除,已过期,或者是未生效都可以删除。刚看到的时候,就有点懵逼了,因为不知道该怎么判断,最初,想的是查询XX的开始日期和截止日期,看看当前时间 new Date() 是否在between  startTime  and  endTime 之外,如果是的话,就可以删除,否则,不能删除,想法倒是挺美的,关键是怎么写sql啊,难道sql还能返回true false?结果就卡在这里了。后来,我灵机一动,终于想出来了该怎么做,hah༺༺超༒神༻༻( • ̀ω•́ )✧

思路描述:可以查询XX的开始时间和结束时间,得到他们的字符串,然后在和系统当前时间的字符串进行比较,如果在区间之外的,妥妥的能删除·············上代码

代码:

//后台java代码
public String queryValidDate(String contractId) throws Exception {
if (Tools.isEmpty(contractId)){
return "fail";
}else{
Contract contractObj = (Contract)dao.findForObject("ContractMapper.getContractObj",contractId);
String start = contractObj.getEffectiveTime();
String end = contractObj.getFailureTime();
Date currentTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowTime = sdf.format(currentTime);
if(DateUtil.compareDate(nowTime,start)){
if (DateUtil.compareDate(end,nowTime)){
return "valid"; //合同处于有效区间内不能删除
}else{
return "success";//合同过期可以删除
}
}else{
return "success";//合同还未生效可以删除
}
}
} 
//工具类里的相关方法compareDate
public static boolean compareDate(String s, String e) {
if(fomatDate(s)==null||fomatDate(e)==null){
return false;
}
return fomatDate(s).getTime() >=fomatDate(e).getTime();
} 
//工具类里的方法formateDate
public static Date fomatDate(String date) {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {
return fmt.parse(date);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
} 
//parse()方法是jdk提供的方法

 总结:我真是太୧(๑•̀◡•́๑)૭了,简单记录一点点,每天成长一点点。

原文地址:https://www.cnblogs.com/xuchao0506/p/9931262.html