123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package com.steerinfo.dil.util;
- import java.math.BigDecimal;
- import java.text.DecimalFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- /**
- * @ author :TXF
- * @ time :2021/8/25 11:25
- */
- public class DataChange {
- /**
- * 解析前端传来的日期字符串
- * @param vueDate
- * @return
- */
- public static Date dataToDate(Object vueDate){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Date parseDate = null;
- if (vueDate != null){
- try {
- String date = (String) vueDate;
- parseDate = sdf.parse(date);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }
- return parseDate;
- }
- /**
- * 数据转换成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 = null;
- if(!"".equals(str)){
- try {
- decimal = new BigDecimal(str);
- } catch (Exception e) {
- System.out.println(data + ":数据解析失败!返回0");
- return new BigDecimal(0);
- }
- }
- return decimal;
- }
- }
- return new BigDecimal(0);
- }
- /**
- * 将时间截取到天 为字符串类型 用于前端只显示到天
- * @param date 传入时间
- * @return
- */
- public static String dateToDayDate(Object date){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- Date changeDate = null;
- if(date == null)
- return null;
- try{
- changeDate = (Date) date;
- }catch (Exception e){
- e.printStackTrace();
- }
- return sdf.format(changeDate);
- }
- /**
- * 遍历从数据库中传进来的数据
- * @param list 从数据库查询出来的list数据列表
- * @param key map中多个需要转换的date参数
- */
- public static void changeDateToDayDate(List<Map<String, Object>> list, String ...key){
- //遍历List
- for (Map<String, Object> map : list) {
- for (String s : key) {
- //从map中取 date的值 并转换成字符串类型的日期
- String stringDate = dateToDayDate(map.get(s));
- if(stringDate.length() == 0){
- break;
- }else {
- //修改map中的值
- map.put(s, stringDate);
- }
- }
- }
- }
- /**
- * 遍历列表使只显示两位小数
- * @param list
- * @param key
- */
- public static void dataTo2Number(List<Map<String, Object>> list, String ...key){
- //遍历List
- for (Map<String, Object> map : list) {
- for (String s : key) {
- //修改数据为带两位小数
- try {
- BigDecimal oldDate = (BigDecimal) map.get(s);
- DecimalFormat df = new DecimalFormat("0.00");
- 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){
- 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();
- }
- /**
- * 根据时间段查询数据
- * @Author TXF
- * @Date 2022/1/10 23:21
- * @param startTime
- * @param endTime
- * @param map
- * @param sdf
- * @return
- **/
- 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()));
- }
- }
- }
|