package com.nuvole.util;
|
|
import java.math.RoundingMode;
|
import java.text.DecimalFormat;
|
import java.text.NumberFormat;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Calendar;
|
import java.util.List;
|
|
/**
|
* 计算工具类
|
*
|
* @Author: lc
|
* @Date: 2019/5/23 19:10
|
*/
|
public class ComputeUtil {
|
|
/**
|
* 计算百分比
|
*
|
* @param num 占比值
|
* @param total 总数
|
* @param scale 小数
|
* @Author: lc
|
* @Date: 2019/8/22 10:32
|
*/
|
public static String percentum(double num, double total, int scale) {
|
if (num == 0 || total == 0) {
|
return "0";
|
}
|
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
|
//可以设置精确几位小数
|
df.setMaximumFractionDigits(scale);
|
//模式 例如四舍五入
|
df.setRoundingMode(RoundingMode.HALF_UP);
|
double accuracy_num = num / total * 100;
|
return df.format(accuracy_num);
|
}
|
|
|
/**
|
* 获取两个日期之间所有日期(年月日)
|
*
|
* @Author: lc
|
* @Date: 2019/8/23 13:41
|
*/
|
public static List<String> getBetweenDates(String begin, String end, String formatString) {
|
SimpleDateFormat format = new SimpleDateFormat(formatString);
|
List<String> result = new ArrayList<String>();
|
try {
|
Calendar tempStart = Calendar.getInstance();
|
tempStart.setTime(format.parse(begin));
|
tempStart.add(Calendar.DAY_OF_YEAR, 1);
|
|
Calendar tempEnd = Calendar.getInstance();
|
tempEnd.setTime(format.parse(end));
|
result.add(format.format(format.parse(begin)));
|
while (tempStart.before(tempEnd)) {
|
result.add(format.format(tempStart.getTime()));
|
tempStart.add(Calendar.DAY_OF_YEAR, 1);
|
}
|
result.add(format.format(format.parse(end)));
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return result;
|
}
|
|
/**
|
* 获取俩个日期间的所有日期(年月)
|
*
|
* @Author: lc
|
* @Date: 2019/8/24 16:31
|
*/
|
public static List<String> getBetweenMonths(String minDate, String maxDate, String formatString) {
|
ArrayList<String> result = new ArrayList<String>();
|
try {
|
SimpleDateFormat sdf = new SimpleDateFormat(formatString);//格式化为年月
|
|
Calendar min = Calendar.getInstance();
|
Calendar max = Calendar.getInstance();
|
min.setTime(sdf.parse(minDate));
|
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
|
|
max.setTime(sdf.parse(maxDate));
|
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
|
|
Calendar curr = min;
|
while (curr.before(max)) {
|
result.add(sdf.format(curr.getTime()));
|
curr.add(Calendar.MONTH, 1);
|
}
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
|
return result;
|
}
|
|
}
|