通用工具类

package org.springblade.common.utils;

import com.alipay.api.domain.MemberInfo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.czCombatSand.systemSettings.entity.SystemSettingsEntity;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Pattern;

/**
 * 通用工具类
 *
 * @author Chill
 */
public class CommonUtil {
    /**
     * 获取当前时间字符串
     *
     * @return
     */
    public static String getCurrentTime() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    /**
     * 当前时间转成时间戳
     *
     * @return
     */
    public static long dateToLong() {
        Date date = new Date();
        long times = date.getTime();
        return times;
    }

    /**
     * 指定时间转成时间戳
     *
     * @return
     */
    public static long suredateToLong(Date date) {
        long times = date.getTime();
        return times;
    }

    /**
     * 返回百分比(两位)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static String getPercent(Double d1, Double d2) {
        if (d1 == null || d2 == null || d2 <= 0) {
            return "0.00%";
        }
        NumberFormat percent = NumberFormat.getPercentInstance();
        percent.setMaximumFractionDigits(2);
        percent.setMinimumFractionDigits(2);
        return percent.format(d1 / d2);
    }

    /**
     * 返回百分比(整的)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static String getPercentNotTwo(Double d1, Double d2) {
        if (d1 == null || d2 == null || d2 <= 0) {
            return "0%";
        }
        NumberFormat percent = NumberFormat.getPercentInstance();
        percent.setMaximumFractionDigits(0);
        percent.setMinimumFractionDigits(0);
        return percent.format(d1 / d2);
    }

    /**
     * 获取近6个月的时间
     *
     * @return
     */
    public static List<String> getRecentSixMonth() {
        List<String> resultList = new ArrayList<String>();
        Calendar cal = Calendar.getInstance();//近六个月
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
        for (int i = 0; i < 6; i++) {
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
            resultList.add(String.valueOf(cal.get(Calendar.YEAR)) + "-" + (cal.get(Calendar.MONTH) + 1 < 10 ? "0" + (cal.get(Calendar.MONTH) + 1) : (cal.get(Calendar.MONTH) + 1)));
        }
        Collections.sort(resultList);
        return resultList;
    }

    /**
     * 获取两个时间之间的间隔天数
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 天数          例如2018-11-01 00:00:00至2018-11-30 23:59:59  返回为30
     */
    public static Integer getBetweenDays(Date startTime, Date endTime) {
        int betweenDays = 0;
        long start = startTime.getTime();
        long end = endTime.getTime();

        betweenDays = (int) (Math.abs(end - start) / (24 * 3600 * 1000));

        return (betweenDays + 1);
    }

    // 获得本周一0点时间
    public static Date getTimesWeekmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    // 获得本周日24点时间
    public static Date getTimesWeeknight() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getTimesWeekmorning());
        cal.add(Calendar.DAY_OF_WEEK, 7);
        return cal.getTime();
    }

    /**
     * 获取昨天日期
     * @return
     */
    public static String getYesterday() {
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();
        SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd");
        return sp.format(d);
    }

    /**
     * 获取当前的年份
     * @return
     */
    public static String getYearTime() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
        return dateFormat.format(date);
    }

    /**
     * 比较两个日期大小    true 开始时间大于结束时间  false 结束时间大于开始时间
     *
     * @param beginTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    public static Boolean compareTime(String beginTime, String endTime) throws ParseException {
        Boolean bool;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (dateFormat.parse(beginTime).getTime() < dateFormat.parse(endTime).getTime()) {
            bool = false;
        } else {
            bool = true;
        }
        return bool;
    }

    /**
     * 两者相除向下取整
     *
     * @param a
     * @param b
     * @return
     */
    public static String compareDivision(String a, String b) {
        BigDecimal aBig = new BigDecimal(a);
        BigDecimal bBig = new BigDecimal(b);
        BigDecimal bigDecimal = aBig.divide(bBig, 0, BigDecimal.ROUND_DOWN);
        return bigDecimal.toString();
    }

    /**
     * 获取list集合中的最大值
     * @param list
     * @return
     */
    public static List<String> getMaxValue(List<String> list) {
        List<String> list1=new ArrayList<>();
        int max = 0;
        for (String str : list) {
            if (Integer.valueOf(str) > max) {
                max = Integer.valueOf(str);
            }
        }
        //总共7个注意一下
        for (int i = 0; i <7 ; i++) {
            list1.add(String.valueOf(max));
        }
        return list1;
    }

    /**
     * 将1,2,3,4.。转换成 "一", "二", "三", "四",输出
     * @param number
     * @return
     */
    public static String toChinese(String number) {
        //将用户输入的字符串转换成char数组
        char[] numChar = number.toCharArray();
        String[] chinese = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        String[] unit = {"十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
        StringBuffer sb = new StringBuffer();
        //获取用户输入的字符串长度
        int size = numChar.length;
        //遍历char数组
        for (int i = 0; i < numChar.length; i++) {
            //将用户输入的数字拿出来
            //比如用户输入1234数字
            //numChar['1','2','3','4']
            //numChar[0]
            //因此拿到的阿拉伯数字就是num=1
            int num = Integer.parseInt(String.valueOf(numChar[i]));
            //因为咱们的汉字和数组下标一一对应,所以可以直接将拿到的阿拉伯数字作为中文数组的下标取汉字
            //因此这时chinese[1]对应的就是汉字"一"
            //再就是打印一个汉字数字就加一个单位
            //通过观察可以找出阿拉伯数字和单位的关系,关系如下
            //1234长度为4,对应到计数单位数组4索引是”万”单位
            //1是千单位,以4-1就可以得到计数单位数组的"千”单位
            //4-1=3|unit[3]拿第一个千
            //4-2=2|unit[2]拿第二个百
            //4-3=1|unit[1]拿第三个十
            if (i != 0) {
                //数组长度-i
                //上述对应成代码就是 size-i
                //因为计数单位数组省略了"个”单位
                //所以需要减去1,取出所有单位
                //单位+数字|千二百三十四
                sb.append(unit[size - i - 1] + chinese[num]);
            } else {
                //这里是第一个数字,第一个数字前面没有计数单位,所以单独拿出处理
                sb.append(chinese[num]);
            }
        }
        //结果就是一千二百三十四
        return sb.toString();
    }

    public static String excelDate(String number) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd");//时间格式话

        Date parse = sf.parse("1900/01/01");

        int i = Integer.parseInt(number);

        calendar.setTime(parse);
//        i-2的原因是实际天数比获取的天数少2天
        calendar.add(Calendar.DATE, i - 2);

        String format = sf.format(calendar.getTime());

        return format;
    }


    /**
     * 逗号拼接的字符串转成数组
     * @param str
     * @return
     */
    public static List<String> stringToList(String str) {
        List result = Arrays.asList(str.split(","));
        return result;
    }

    /**
     * 时间字符串转Date类型(带时分秒)
     * @param str
     * @return
     */
    public static Date transferString2Date(String s) {
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);
        } catch (ParseException e) {
            //LOGGER.error("时间转换错误, string = {}", s, e);
        }
        return date;
    }

    /**
     * 时间字符串转Date类型(不带时分秒)
     * @param str
     * @return
     */
    public static Date transferString2Date(String s) {
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(s);
        } catch (ParseException e) {
            //LOGGER.error("时间转换错误, string = {}", s, e);
        }
        return date;
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(CommonUtil.getPercentNotTwo(Double.valueOf(53),Double.valueOf(4)));

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值