importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.util.StringUtils;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Calendar;importjava.util.Date;importjava.util.List;/*** @ClassName BatchNumberUtil
* @Description 批次号工具
* @Author 北海派
* @Date 2020/5/20 12:25
* @Version 1.0
**/
public classBatchNumberUtil {private static final Logger log = LoggerFactory.getLogger("BatchNumberUtil.class");/*** 校验格式
*
*@parambatchNumber 20200508
*@return
*/
public static booleanisValidDate(String batchNumber) {boolean result = true;if (!StringUtils.isEmpty(batchNumber)) {
SimpleDateFormat format= new SimpleDateFormat("yyyyMMdd");try{
format.setLenient(false);
format.parse(batchNumber);
}catch(ParseException e) {
result= false;
log.error("批次号格式异常:{}", batchNumber);
}
}else{
result= false;
}returnresult;
}/*** 获取生产日期和失效日期
*
*@parambatchNumber 批次号
*@parammonth 效期(月数)
*@return
*/
public static ListgetDates(String batchNumber, Integer month) {
List dateList = new ArrayList<>();
Calendar calendar=Calendar.getInstance();
Date production= null;
Date invalid;if (!StringUtils.isEmpty(batchNumber) && month != null && month > 0) {
SimpleDateFormat format= new SimpleDateFormat("yyyyMMdd");try{
format.setLenient(false);
production=format.parse(batchNumber);
}catch(ParseException e) {
log.error("批次号格式异常:{}", batchNumber);
}if (null !=production) {
calendar.setTime(production);
calendar.add(Calendar.MONTH, month);
invalid=calendar.getTime();
dateList.add(0, production);
dateList.add(1, invalid);
}
}returndateList;
}
}