前言
一、数字相关工具类
1. 保留指定小数位
2. 数字转汉字
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
public class NumberUtils {
public static String round(double value, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("小数位数不能为负数");
}
BigDecimal bd = BigDecimal.valueOf(value);
bd = bd.setScale(scale, BigDecimal.ROUND_HALF_UP);
String zeros = ".";
for (int i = 0; i < scale; i++) {
zeros += "0";
}
if (bd.toString().endsWith(zeros)) {
return bd.toString().substring(0, StringUtils.indexOf(bd.toString(), zeros));
}
return bd.toString();
}
public static String convertChinese(int number) {
String[] num = {"一", "二", "三", "四", "五", "六", "七", "八", "九"};
String[] unit = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万亿"};
String result = String.valueOf(number);
char[] ch = result.toCharArray();
String str = "";
int length = ch.length;
for (int i = 0; i < length; i++) {
int c = (int) ch[i] - 48;
if (c != 0) {
str += num[c - 1] + unit[length - i - 1];
}
}
return str;
}
}
- 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
二、获取bean
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(98)
public class SpringUtil implements ApplicationContextAware {
private static Logger logger = LoggerFactory.getLogger(SpringUtil.class);
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext;
}
logger.info("ApplicationContext配置成功,applicationContext对象:" + SpringUtil.applicationContext);
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
- 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
三、分页相关工具类
1. 假分页,包含一些自定义类,可根据项目具体封装结构修改
2. 分页序号
import com.gsafety.bg.gsdss.common.page.PageResult;
import org.springframework.data.domain.Pageable;
import java.util.List;
public class PageStaticUtils<T> {
public PageResult<T> pageData(List<T> list, Pageable page) {
int total = list.size();
int pages = (total / page.getPageSize()) + (total % page.getPageSize() > 0 ? 1 : 0);
int start = page.getPageSize() * page.getPageNumber();
int end = Math.min((start + page.getPageSize()), total);
PageResult<T> pageResult = new PageResult<T>();
pageResult.setNowPage(page.getPageNumber() + 1);
pageResult.setPages(pages);
pageResult.setPageSize(page.getPageSize());
pageResult.setTotal(total);
pageResult.setList(list.subList(start, end));
return pageResult;
}
public Integer getPageNum(List<T> list, T data, Integer pageSize, Integer nowPage) {
return (list.indexOf(data) + 1) + pageSize * nowPage;
}
}
- 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
四、时间工具类
1. 星期获取
2. 时间段耗时计算 [ x时x分 ]
import java.time.LocalDate;
public class DayOfWeekUtils {
public static String week(LocalDate day) {
String[][] strArray = {{"MONDAY", "一"}, {"TUESDAY", "二"}, {"WEDNESDAY", "三"}, {"THURSDAY", "四"}, {"FRIDAY", "五"}, {"SATURDAY", "六"}, {"SUNDAY", "日"}};
String k = String.valueOf(day.getDayOfWeek());
for (int i = 0; i < strArray.length; i++) {
if (k.equals(strArray[i][0])) {
k = strArray[i][1];
break;
}
}
return "星期" + k;
}
public static String printSplitTime(LocalDateTime start, LocalDateTime end) {
Duration dur = Duration.between(start, end);
long minute = dur.toMinutes() - dur.toHours() * 60;
long hour = dur.toHours();
return hour + "时" + minute + "分";
}
}
- 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
五、经纬度距离计算
import lombok.experimental.UtilityClass;
@UtilityClass
public class DistanceUtil {
private static final double EARTH_RADIUS = 6378137;
private static double rad(double d) {
return d * Math.PI / 180.0;
}
public static double getDistance(double centerLng, double centerLat, double targetLng, double targetLat) {
double radLat1 = rad(centerLat);
double radLat2 = rad(targetLat);
double a = radLat1 - radLat2;
double b = rad(centerLng) - rad(targetLng);
double s = 2 * Math.asin(
Math.sqrt(
Math.pow(Math.sin(a / 2), 2)
+ Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
)
);
s = s * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000;
return s;
}
}
- 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
