文章目录
- 前言
- 一、保留指定小数位
- 二、获取bean
- 三、假分页计算
- 四、星期计算
- 五、经纬度计算距离
前言
一、保留指定小数位
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
public class OffSetPointUtil {
/**
* 保留指定小数位数
*
* @param value 原始double值
* @param scale 小数位数
* @return 保留指定小数位数后的字符串形式
*/
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);
if (StringUtils.indexOf(bd.toString(), ".0") != -1) {
return bd.toString().substring(0, StringUtils.indexOf(bd.toString(), ".0"));
}
return bd.toString();
}
}
二、获取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);
}
}
三、假分页计算
包含一些自定义类,可根据项目具体封装结构修改
import com.gsafety.bg.gsdss.common.page.PageResult;
import org.springframework.data.domain.Pageable;
import java.util.List;
public class PageStaticUtils<T> {
/**
* 后端计算分页
*
* @param list list
* @param page page
* @return PageResult
*/
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;
}
}
四、星期计算
import java.time.LocalDate;
public class DayOfWeekUtil {
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;
}
}
五、经纬度计算距离
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;
}
/**
* 根据两点间经纬度坐标(double值),计算两点间距离,单位为米
*
* @param centerLng 初始点经度
* @param centerLat 初始点纬度
* @param targetLng 目标点经度
* @param targetLat 目标点纬度
*/
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;
}
}