前言
最近有一个项目用户在测试阶段反馈了一个错误,如下java.text.ParseException: Unparseable date: “24-MAR-26”,这个错误大致意思是SimpleDateFormat处理不了这种类型的时间,因为我们在开发的时候规定的时间类型一般为SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM”);这种时间类型,所以用户提供的类型为如下这种的处理不了。
我的具体代码为
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
String dateString = formatter.format(currentTime);
Calendar bef = Calendar.getInstance();
Calendar aft = Calendar.getInstance();
try {
bef.setTime(formatter.parse("2020-12-12"));
aft.setTime(formatter.parse(dateString));
} catch (ParseException e) {
e.printStackTrace();
}
int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
int difference = month + result;
if (difference > 6) {
return true;
}
这段代码是为了判断现在日期是否大于指定日期6个月