Java计算同比环比
同比环比计算基本概念和计算公式
同比率:本年数据比上一年数据增长或减少的比率
同比率计算公式:rate = (本年数据 - 前一年数据) / 前一年数据
实例:2020年10月游客数量为80W,2019年10月游客数量为100W,2018年10月游客数量为90W
2020年同比率为:rate :(80 - 100)/100 * 100%= -20%
2019年同比率为:rate :(100 - 900)/900 * 100%= +11%
(“+” 为增长,“-”为降低)
环比率:本月(季度)数据比上个月(季度)数据增长或减少的比率
环比率计算公式:rate = (本月数据 - 上个月数据) / 上个月数据
实例:2020年10月游客数量为100W,2020年9月游客数量为90W,2020年7月游客数量为80W
2020年10月同比率为:rate :(100 - 90)/90 * 100%= +11%
2019年10月同比率为:rate :(90- 80)/800 * 100%= +12.5%
注:同比环比计算公式是相同的,但计算数据对应的时间是不同的
代码实现逻辑
通过Calendar等时间函数和HashMap,[ hashmap(key,value) key为时间,value为该时间对应的值]。将key和value一一对应的存入集合中,通过对key进行操作,再用key获取HashMap中相对应的value,套公式计算(重点在于对时间(key)的操作,通过key可直接获取value进行计算)
详细逻辑步骤
首先通过SQL语句获取数据库中相应的时间和该时间对应的数据,按时间分组排序
SELECT
DATAS.DATE AS NAME ,
SUM( DATAS.VISITORSUM) AS VALUE,
2 AS sfzj,
'' AS bfb
FROM
(SELECT TOURIST.* ,CONCAT(YEAR,'年',QUARTER,'月') AS DATE
FROM TOURISTFLOW TOURIST)DATAS
GROUP BY DATAS.DATE
ORDER BY DATAS.DATE
接着设置时间范围,将最早的时间减去一年设为最小时间,最后的时间为最大时间,以此为范围即可保证覆盖所有数据
// 设置时间范围
// 获取最前的时间的第一个列表
analyzeBean firstTimeSubway = analyzeByYear.get(0);
String startTime = firstTimeSubway.getTime();
// 获取最后时间的最后一个列表
analyzeBean endTimeSubway = analyzeByYear.get(analyzeByYear.size() - 1);
String endTime = endTimeSubway.getTime();
// 时间格式转换
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");
Date parse = format.parse(startTime);
Date parse1 = format.parse(endTime);
Calendar c = Calendar.getInstance();
c.setTime(parse);
c.add(Calendar.YEAR, -1);
Date y = c.getTime();
// 获取最前的时间的前一年的时间作为查询范围
String firstTime = format.format(y);
analyzeRequestBean.setStartTime(firstTime);
Calendar c1 = Calendar.getInstance();
c1.setTime(parse1);
Date y1 = c1.getTime();
// 获取最后一年的时间作为查询范围
String lastTime = format.format(y1);
analyzeRequestBean.setStartTime(lastTime);
在将所有数据的结果集存入HashMap中 hash(key,value) key为时间,value为数据值
hashMap.put(time, analyzeByYear.get(i).getValue());
最后通过for循环和CaleCndar函数和Date函数对时间(key)进行相应的操作,再通过时间(key)到HashMap中找对应的值进行计算
for (int i = 0; i < analyzeByYear.size(); i++) {
AnalyzeBean analyzeBean = analyzeByYear.get(i);
if (i == 0) {
// 是否增长("0:降低 1:增加 2:既不增长也不降低")
analyzeBean.setSfzj(2);
analyzeBean.setBfb(null);
} else {
SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月");
// 当前数据
Date parse2 = format2.parse(analyzeBean.getTime());
Calendar c2 = Calendar.gaetInstance();
c2.setTime(parse2);
c2.add(Calendar.YEAR, 0);
Date t = c2.getTime();
String time = format2.format(t);
Integer value = hashMap.get(time);
// 往年数据
Date parse3 = format2.parse(time);
Calendar c3 = Calendar.getInstance();
c3.setTime(parse3);
c3.add(Calendar.YEAR, -1);
Date year = c3.getTime();
String time1 = format2.format(year);
Integer value1 = hashMap.get(time1);
if (null != value1 && null != value) {
if (value.equals(value1)) {
analyzeBean.setSfzj(2);
analyzeBean.setBfb(null);
} else {
if (value > value1) {
analyzeBean.setSfzj(1);
} else {
analyzeBean.setSfzj(0);
}
// 2个值减法 绝对值
int abs = Math.abs(value - value1);
float a = (float) (abs) / (float) value1 * 100;
analyzeBean.setBfb(a + "");
}
} else {
analyzeBean.setSfzj(2);
analyzeBean.setBfb(null);
}
}
}
同比实例代码:
// 求同比
@Override
public Result getAnalyzeByYear(AnalyzeRequestBean analyzeRequestBean) {
try {
// 检查参数
if (null == analyzeRequestBean) {
return ResultUtil.fail(ResultEnum.PARAMS_ERROR);
}
a
List<AnalyzeBean> analyzeByYear
= InfoMapper.getAnalyzeByYear(analyzeRequestBean);
if (analyzeByYear == null || analyzeByYear.size() == 0) {
return ResultUtil.ok(null);
}
// 设置时间范围
// 获取最前的时间的第一个列表
analyzeBean firstTimeSubway = analyzeByYear.get(0);
String startTime = firstTimeSubway.getTime();
// 获取最后时间的最后一个列表
analyzeBean endTimeSubway = analyzeByYear.get(analyzeByYear.size() - 1);
String endTime = endTimeSubway.getTime();
// 时间格式转换
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");
Date parse = format.parse(startTime);
Date parse1 = format.parse(endTime);
Calendar c = Calendar.getInstance();
c.setTime(parse);
c.add(CaleCndar.YEAR, -1);
Date y = c.getTime();
// 获取最前的时间的前一年的时间作为查询范围
String firstTime = format.format(y);
analyzeRequestBean.setStartTime(firstTime);
Calendar c1 = Calendar.getInstance();
c1.setTime(parse1);
Date y1 = c1.getTime();
// 获取最后一年的时间作为查询范围
String lastTime = format.format(y1);
analyzeRequestBean.setStartTime(lastTime);
// 把大范围的结果集都放入hashMap中
HashMap<String, Integer> hashMap = new HashMap<>();
for (int i = 0; i < analyzeByYear.size(); i++) {
analyzeBean analyzeBean = analyzeByYear.get(i);
SimpleDateFormat format1 = new SimpleDateFormat("yyyy年MM月");
Date parse2 = format1.parse(analyzeBean.getTime());
Calendar c2 = Calendar.getInstance();
c2.setTime(parse2);
c2.add(Calendar.YEAR, 0);
Date t = c2.getTime();
String time = format1.format(t);
hashMap.put(time, analyzeByYear.get(i).getValue());
}
for (int i = 0; i < analyzeByYear.size(); i++) {
AnalyzeBean analyzeBean = analyzeByYear.get(i);
if (i == 0) {
// 是否增长("0:降低 1:增加 2:既不增长也不降低")
analyzeBean.setSfzj(2);
analyzeBean.setBfb(null);
} else {
SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月");
// 当前数据
Date parse2 = format2.parse(analyzeBean.getTime());
Calendar c2 = Calendar.gaetInstance();
c2.setTime(parse2);
c2.add(Calendar.YEAR, 0);
Date t = c2.getTime();
String time = format2.format(t);
Integer value = hashMap.get(time);
// 往年数据
Date parse3 = format2.parse(time);
Calendar c3 = Calendar.getInstance();
c3.setTime(parse3);
c3.add(Calendar.YEAR, -1);
Date year = c3.getTime();
String time1 = format2.format(year);
Integer value1 = hashMap.get(time1);
if (null != value1 && null != value) {
if (value.equals(value1)) {
analyzeBean.setSfzj(2);
analyzeBean.setBfb(null);
} else {
if (value > value1) {
analyzeBean.setSfzj(1);
} else {
analyzeBean.setSfzj(0);
}
// 2个值减法 绝对值
int abs = Math.abs(value - value1);
float a = (float) (abs) / (float) value1 * 100;
analyzeBean.setBfb(a + "");
}
} else {
analyzeBean.setSfzj(2);
analyzeBean.setBfb(null);
}
}
}
return ResultUtil.ok(analyzeByYear);
} catch (ParseException ex) {
ex.printStackTrace();
}
return ResultUtil.ok(null);
环比类似,只是把c.add(Calendar.YEAR, 0);换成c.add(Calendar.MONTH, 0)
实现逻辑其实不难,只是我写复杂了,如有更好的方法欢迎留言交流讨论## Java计算同比环比