xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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;
    }
 
}