计算运行年份
错误1:结果不准确
String installTime = collecotrVo.getInstallTime();
DateTime dateTime = DateUtil.parseDate(installTime);
Calendar cal1 = Calendar.getInstance();
cal1.setTime(dateTime);
int i1 = cal1.get(Calendar.YEAR);
Calendar cal = Calendar.getInstance();
int i = cal.get(Calendar.YEAR);
collecotrVo.setOperationYear(i-i1);
错误2:
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int mon = cal.get(Calendar.MONTH)+1;
int day = cal.get(Calendar.DAY_OF_MONTH);
DateTime dateTime = DateUtil.parseDate(installTime);
Calendar cal1 = Calendar.getInstance();
cal1.setTime(dateTime);
int year1 = cal1.get(Calendar.YEAR);
int mon1 = cal1.get(Calendar.MONTH)+1;
int day1 = cal1.get(Calendar.DAY_OF_MONTH);
int y= year-year1;
int m= mon-mon1;
int d= day-day1;
if (y==0){
excel.setOperationYear("小于1年");
}else if(y>0&&m<0){
DecimalFormat df = new DecimalFormat("0.00");
String result = df.format((float)((m+12)*30+d)/365);
excel.setOperationYear(y+"."+result.substring(result.indexOf(".")+1));
}
正确:
private void calculateOperationYear(String installTime, AreaExceptionPointExcel excel) throws ParseException {
SimpleDateFormat ft=new SimpleDateFormat("yyyy-MM-dd");
long d = DateUtil.between(DateUtil.date(), ft.parse(installTime), DateUnit.DAY);
if (d<365){
excel.setOperationYear("小于1年");
}else {
DecimalFormat df = new DecimalFormat("0.00");
String result = df.format(((float)d/365));
excel.setOperationYear(result);
}
}