1、时间处理的工具类
public class DateTimeFormatUtils {
/**
* 将Date类型时间转化未String类型
* @param date
* @param formatter
* @return
*/
public static String date2String(Date date, String formatter){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter);
return simpleDateFormat.format(date);
}
/**
* 将String类型时间转化为Date类型
* @param dateStr
* @param formatter
* @return
* @throws ParseException
*/
public static Date string2Date(String dateStr, String formatter) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatter);
return simpleDateFormat.parse(dateStr);
}
/**
* 将Date类型转化未LocalDateTime类型
* @param date
* @return
*/
public static LocalDateTime date2LocalDateTime(Date date){
String dateStr = date2String(date, "yyyy-MM-dd HH:mm:ss");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return LocalDateTime.parse(dateStr, formatter);
}
/**
* 将LocalDateTime转化为Date
* @param localDateTime
* @return
* @throws ParseException
*/
public static Date localDateTime2Date(LocalDateTime localDateTime) throws ParseException {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String localDateTimeStr = formatter.format(localDateTime);
return string2Date(localDateTimeStr, "yyyy-MM-dd HH:mm:ss");
}
2、手动将数据量大的list,分割成许多小的list,并保存到一个大的List中,相当于手动分页:
public static <T> List<List<T>> getListPage(List<T> list, int pageSize){
double size = list.size();
int remainder = (int) (size % pageSize);
int pageTotal = (int) Math.ceil(size/pageSize);
List<List<T>> resultList = new ArrayList<>();
for (int i = 1; i <= pageTotal; i++){
int maxNum = Math.min((int)size, pageSize * i);
int minNum = pageSize*(i-1);
List<T> result = list.subList(minNum, maxNum);
resultList.add(result);
}
return resultList;
}
3、两种深拷贝的工具类
/**
* 深拷贝工具类
* @param originObject 原对象信息
* @return 深拷贝后的对象信息
* @throws Exception
*/
public static Object deepCopy(Object originObject) throws Exception {
//通过输出流读取数据
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(originObject);
//通过输入流将输出流中的对象的字节数据读取出来
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
return objectInputStream.readObject();
}
/**
* 通过JSON的方式实现深拷贝
* @param originObject
* @param clazz
* @return
* @param <T>
*/
public static <T> T deepCopy(Object originObject, Class<T> clazz) {
String jsonString = JSON.toJSONString(originObject);
return JSON.parseObject(jsonString, clazz);
}
4、创建多线程的工具类
public class ThreadUtils {
/**
* 核心线程数 = cpu 核心数 + 1
*/
private final static int core = Runtime.getRuntime().availableProcessors() + 1;
private final static Logger logger = LoggerFactory.getLogger(ThreadUtils.class);
public static ThreadPoolTaskExecutor threadPoolTaskExecutor() {
logger.info("=======线程池初始化开始=======");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(core);
executor.setMaxPoolSize(core * 2);
executor.setQueueCapacity(128);
executor.setKeepAliveSeconds(300);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
logger.info("=======线程池初始化开始=======");
return executor;
}
}
5、创建验证码的工具类
public class VerificationCodeUtils {
/**
* 生成验证码图片的宽度
*/
private static final int width = 100;
/**
* 生成验证码图片的高度
*/
private static final int height = 30;
/**
* 字符样式
*/
private static final String[] fontNames = { "宋体", "楷体", "隶书", "微软雅黑" };
/**
* 定义验证码图片的背景颜色为白色
*/
private static final Color bgColor = new Color(255, 255, 255);
/**
* 生成随机
*/
private static final Random random = new Random();
/**
* 定义code字符
*/
private static final String codes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* 记录随机字符串
*/
@Getter
@Setter
private static String text;
/**
* 获取一个随意颜色
* @return
*/
private static Color randomColor() {
int red = random.nextInt(150);
int green = random.nextInt(150);
int blue = random.nextInt(150);
return new Color(red, green, blue);
}
/**
* 获取一个随机字体
*
* @return
*/
private static Font randomFont() {
String name = fontNames[random.nextInt(fontNames.length)];
int style = random.nextInt(4);
int size = random.nextInt(5) + 24;
return new Font(name, style, size);
}
/**
* 获取一个随机字符
*
* @return
*/
private static char randomChar() {
return codes.charAt(random.nextInt(codes.length()));
}
/**
* 创建一个空白的BufferedImage对象
*
* @return
*/
private static BufferedImage createImage() {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) image.getGraphics();
//设置验证码图片的背景颜色
g2.setColor(bgColor);
g2.fillRect(0, 0, width, height);
return image;
}
public static BufferedImage getImage() {
BufferedImage image = createImage();
Graphics2D g2 = (Graphics2D) image.getGraphics();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4; i++) {
String s = randomChar() + "";
sb.append(s);
g2.setColor(randomColor());
g2.setFont(randomFont());
float x = i * width * 1.0f / 4;
g2.drawString(s, x, height - 8);
}
text = sb.toString();
drawLine(image);
return image;
}
/**
* 绘制干扰线
*
* @param image
*/
private static void drawLine(BufferedImage image) {
Graphics2D g2 = (Graphics2D) image.getGraphics();
int num = 5;
for (int i = 0; i < num; i++) {
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
int x2 = random.nextInt(width);
int y2 = random.nextInt(height);
g2.setColor(randomColor());
g2.setStroke(new BasicStroke(1.5f));
g2.drawLine(x1, y1, x2, y2);
}
}
public static void output(BufferedImage image, OutputStream out) throws IOException {
ImageIO.write(image, "JPEG", out);
}
}
6、将Map中的值通过反射赋值给对象
public static <T> T setEntityValue(Map<String,String> itemMap, Class<T> clazz){
try {
Class<?> aClass = Class.forName(clazz.getName());
Field[] declaredFields = aClass.getDeclaredFields();
T object = (T) aClass.newInstance();
for (Field field : declaredFields) {
String name = field.getName();
String fieldValue = itemMap.get(name);
if (StringUtils.isNotEmpty(fieldValue)){
field.setAccessible(true);
field.set(object, fieldValue);
}
}
return object;
} catch (Exception e){
throw new ServiceException("通过反射给" + clazz.getName() + "字段赋值时出错:" + e);
}
}