DataChange.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. System.out.println(data + ":数据解析失败!返回0");
  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. if(stringDate.length() == 0){
  86. break;
  87. }else {
  88. //修改map中的值
  89. map.put(s, stringDate);
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * 遍历列表使只显示两位小数
  96. * @param list
  97. * @param key
  98. */
  99. public static void dataTo2Number(List<Map<String, Object>> list, String ...key){
  100. //遍历List
  101. for (Map<String, Object> map : list) {
  102. for (String s : key) {
  103. //修改数据为带两位小数
  104. try {
  105. BigDecimal oldDate = (BigDecimal) map.get(s);
  106. DecimalFormat df = new DecimalFormat("0.00");
  107. String resultDeduction = df.format(oldDate.doubleValue());
  108. map.put(s, resultDeduction);
  109. } catch (Exception e) {
  110. System.out.println("原料扣减量数据有误");
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * 计算相差时间 日时分秒
  117. * @param
  118. * @return
  119. */
  120. public static String calculatedTimeDifference(Date time1, Date time2){
  121. long t1 = time1.getTime();
  122. long t2 = time2.getTime();
  123. if(t1 > t2){
  124. long temp = t1;
  125. t1 = t2;
  126. t2 = temp;
  127. }
  128. long between = t2 - t1;
  129. long day = between / (24 * 60 * 60 * 1000);
  130. long hour = (between / (60 * 60 * 1000) - day * 24);
  131. long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
  132. long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
  133. return day + "天"+ + hour+ "时" + min + "分" + s + "秒";
  134. }
  135. /**
  136. * 生成带时间的八位数顺序号
  137. * @param start 前缀
  138. * @param id 顺序号 主键Id
  139. * @return
  140. */
  141. public static String generateEightDigitsNumber(String start, Integer id){
  142. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  143. StringBuilder sb = new StringBuilder(start + sdf.format(new Date()));
  144. sb.append(
  145. id < 10
  146. ? "0000000" + id : id < 100
  147. ? "000000" + id : id < 1000
  148. ? "00000" + id : id < 10000
  149. ? "0000" + id : id < 100000
  150. ? "000" + id : id < 1000000
  151. ? "00" + id : id < 10000000
  152. ? "0" + id : id.toString()
  153. );
  154. return sb.toString();
  155. }
  156. /**
  157. * 根据时间段查询数据
  158. * @Author TXF
  159. * @Date 2022/1/10 23:21
  160. * @param startTime
  161. * @param endTime
  162. * @param map
  163. * @param sdf
  164. * @return
  165. **/
  166. public static void queryDataByDate(String startTime, String endTime, Map<String, Object> map, SimpleDateFormat sdf){
  167. if (startTime != null && !"null".equals(startTime) && endTime != null && !"null".equals(endTime)) {
  168. map.put("startDate", sdf.format(new Date(Long.parseLong(startTime))));
  169. map.put("endDate", sdf.format(new Date(Long.parseLong(endTime) + 86400000)));
  170. } else if (startTime != null && !"null".equals(startTime)) {
  171. map.put("oneDate", sdf.format(new Date(Long.parseLong(startTime))));
  172. } else if (endTime != null && !"null".equals(endTime)) {
  173. map.put("oneDate", sdf.format(new Date(Long.parseLong(endTime))));
  174. } else {
  175. map.put("oneDate", sdf.format(new Date()));
  176. }
  177. }
  178. }