second commit by ailanyin
This commit is contained in:
@ -5,28 +5,30 @@ import java.math.RoundingMode;
|
||||
|
||||
/**
|
||||
* 精确的浮点数运算
|
||||
*
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class BigDecimalUtil
|
||||
{
|
||||
public class BigDecimalUtil {
|
||||
|
||||
/** 默认除法运算精度 */
|
||||
/**
|
||||
* 默认除法运算精度
|
||||
*/
|
||||
private static final int DEF_DIV_SCALE = 10;
|
||||
|
||||
/** 这个类不能实例化 */
|
||||
private BigDecimalUtil()
|
||||
{
|
||||
/**
|
||||
* 这个类不能实例化
|
||||
*/
|
||||
private BigDecimalUtil() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供精确的加法运算。
|
||||
*
|
||||
* @param v1 被加数
|
||||
* @param v2 加数
|
||||
* @return 两个参数的和
|
||||
*/
|
||||
public static double add(double v1, double v2)
|
||||
{
|
||||
public static double add(double v1, double v2) {
|
||||
BigDecimal b1 = new BigDecimal(v1);
|
||||
BigDecimal b2 = new BigDecimal(v2);
|
||||
return b1.add(b2).doubleValue();
|
||||
@ -34,12 +36,12 @@ public class BigDecimalUtil
|
||||
|
||||
/**
|
||||
* 提供精确的减法运算。
|
||||
*
|
||||
* @param v1 被减数
|
||||
* @param v2 减数
|
||||
* @return 两个参数的差
|
||||
*/
|
||||
public static double sub(double v1, double v2)
|
||||
{
|
||||
public static double sub(double v1, double v2) {
|
||||
BigDecimal b1 = new BigDecimal(v1);
|
||||
BigDecimal b2 = new BigDecimal(v2);
|
||||
return b1.subtract(b2).doubleValue();
|
||||
@ -47,12 +49,12 @@ public class BigDecimalUtil
|
||||
|
||||
/**
|
||||
* 提供精确的乘法运算。
|
||||
*
|
||||
* @param v1 被乘数
|
||||
* @param v2 乘数
|
||||
* @return 两个参数的积
|
||||
*/
|
||||
public static double mul(double v1, double v2)
|
||||
{
|
||||
public static double mul(double v1, double v2) {
|
||||
BigDecimal b1 = new BigDecimal(v1);
|
||||
BigDecimal b2 = new BigDecimal(v2);
|
||||
return b1.multiply(b2).doubleValue();
|
||||
@ -61,34 +63,51 @@ public class BigDecimalUtil
|
||||
/**
|
||||
* 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
|
||||
* 小数点以后10位,以后的数字四舍五入。
|
||||
*
|
||||
* @param v1 被除数
|
||||
* @param v2 除数
|
||||
* @return 两个参数的商
|
||||
*/
|
||||
public static double div(double v1, double v2)
|
||||
{
|
||||
public static double div(double v1, double v2) {
|
||||
return div(v1, v2, DEF_DIV_SCALE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
|
||||
* 定精度,以后的数字四舍五入。
|
||||
* @param v1 被除数
|
||||
* @param v2 除数
|
||||
*
|
||||
* @param v1 被除数
|
||||
* @param v2 除数
|
||||
* @param scale 表示表示需要精确到小数点以后几位。
|
||||
* @return 两个参数的商
|
||||
*/
|
||||
public static double div(double v1, double v2, int scale)
|
||||
{
|
||||
if (scale < 0)
|
||||
{
|
||||
public static Double div(double v1, double v2, int scale) {
|
||||
if (scale < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"The scale must be a positive integer or zero");
|
||||
}
|
||||
BigDecimal b1 = new BigDecimal(v1);
|
||||
BigDecimal b2 = new BigDecimal(v2);
|
||||
if (b1.compareTo(BigDecimal.ZERO) == 0)
|
||||
{
|
||||
if (b1.compareTo(BigDecimal.ZERO) == 0) {
|
||||
return BigDecimal.ZERO.doubleValue();
|
||||
}
|
||||
return b1.divide(b2, scale, RoundingMode.HALF_UP).doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
|
||||
* 定精度,以后的数字四舍五入。
|
||||
*
|
||||
* @param v1 被除数
|
||||
* @param v2 除数
|
||||
* @param scale 表示表示需要精确到小数点以后几位。
|
||||
* @return 两个参数的商
|
||||
*/
|
||||
public static double div(String v1, String v2, int scale) {
|
||||
|
||||
BigDecimal b1 = new BigDecimal(v1);
|
||||
BigDecimal b2 = new BigDecimal(v2);
|
||||
if (b1.compareTo(BigDecimal.ZERO) == 0) {
|
||||
return BigDecimal.ZERO.doubleValue();
|
||||
}
|
||||
return b1.divide(b2, scale, RoundingMode.HALF_UP).doubleValue();
|
||||
@ -96,14 +115,13 @@ public class BigDecimalUtil
|
||||
|
||||
/**
|
||||
* 提供精确的小数位四舍五入处理。
|
||||
* @param v 需要四舍五入的数字
|
||||
*
|
||||
* @param v 需要四舍五入的数字
|
||||
* @param scale 小数点后保留几位
|
||||
* @return 四舍五入后的结果
|
||||
*/
|
||||
public static double round(double v, int scale)
|
||||
{
|
||||
if (scale < 0)
|
||||
{
|
||||
public static double round(double v, int scale) {
|
||||
if (scale < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"The scale must be a positive integer or zero");
|
||||
}
|
||||
@ -111,5 +129,23 @@ public class BigDecimalUtil
|
||||
BigDecimal one = BigDecimal.ONE;
|
||||
return b.divide(one, scale, RoundingMode.HALF_UP).doubleValue();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 计算完成率
|
||||
*
|
||||
* @param part 部分
|
||||
* @param total 总体
|
||||
* @return 完成率
|
||||
*/
|
||||
public static String calculateCompletionRate(String part,String total) {
|
||||
// eg: 67.0
|
||||
String rate = String.valueOf(
|
||||
div(
|
||||
Double.valueOf(part),
|
||||
Double.valueOf(total),2
|
||||
) * 100
|
||||
);
|
||||
// 67.0 -> 67
|
||||
return rate.substring(0,rate.length() - 2);
|
||||
}
|
||||
}
|
||||
|
@ -4,10 +4,12 @@ import org.apache.commons.lang3.time.DateFormatUtils;
|
||||
import org.apache.poi.util.LocaleUtil;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
@ -141,8 +143,9 @@ public class DateUtil extends org.apache.commons.lang3.time.DateUtils {
|
||||
}
|
||||
|
||||
public static Date getJavaDate(double date) {
|
||||
return getJavaDate(date, false, (TimeZone)null, false);
|
||||
return getJavaDate(date, false, (TimeZone) null, false);
|
||||
}
|
||||
|
||||
public static Date getJavaDate(double date, boolean use1904windowing, TimeZone tz, boolean roundSeconds) {
|
||||
Calendar calendar = getJavaCalendar(date, use1904windowing, tz, roundSeconds);
|
||||
return calendar == null ? null : calendar.getTime();
|
||||
@ -152,8 +155,8 @@ public class DateUtil extends org.apache.commons.lang3.time.DateUtils {
|
||||
if (!isValidExcelDate(date)) {
|
||||
return null;
|
||||
} else {
|
||||
int wholeDays = (int)Math.floor(date);
|
||||
int millisecondsInDay = (int)((date - (double)wholeDays) * 8.64E7D + 0.5D);
|
||||
int wholeDays = (int) Math.floor(date);
|
||||
int millisecondsInDay = (int) ((date - (double) wholeDays) * 8.64E7D + 0.5D);
|
||||
Calendar calendar;
|
||||
if (timeZone != null) {
|
||||
calendar = LocaleUtil.getLocaleCalendar(timeZone);
|
||||
@ -169,6 +172,7 @@ public class DateUtil extends org.apache.commons.lang3.time.DateUtils {
|
||||
public static boolean isValidExcelDate(double value) {
|
||||
return value > -4.9E-324D;
|
||||
}
|
||||
|
||||
public static void setCalendar(Calendar calendar, int wholeDays, int millisecondsInDay, boolean use1904windowing, boolean roundSeconds) {
|
||||
int startYear = 1900;
|
||||
int dayAdjust = -1;
|
||||
@ -195,4 +199,141 @@ public class DateUtil extends org.apache.commons.lang3.time.DateUtils {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取本周的开始时间
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static Date getBeginDayOfWeek() {
|
||||
Date date = new Date();
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(date);
|
||||
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
|
||||
if (dayofweek == 1) {
|
||||
dayofweek += 7;
|
||||
}
|
||||
cal.add(Calendar.DATE, 2 - dayofweek);
|
||||
return getDayStartTime(cal.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本周的结束时间
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static Date getEndDayOfWeek() {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(getBeginDayOfWeek());
|
||||
cal.add(Calendar.DAY_OF_WEEK, 6);
|
||||
Date weekEndSta = cal.getTime();
|
||||
return getDayEndTime(weekEndSta);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某个日期的开始时间
|
||||
* @param d
|
||||
* @return
|
||||
*/
|
||||
public static Timestamp getDayStartTime(Date d) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
if (null != d) {
|
||||
calendar.setTime(d);
|
||||
}
|
||||
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
|
||||
calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
|
||||
calendar.set(Calendar.MILLISECOND, 0);
|
||||
return new Timestamp(calendar.getTimeInMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本月的开始时间
|
||||
* @return
|
||||
*/
|
||||
public static Date getBeginDayOfMonth() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(getNowYear(), getNowMonth() - 1, 1);
|
||||
return getDayStartTime(calendar.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本月的结束时间
|
||||
* @return
|
||||
*/
|
||||
public static Date getEndDayOfMonth() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(getNowYear(), getNowMonth() - 1, 1);
|
||||
int day = calendar.getActualMaximum(5);
|
||||
calendar.set(getNowYear(), getNowMonth() - 1, day);
|
||||
return getDayEndTime(calendar.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取今年是哪一年
|
||||
* @return
|
||||
*/
|
||||
public static Integer getNowYear() {
|
||||
Date date = new Date();
|
||||
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
|
||||
gc.setTime(date);
|
||||
return Integer.valueOf(gc.get(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本月是哪一月
|
||||
* @return
|
||||
*/
|
||||
public static int getNowMonth() {
|
||||
Date date = new Date();
|
||||
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
|
||||
gc.setTime(date);
|
||||
return gc.get(2) + 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取某个日期的结束时间
|
||||
* @param d
|
||||
* @return
|
||||
*/
|
||||
public static Timestamp getDayEndTime(Date d) {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
if (null != d) {
|
||||
calendar.setTime(d);
|
||||
}
|
||||
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
|
||||
calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
|
||||
calendar.set(Calendar.MILLISECOND, 999);
|
||||
return new Timestamp(calendar.getTimeInMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本年的开始时间
|
||||
* @return
|
||||
*/
|
||||
public static Date getBeginDayOfYear() {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.set(Calendar.YEAR, getNowYear());
|
||||
cal.set(Calendar.MONTH, Calendar.JANUARY);
|
||||
cal.set(Calendar.DATE, 1);
|
||||
return getDayStartTime(cal.getTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本年的结束时间
|
||||
* @return
|
||||
*/
|
||||
public static java.util.Date getEndDayOfYear() {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.set(Calendar.YEAR, getNowYear());
|
||||
cal.set(Calendar.MONTH, Calendar.DECEMBER);
|
||||
cal.set(Calendar.DATE, 31);
|
||||
return getDayEndTime(cal.getTime());
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(getBeginDayOfMonth());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.ailanyin.common.utils;
|
||||
|
||||
import cn.hutool.core.text.StrFormatter;
|
||||
import com.ailanyin.common.constant.Constants;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import com.ailanyin.common.constant.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@ -23,7 +23,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* 获取参数不为空值
|
||||
*
|
||||
*
|
||||
* @param value defaultValue 要判断的value
|
||||
* @return value 返回值
|
||||
*/
|
||||
@ -34,7 +34,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* * 判断一个Collection是否为空, 包含List,Set,Queue
|
||||
*
|
||||
*
|
||||
* @param coll 要判断的Collection
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
@ -45,7 +45,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* * 判断一个Collection是否非空,包含List,Set,Queue
|
||||
*
|
||||
*
|
||||
* @param coll 要判断的Collection
|
||||
* @return true:非空 false:空
|
||||
*/
|
||||
@ -56,7 +56,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* * 判断一个对象数组是否为空
|
||||
*
|
||||
*
|
||||
* @param objects 要判断的对象数组
|
||||
** @return true:为空 false:非空
|
||||
*/
|
||||
@ -67,7 +67,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* * 判断一个对象数组是否非空
|
||||
*
|
||||
*
|
||||
* @param objects 要判断的对象数组
|
||||
* @return true:非空 false:空
|
||||
*/
|
||||
@ -78,7 +78,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* * 判断一个Map是否为空
|
||||
*
|
||||
*
|
||||
* @param map 要判断的Map
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
@ -89,7 +89,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* * 判断一个Map是否为空
|
||||
*
|
||||
*
|
||||
* @param map 要判断的Map
|
||||
* @return true:非空 false:空
|
||||
*/
|
||||
@ -100,7 +100,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* * 判断一个字符串是否为空串
|
||||
*
|
||||
*
|
||||
* @param str String
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
@ -111,7 +111,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* * 判断一个字符串是否为非空串
|
||||
*
|
||||
*
|
||||
* @param str String
|
||||
* @return true:非空串 false:空串
|
||||
*/
|
||||
@ -122,7 +122,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* * 判断一个对象是否为空
|
||||
*
|
||||
*
|
||||
* @param object Object
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
@ -133,7 +133,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* * 判断一个对象是否非空
|
||||
*
|
||||
*
|
||||
* @param object Object
|
||||
* @return true:非空 false:空
|
||||
*/
|
||||
@ -144,7 +144,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* * 判断一个对象是否是数组类型(Java基本型别的数组)
|
||||
*
|
||||
*
|
||||
* @param object 对象
|
||||
* @return true:是数组 false:不是数组
|
||||
*/
|
||||
@ -163,7 +163,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* 截取字符串
|
||||
*
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param start 开始
|
||||
* @return 结果
|
||||
@ -194,7 +194,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* 截取字符串
|
||||
*
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param start 开始
|
||||
* @param end 结束
|
||||
@ -246,7 +246,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
* 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
|
||||
* 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
|
||||
* 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
|
||||
*
|
||||
*
|
||||
* @param template 文本模板,被替换的部分用 {} 表示
|
||||
* @param params 参数值
|
||||
* @return 格式化后的文本
|
||||
@ -262,7 +262,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* 是否为http(s)://开头
|
||||
*
|
||||
*
|
||||
* @param link 链接
|
||||
* @return 结果
|
||||
*/
|
||||
@ -273,7 +273,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* 字符串转set
|
||||
*
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param sep 分隔符
|
||||
* @return set集合
|
||||
@ -285,7 +285,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* 字符串转list
|
||||
*
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param sep 分隔符
|
||||
* @param filterBlank 过滤纯空白
|
||||
@ -399,7 +399,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* 是否包含字符串
|
||||
*
|
||||
*
|
||||
* @param str 验证字符串
|
||||
* @param strs 字符串组
|
||||
* @return 包含返回true
|
||||
@ -421,7 +421,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
|
||||
*
|
||||
*
|
||||
* @param name 转换前的下划线大写方式命名的字符串
|
||||
* @return 转换后的驼峰式命名的字符串
|
||||
*/
|
||||
@ -490,7 +490,7 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
|
||||
/**
|
||||
* 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
|
||||
*
|
||||
*
|
||||
* @param str 指定字符串
|
||||
* @param strs 需要检查的字符串数组
|
||||
* @return 是否匹配
|
||||
@ -512,11 +512,11 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断url是否与规则配置:
|
||||
* ? 表示单个字符;
|
||||
* * 表示一层路径内的任意字符串,不可跨层级;
|
||||
* 判断url是否与规则配置:
|
||||
* ? 表示单个字符;
|
||||
* * 表示一层路径内的任意字符串,不可跨层级;
|
||||
* ** 表示任意层路径;
|
||||
*
|
||||
*
|
||||
* @param pattern 匹配规则
|
||||
* @param url 需要匹配的url
|
||||
* @return
|
||||
@ -532,4 +532,5 @@ public class StringUtil extends org.apache.commons.lang3.StringUtils
|
||||
{
|
||||
return (T) obj;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user