DataChange.java 13 KB

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