DataChange.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.steerinfo.dil.util;
  2. import java.math.BigDecimal;
  3. import java.text.DecimalFormat;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. import java.util.List;
  8. import java.util.Map;
  9. /**
  10. * @ author :TXF
  11. * @ time :2021/8/25 11:25
  12. */
  13. public class DataChange {
  14. /**
  15. * 解析前端传来的日期字符串
  16. * @param vueDate
  17. * @return
  18. */
  19. public static Date dataToDate(Object vueDate){
  20. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  21. Date parseDate = null;
  22. if (vueDate != null){
  23. try {
  24. String date = (String) vueDate;
  25. parseDate = sdf.parse(date);
  26. } catch (ParseException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. return parseDate;
  31. }
  32. /**
  33. * 数据转换成BigDecimal
  34. * @param data
  35. * @return
  36. */
  37. public static BigDecimal dataToBigDecimal(Object data){
  38. if (data != null){
  39. if(data instanceof BigDecimal){
  40. return (BigDecimal) data;
  41. }else{
  42. String str = String.valueOf(data);
  43. BigDecimal decimal = null;
  44. if(!"".equals(str)){
  45. try {
  46. decimal = new BigDecimal(str);
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. return new BigDecimal(0);
  50. }
  51. }
  52. return decimal;
  53. }
  54. }
  55. return new BigDecimal(0);
  56. }
  57. /**
  58. * 将时间截取到天 为字符串类型 用于前端只显示到天
  59. * @param date 传入时间
  60. * @return
  61. */
  62. public static String dateToDayDate(Object date){
  63. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  64. Date changeDate = null;
  65. if(date == null)
  66. return null;
  67. try{
  68. changeDate = (Date) date;
  69. }catch (Exception e){
  70. e.printStackTrace();
  71. }
  72. return sdf.format(changeDate);
  73. }
  74. /**
  75. * 遍历从数据库中传进来的数据
  76. * @param list 从数据库查询出来的list数据列表
  77. * @param key map中多个需要转换的date参数
  78. */
  79. public static void changeDateToDayDate(List<Map<String, Object>> list, String ...key){
  80. //遍历List
  81. for (Map<String, Object> map : list) {
  82. for (String s : key) {
  83. //从map中取 date的值 并转换成字符串类型的日期
  84. String stringDate = dateToDayDate(map.get(s));
  85. map.put(s, stringDate);
  86. }
  87. }
  88. }
  89. /**
  90. * 遍历列表使只显示两位小数
  91. * @param list
  92. * @param key
  93. */
  94. public static void dataTo2Number(List<Map<String, Object>> list, String ...key){
  95. //遍历List
  96. for (Map<String, Object> map : list) {
  97. for (String s : key) {
  98. //修改数据为带两位小数
  99. BigDecimal oldDate = (BigDecimal) map.get(s);
  100. DecimalFormat df = new DecimalFormat("0.00");
  101. String resultDeduction = df.format(oldDate.doubleValue());
  102. map.put(s, resultDeduction);
  103. }
  104. }
  105. }
  106. /**
  107. * 计算相差时间 日时分秒
  108. * @param
  109. * @return
  110. */
  111. public static String calculatedTimeDifference(Date time1, Date time2){
  112. long t1 = time1.getTime();
  113. long t2 = time2.getTime();
  114. if(t1 > t2){
  115. long temp = t1;
  116. t1 = t2;
  117. t2 = temp;
  118. }
  119. long between = t2 - t1;
  120. long day = between / (24 * 60 * 60 * 1000);
  121. long hour = (between / (60 * 60 * 1000) - day * 24);
  122. long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
  123. long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
  124. return day + "天"+ + hour+ "时" + min + "分" + s + "秒";
  125. }
  126. /**
  127. * 生成带时间的八位数顺序号
  128. * @param start 前缀
  129. * @param id 顺序号 主键Id
  130. * @return
  131. */
  132. public static String generateEightDigitsNumber(String start, Integer id){
  133. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  134. StringBuilder sb = new StringBuilder(start + sdf.format(new Date()));
  135. sb.append(
  136. id < 10
  137. ? "0000000" + id : id < 100
  138. ? "000000" + id : id < 1000
  139. ? "00000" + id : id < 10000
  140. ? "0000" + id : id < 100000
  141. ? "000" + id : id < 1000000
  142. ? "00" + id : id < 10000000
  143. ? "0" + id : id.toString()
  144. );
  145. return sb.toString();
  146. }
  147. }