|
@@ -7,6 +7,7 @@ import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
* @ author :TXF
|
|
@@ -17,24 +18,45 @@ public class DataChange {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 解析前端传来的日期字符串
|
|
|
+ * 时间转换类
|
|
|
+ * 处理了三种类型 yyyy-MM-dd HH:mm:ss yyyy/MM/dd HH:mm:ss 时间戳类型(带毫秒数时间戳13位)
|
|
|
* @param vueDate
|
|
|
* @return
|
|
|
*/
|
|
|
public static Date dataToDate(Object vueDate){
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
- Date parseDate = null;
|
|
|
- if (vueDate != null){
|
|
|
+ if(vueDate instanceof Date){
|
|
|
+ return (Date) vueDate;
|
|
|
+ } else {
|
|
|
try {
|
|
|
- String date = (String) vueDate;
|
|
|
- parseDate = sdf.parse(date);
|
|
|
- } catch (ParseException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ String str = String.valueOf(vueDate);
|
|
|
+ if(judgeNumber(str) && str.length() == 13){
|
|
|
+ return new Date(Long.parseLong(str));
|
|
|
+ }else if(str.contains("-")){
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ return sdf.parse(str);
|
|
|
+ }else if(str.contains("/")){
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
|
|
+ return sdf.parse(str);
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ System.out.println("时间解析错误!返回null");
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|
|
|
- return parseDate;
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否纯数字(不带小数点)仅供上面方法使用
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean judgeNumber(String str){
|
|
|
+ Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
|
|
|
+ return pattern.matcher(str).matches();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 数据转换成BigDecimal
|
|
|
* @param data
|
|
@@ -42,20 +64,20 @@ public class DataChange {
|
|
|
*/
|
|
|
public static BigDecimal dataToBigDecimal(Object data){
|
|
|
if (data != null){
|
|
|
- if(data instanceof String){
|
|
|
- String data1 = (String) data;
|
|
|
- return new BigDecimal(data1);
|
|
|
- }
|
|
|
- if(data instanceof Integer){
|
|
|
- Integer data2 = (Integer) data;
|
|
|
- return new BigDecimal(data2);
|
|
|
- }
|
|
|
- if(data instanceof Double){
|
|
|
- Double data3 = (Double) data;
|
|
|
- return new BigDecimal(data3);
|
|
|
- }
|
|
|
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);
|
|
@@ -69,8 +91,10 @@ public class DataChange {
|
|
|
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;
|
|
|
+ changeDate = (Date) date;
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -104,14 +128,18 @@ public class DataChange {
|
|
|
* @param key
|
|
|
*/
|
|
|
public static void dataTo2Number(List<Map<String, Object>> list, String ...key){
|
|
|
+ DecimalFormat df = new DecimalFormat("0.00");
|
|
|
//遍历List
|
|
|
for (Map<String, Object> map : list) {
|
|
|
for (String s : key) {
|
|
|
//修改数据为带两位小数
|
|
|
- BigDecimal oldDate = (BigDecimal) map.get(s);
|
|
|
- DecimalFormat df = new DecimalFormat("0.00");
|
|
|
- String resultDeduction = df.format(oldDate.doubleValue());
|
|
|
- map.put(s, resultDeduction);
|
|
|
+ try {
|
|
|
+ BigDecimal oldDate = dataToBigDecimal(map.get(s));
|
|
|
+ String resultDeduction = df.format(oldDate.doubleValue());
|
|
|
+ map.put(s, resultDeduction);
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println("原料扣减量数据有误");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -154,8 +182,79 @@ public class DataChange {
|
|
|
? "0000" + id : id < 100000
|
|
|
? "000" + id : id < 1000000
|
|
|
? "00" + id : id < 10000000
|
|
|
- ? "0" + id : id
|
|
|
+ ? "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()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 只支持两个时间查询
|
|
|
+ * @Author TXF
|
|
|
+ * @Date 2022/1/15 9:08
|
|
|
+ * @param startTime
|
|
|
+ * @param endTime
|
|
|
+ * @param sdf
|
|
|
+ * @return
|
|
|
+ **/
|
|
|
+ public static void queryDataByDateTime(String startTime, String endTime, Map<String, Object> map,SimpleDateFormat sdf){
|
|
|
+ SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ 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))));
|
|
|
+ }
|
|
|
+ //如果开始时间和结束时间有且只有一个为空 则只查那天的数据
|
|
|
+ else if((startTime != null && !"null".equals(startTime)) || (endTime != null && !"null".equals(endTime))){
|
|
|
+ if(startTime != null && !"null".equals(startTime)){
|
|
|
+ queryDataByTwoDateSon(map, startTime, sdfDate);
|
|
|
+ }
|
|
|
+ if(endTime != null && !"null".equals(endTime)){
|
|
|
+ queryDataByTwoDateSon(map, endTime, sdfDate);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ //如果两者时间都为空,则查询当天数据
|
|
|
+ String nowDate = sdfDate.format(new Date());
|
|
|
+ map.put("oneDate", nowDate + " 00:00:00");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上面方法的儿子方法 如果只传入了一个时间 则查询那天的数据
|
|
|
+ * @Author TXF
|
|
|
+ * @Date 2022/1/17 16:17
|
|
|
+ * @param map
|
|
|
+ * @param time
|
|
|
+ * @param sdfDate
|
|
|
+ * @return
|
|
|
+ **/
|
|
|
+ private static void queryDataByTwoDateSon(Map<String, Object> map, String time, SimpleDateFormat sdfDate){
|
|
|
+ Date date1 = new Date(Long.parseLong(time));
|
|
|
+ Date date2 = new Date(Long.parseLong(time) + 86400000);
|
|
|
+ String dayStartTime = sdfDate.format(date1);
|
|
|
+ String dayEndTime = sdfDate.format(date2);
|
|
|
+ map.put("startDate", dayStartTime + " 00:00:00");
|
|
|
+ map.put("endDate", dayEndTime + " 00:00:00");
|
|
|
+ }
|
|
|
}
|