DataChange.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package com.steerinfo.dil.util;
  2. import org.springframework.web.multipart.MultipartFile;
  3. import javax.imageio.ImageIO;
  4. import java.awt.*;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.math.BigDecimal;
  9. import java.text.DateFormat;
  10. import java.text.DecimalFormat;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Calendar;
  13. import java.util.Date;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.regex.Pattern;
  17. /**
  18. * @ author :TXF
  19. * @ time :2021/8/25 11:25
  20. */
  21. public class DataChange {
  22. static String tempDir = "/temp/";
  23. /**
  24. * 时间转换类
  25. * 处理了三种类型 yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss 时间戳类型(带毫秒数时间戳13位)
  26. *
  27. * @param vueDate
  28. * @return
  29. */
  30. public static Date dataToDate(Object vueDate) {
  31. if (vueDate instanceof Date) {
  32. return (Date) vueDate;
  33. } else if (vueDate instanceof Long) {
  34. return new Date((Long) vueDate);
  35. } else {
  36. try {
  37. String str = String.valueOf(vueDate);
  38. if (judgeNumber(str) && str.length() == 13) {
  39. return new Date(Long.parseLong(str));
  40. } else if (str.contains("-")) {
  41. if (str.length() == "yyyy-MM-dd".length()) {
  42. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  43. return sdf.parse(str);
  44. }
  45. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  46. return sdf.parse(str);
  47. } else if (str.contains("/")) {
  48. if (str.length() == "yyyy/MM/dd".length()) {
  49. SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
  50. return sdf.parse(str);
  51. }
  52. SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  53. return sdf.parse(str);
  54. }
  55. } catch (Exception e) {
  56. System.out.println("时间解析错误!返回null");
  57. return null;
  58. }
  59. }
  60. return null;
  61. }
  62. /**
  63. * 判断是否纯数字(不带小数点)仅供上面方法使用
  64. *
  65. * @param str
  66. * @return
  67. */
  68. public static boolean judgeNumber(String str) {
  69. Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
  70. return pattern.matcher(str).matches();
  71. }
  72. /**
  73. * 数据转换成BigDecimal
  74. *
  75. * @param data
  76. * @return
  77. */
  78. public static BigDecimal dataToBigDecimal(Object data) {
  79. if (data != null) {
  80. if (data instanceof BigDecimal) {
  81. return (BigDecimal) data;
  82. } else {
  83. String str = String.valueOf(data);
  84. BigDecimal decimal = BigDecimal.ZERO;
  85. if (!"".equals(str)) {
  86. try {
  87. decimal = new BigDecimal(str);
  88. } catch (Exception e) {
  89. System.out.println(data + ":数据解析失败!返回0");
  90. return BigDecimal.ZERO;
  91. }
  92. }
  93. return decimal;
  94. }
  95. }
  96. return BigDecimal.ZERO;
  97. }
  98. /**
  99. * 将时间截取到天 为字符串类型 用于前端只显示到天
  100. *
  101. * @param date 传入时间
  102. * @return
  103. */
  104. public static Date dataToDayDate(Object date) {
  105. try {
  106. Date origin = dataToDate(date);
  107. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  108. if (origin == null) {
  109. return null;
  110. }
  111. return sdf.parse(sdf.format(origin));
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. return null;
  115. }
  116. }
  117. /**
  118. * 查询日期是否过期,过期则返回 true
  119. *
  120. * @param day 传入时间
  121. * @return
  122. */
  123. public static boolean isExpireDay(Object day) {
  124. try {
  125. if (dataToDayDate(day).getTime() < dataToDayDate(new Date()).getTime()) {
  126. return true;
  127. } else {
  128. return false;
  129. }
  130. } catch (Exception e) {
  131. e.printStackTrace();
  132. return true;
  133. }
  134. }
  135. /**
  136. * 遍历列表使只显示两位小数
  137. *
  138. * @param list
  139. * @param key
  140. */
  141. public static void dataTo2Number(List<Map<String, Object>> list, String... key) {
  142. DecimalFormat df = new DecimalFormat("0.00");
  143. //遍历List
  144. for (Map<String, Object> map : list) {
  145. for (String s : key) {
  146. //修改数据为带两位小数
  147. try {
  148. BigDecimal oldDate = dataToBigDecimal(map.get(s));
  149. String resultDeduction = df.format(oldDate.doubleValue());
  150. map.put(s, resultDeduction);
  151. } catch (Exception e) {
  152. System.out.println("原料扣减量数据有误");
  153. }
  154. }
  155. }
  156. }
  157. /**
  158. * 计算相差时间 日时分秒
  159. *
  160. * @param
  161. * @return
  162. */
  163. public static String calculatedTimeDifference(Date time1, Date time2) {
  164. long t1 = time1.getTime();
  165. long t2 = time2.getTime();
  166. if (t1 > t2) {
  167. long temp = t1;
  168. t1 = t2;
  169. t2 = temp;
  170. }
  171. long between = t2 - t1;
  172. long day = between / (24 * 60 * 60 * 1000);
  173. long hour = (between / (60 * 60 * 1000) - day * 24);
  174. long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
  175. long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
  176. return day + "天" + +hour + "时" + min + "分" + s + "秒";
  177. }
  178. /**
  179. * 生成带时间的八位数顺序号
  180. *
  181. * @param start 前缀
  182. * @param id 顺序号 主键Id
  183. * @return
  184. */
  185. public static String generateEightDigitsNumber(String start, Integer id) {
  186. id = id % 100000000; //保证不超过
  187. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  188. StringBuilder sb = new StringBuilder(start + sdf.format(new Date()));
  189. sb.append(
  190. id < 10
  191. ? "0000000" + id : id < 100
  192. ? "000000" + id : id < 1000
  193. ? "00000" + id : id < 10000
  194. ? "0000" + id : id < 100000
  195. ? "000" + id : id < 1000000
  196. ? "00" + id : id < 10000000
  197. ? "0" + id : id.toString()
  198. );
  199. return sb.toString();
  200. }
  201. /**
  202. * 根据时间段查询数据 支持只选择单个时间
  203. *
  204. * @param startTime
  205. * @param endTime
  206. * @param map
  207. * @param sdf
  208. * @return
  209. * @Author TXF
  210. * @Date 2022/1/10 23:21
  211. **/
  212. public static void queryDataByDate(String startTime, String endTime, Map<String, Object> map, SimpleDateFormat sdf) {
  213. if (startTime != null && !"null".equals(startTime) && endTime != null && !"null".equals(endTime)) {
  214. map.put("startDate", sdf.format(new Date(Long.parseLong(startTime))));
  215. map.put("endDate", sdf.format(new Date(Long.parseLong(endTime) + 86400000)));
  216. } else if (startTime != null && !"null".equals(startTime)) {
  217. map.put("oneDate", sdf.format(new Date(Long.parseLong(startTime))));
  218. } else if (endTime != null && !"null".equals(endTime)) {
  219. map.put("oneDate", sdf.format(new Date(Long.parseLong(endTime))));
  220. } else {
  221. map.put("oneDate", sdf.format(new Date()));
  222. }
  223. }
  224. /**
  225. * 只支持两个时间查询
  226. *
  227. * @param startTime
  228. * @param endTime
  229. * @param sdf
  230. * @return
  231. * @Author TXF
  232. * @Date 2022/1/15 9:08
  233. **/
  234. public static void queryDataByDateTime(String startTime, String endTime, Map<String, Object> map, SimpleDateFormat sdf) {
  235. SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
  236. if (startTime != null && !"null".equals(startTime) && endTime != null && !"null".equals(endTime)) {
  237. map.put("startDate", sdf.format(new Date(Long.parseLong(startTime))));
  238. map.put("endDate", sdf.format(new Date(Long.parseLong(endTime))));
  239. }
  240. //如果开始时间和结束时间有且只有一个为空 则只查那天的数据
  241. else if ((startTime != null && !"null".equals(startTime)) || (endTime != null && !"null".equals(endTime))) {
  242. if (startTime != null && !"null".equals(startTime)) {
  243. queryDataByTwoDateSon(map, startTime, sdfDate);
  244. }
  245. if (endTime != null && !"null".equals(endTime)) {
  246. queryDataByTwoDateSon(map, endTime, sdfDate);
  247. }
  248. } else {
  249. //如果两者时间都为空,则查询当天数据
  250. String nowDate = sdfDate.format(new Date());
  251. map.put("oneDate", nowDate + " 00:00:00");
  252. }
  253. }
  254. /**
  255. * 上面方法的儿子方法 如果只传入了一个时间 则查询那天的数据
  256. *
  257. * @param map
  258. * @param time
  259. * @param sdfDate
  260. * @return
  261. * @Author TXF
  262. * @Date 2022/1/17 16:17
  263. **/
  264. private static void queryDataByTwoDateSon(Map<String, Object> map, String time, SimpleDateFormat sdfDate) {
  265. Date date1 = new Date(Long.parseLong(time));
  266. Date date2 = new Date(Long.parseLong(time) + 86400000);
  267. String dayStartTime = sdfDate.format(date1);
  268. String dayEndTime = sdfDate.format(date2);
  269. map.put("startDate", dayStartTime + " 00:00:00");
  270. map.put("endDate", dayEndTime + " 00:00:00");
  271. }
  272. public static void getMonthStartEnd(Map<String, Object> map) {
  273. //
  274. Calendar cale = Calendar.getInstance();
  275. // 获取当月第一天和最后一天
  276. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  277. String firstDay, lastDay;
  278. // 获取前月的第一天
  279. cale.add(Calendar.MONTH, 0);
  280. cale.set(Calendar.DAY_OF_MONTH, 1);
  281. firstDay = format.format(cale.getTime());
  282. // 获取前月的最后一天
  283. cale = Calendar.getInstance();
  284. cale.add(Calendar.MONTH, 1);
  285. cale.set(Calendar.DAY_OF_MONTH, 0);
  286. lastDay = format.format(cale.getTime());
  287. System.out.println("本月第一天和最后一天分别是 : " + firstDay + " and " + lastDay);
  288. map.put("firstDay", firstDay);
  289. map.put("lastDay", lastDay);
  290. }
  291. public static File paintWater(MultipartFile file, String text, int xOffset, int yOffset) throws Exception {
  292. //将文件对象转化为图片对象
  293. Image srcImg = ImageIO.read(file.getInputStream());
  294. //获取图片的宽
  295. int srcImgWidth = srcImg.getWidth(null);
  296. //获取图片的高
  297. int srcImgHeight = srcImg.getHeight(null);
  298. // 加水印
  299. BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
  300. //创建画笔
  301. Graphics2D graphics = bufImg.createGraphics();
  302. //srcImg 为上面获取到的原始图片的图片对象
  303. graphics.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
  304. //根据图片的背景设置水印颜色
  305. graphics.setColor(new Color(255, 0, 0, 255));
  306. //设置字体 画笔字体样式为微软雅黑,加粗,文字大小为60pt
  307. graphics.setFont(new Font("微软雅黑", Font.BOLD, 40));
  308. //设置水印的坐标
  309. int x = 0;
  310. if (xOffset >= 0) {
  311. x = xOffset;
  312. } else {
  313. x = srcImgWidth + (xOffset % srcImgWidth);
  314. }
  315. int y = 0;
  316. if (yOffset >= 0) {
  317. y = yOffset;
  318. } else {
  319. y = srcImgHeight + (yOffset % srcImgWidth);
  320. }
  321. //自定义水印 第一个参数是水印内容,第二个参数是x轴坐标,第三个参数是y轴坐标
  322. graphics.drawString(text, x, y);
  323. //时间水印
  324. DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  325. graphics.drawString(df.format(new Date()), x, y + 50);
  326. graphics.dispose();
  327. //待存储的地址
  328. File fileDir = new File(tempDir);
  329. if (!fileDir.exists() || !fileDir.isDirectory()) {
  330. fileDir.mkdir();
  331. }
  332. String tarImgPath = tempDir + file.getOriginalFilename();
  333. // 输出图片
  334. FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
  335. ImageIO.write(bufImg, "png", outImgStream);
  336. outImgStream.flush();
  337. outImgStream.close();
  338. //返回目标图片
  339. File targetFile = new File(tarImgPath);
  340. // targetFile.delete();//临时文件返回后,保存在其他地方然后及时删除
  341. return targetFile;
  342. }
  343. }