| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- package com.steerinfo.dil.util;
- import org.springframework.web.multipart.MultipartFile;
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.math.BigDecimal;
- import java.text.DateFormat;
- import java.text.DecimalFormat;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import java.util.regex.Pattern;
- /**
- * @ author :TXF
- * @ time :2021/8/25 11:25
- */
- public class DataChange {
- static String tempDir = "/temp/";
- /**
- * 时间转换类
- * 处理了三种类型 yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss 时间戳类型(带毫秒数时间戳13位)
- *
- * @param vueDate
- * @return
- */
- public static Date dataToDate(Object vueDate) {
- if (vueDate instanceof Date) {
- return (Date) vueDate;
- } else if (vueDate instanceof Long) {
- return new Date((Long) vueDate);
- } else {
- try {
- String str = String.valueOf(vueDate);
- if (judgeNumber(str) && str.length() == 13) {
- return new Date(Long.parseLong(str));
- } else if (str.contains("-")) {
- if (str.length() == "yyyy-MM-dd".length()) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- return sdf.parse(str);
- }
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- return sdf.parse(str);
- } else if (str.contains("/")) {
- if (str.length() == "yyyy/MM/dd".length()) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
- return sdf.parse(str);
- }
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
- return sdf.parse(str);
- }
- } catch (Exception e) {
- System.out.println("时间解析错误!返回null");
- return null;
- }
- }
- return null;
- }
- /**
- * 判断是否纯数字(不带小数点)仅供上面方法使用
- *
- * @param str
- * @return
- */
- public static boolean judgeNumber(String str) {
- Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
- return pattern.matcher(str).matches();
- }
- /**
- * 数据转换成BigDecimal
- *
- * @param data
- * @return
- */
- public static BigDecimal dataToBigDecimal(Object data) {
- if (data != null) {
- if (data instanceof BigDecimal) {
- return (BigDecimal) data;
- } else {
- String str = String.valueOf(data);
- BigDecimal decimal = BigDecimal.ZERO;
- if (!"".equals(str)) {
- try {
- decimal = new BigDecimal(str);
- } catch (Exception e) {
- System.out.println(data + ":数据解析失败!返回0");
- return BigDecimal.ZERO;
- }
- }
- return decimal;
- }
- }
- return BigDecimal.ZERO;
- }
- /**
- * 将时间截取到天 为字符串类型 用于前端只显示到天
- *
- * @param date 传入时间
- * @return
- */
- public static Date dataToDayDate(Object date) {
- try {
- Date origin = dataToDate(date);
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- if (origin == null) {
- return null;
- }
- return sdf.parse(sdf.format(origin));
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 查询日期是否过期,过期则返回 true
- *
- * @param day 传入时间
- * @return
- */
- public static boolean isExpireDay(Object day) {
- try {
- if (dataToDayDate(day).getTime() < dataToDayDate(new Date()).getTime()) {
- return true;
- } else {
- return false;
- }
- } catch (Exception e) {
- e.printStackTrace();
- return true;
- }
- }
- /**
- * 遍历列表使只显示两位小数
- *
- * @param list
- * @param key
- */
- public static void dataTo2Number(List<Map<String, Object>> list, String... key) {
- DecimalFormat df = new DecimalFormat("0.00");
- //遍历List
- for (Map<String, Object> map : list) {
- for (String s : key) {
- //修改数据为带两位小数
- try {
- BigDecimal oldDate = dataToBigDecimal(map.get(s));
- String resultDeduction = df.format(oldDate.doubleValue());
- map.put(s, resultDeduction);
- } catch (Exception e) {
- System.out.println("原料扣减量数据有误");
- }
- }
- }
- }
- /**
- * 计算相差时间 日时分秒
- *
- * @param
- * @return
- */
- public static String calculatedTimeDifference(Date time1, Date time2) {
- long t1 = time1.getTime();
- long t2 = time2.getTime();
- if (t1 > t2) {
- long temp = t1;
- t1 = t2;
- t2 = temp;
- }
- long between = t2 - t1;
- long day = between / (24 * 60 * 60 * 1000);
- long hour = (between / (60 * 60 * 1000) - day * 24);
- long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
- long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
- return day + "天" + +hour + "时" + min + "分" + s + "秒";
- }
- /**
- * 生成带时间的八位数顺序号
- *
- * @param start 前缀
- * @param id 顺序号 主键Id
- * @return
- */
- public static String generateEightDigitsNumber(String start, Integer id) {
- id = id % 100000000; //保证不超过
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
- StringBuilder sb = new StringBuilder(start + sdf.format(new Date()));
- sb.append(
- id < 10
- ? "0000000" + id : id < 100
- ? "000000" + id : id < 1000
- ? "00000" + id : id < 10000
- ? "0000" + id : id < 100000
- ? "000" + id : id < 1000000
- ? "00" + id : id < 10000000
- ? "0" + id : id.toString()
- );
- return sb.toString();
- }
- /**
- * 根据时间段查询数据 支持只选择单个时间
- *
- * @param startTime
- * @param endTime
- * @param map
- * @param sdf
- * @return
- * @Author TXF
- * @Date 2022/1/10 23:21
- **/
- public static void queryDataByDate(String startTime, String endTime, Map<String, Object> map, SimpleDateFormat sdf) {
- if (startTime != null && !"null".equals(startTime) && endTime != null && !"null".equals(endTime)) {
- map.put("startDate", sdf.format(new Date(Long.parseLong(startTime))));
- map.put("endDate", sdf.format(new Date(Long.parseLong(endTime) + 86400000)));
- } else if (startTime != null && !"null".equals(startTime)) {
- map.put("oneDate", sdf.format(new Date(Long.parseLong(startTime))));
- } else if (endTime != null && !"null".equals(endTime)) {
- map.put("oneDate", sdf.format(new Date(Long.parseLong(endTime))));
- } else {
- map.put("oneDate", sdf.format(new Date()));
- }
- }
- /**
- * 只支持两个时间查询
- *
- * @param startTime
- * @param endTime
- * @param sdf
- * @return
- * @Author TXF
- * @Date 2022/1/15 9:08
- **/
- public static void queryDataByDateTime(String startTime, String endTime, Map<String, Object> map, SimpleDateFormat sdf) {
- SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
- if (startTime != null && !"null".equals(startTime) && endTime != null && !"null".equals(endTime)) {
- map.put("startDate", sdf.format(new Date(Long.parseLong(startTime))));
- map.put("endDate", sdf.format(new Date(Long.parseLong(endTime))));
- }
- //如果开始时间和结束时间有且只有一个为空 则只查那天的数据
- else if ((startTime != null && !"null".equals(startTime)) || (endTime != null && !"null".equals(endTime))) {
- if (startTime != null && !"null".equals(startTime)) {
- queryDataByTwoDateSon(map, startTime, sdfDate);
- }
- if (endTime != null && !"null".equals(endTime)) {
- queryDataByTwoDateSon(map, endTime, sdfDate);
- }
- } else {
- //如果两者时间都为空,则查询当天数据
- String nowDate = sdfDate.format(new Date());
- map.put("oneDate", nowDate + " 00:00:00");
- }
- }
- /**
- * 上面方法的儿子方法 如果只传入了一个时间 则查询那天的数据
- *
- * @param map
- * @param time
- * @param sdfDate
- * @return
- * @Author TXF
- * @Date 2022/1/17 16:17
- **/
- private static void queryDataByTwoDateSon(Map<String, Object> map, String time, SimpleDateFormat sdfDate) {
- Date date1 = new Date(Long.parseLong(time));
- Date date2 = new Date(Long.parseLong(time) + 86400000);
- String dayStartTime = sdfDate.format(date1);
- String dayEndTime = sdfDate.format(date2);
- map.put("startDate", dayStartTime + " 00:00:00");
- map.put("endDate", dayEndTime + " 00:00:00");
- }
- public static void getMonthStartEnd(Map<String, Object> map) {
- //
- Calendar cale = Calendar.getInstance();
- // 获取当月第一天和最后一天
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- String firstDay, lastDay;
- // 获取前月的第一天
- cale.add(Calendar.MONTH, 0);
- cale.set(Calendar.DAY_OF_MONTH, 1);
- firstDay = format.format(cale.getTime());
- // 获取前月的最后一天
- cale = Calendar.getInstance();
- cale.add(Calendar.MONTH, 1);
- cale.set(Calendar.DAY_OF_MONTH, 0);
- lastDay = format.format(cale.getTime());
- System.out.println("本月第一天和最后一天分别是 : " + firstDay + " and " + lastDay);
- map.put("firstDay", firstDay);
- map.put("lastDay", lastDay);
- }
- public static File paintWater(MultipartFile file, String text, int xOffset, int yOffset) throws Exception {
- //将文件对象转化为图片对象
- Image srcImg = ImageIO.read(file.getInputStream());
- //获取图片的宽
- int srcImgWidth = srcImg.getWidth(null);
- //获取图片的高
- int srcImgHeight = srcImg.getHeight(null);
- // 加水印
- BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
- //创建画笔
- Graphics2D graphics = bufImg.createGraphics();
- //srcImg 为上面获取到的原始图片的图片对象
- graphics.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
- //根据图片的背景设置水印颜色
- graphics.setColor(new Color(255, 0, 0, 255));
- //设置字体 画笔字体样式为微软雅黑,加粗,文字大小为60pt
- graphics.setFont(new Font("微软雅黑", Font.BOLD, 40));
- //设置水印的坐标
- int x = 0;
- if (xOffset >= 0) {
- x = xOffset;
- } else {
- x = srcImgWidth + (xOffset % srcImgWidth);
- }
- int y = 0;
- if (yOffset >= 0) {
- y = yOffset;
- } else {
- y = srcImgHeight + (yOffset % srcImgWidth);
- }
- //自定义水印 第一个参数是水印内容,第二个参数是x轴坐标,第三个参数是y轴坐标
- graphics.drawString(text, x, y);
- //时间水印
- DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- graphics.drawString(df.format(new Date()), x, y + 50);
- graphics.dispose();
- //待存储的地址
- File fileDir = new File(tempDir);
- if (!fileDir.exists() || !fileDir.isDirectory()) {
- fileDir.mkdir();
- }
- String tarImgPath = tempDir + file.getOriginalFilename();
- // 输出图片
- FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
- ImageIO.write(bufImg, "png", outImgStream);
- outImgStream.flush();
- outImgStream.close();
- //返回目标图片
- File targetFile = new File(tarImgPath);
- // targetFile.delete();//临时文件返回后,保存在其他地方然后及时删除
- return targetFile;
- }
- }
|