diff --git a/backend/src/main/java/io/dataease/service/chart/ChartViewService.java b/backend/src/main/java/io/dataease/service/chart/ChartViewService.java index 132bec8b4e..c0d6229740 100644 --- a/backend/src/main/java/io/dataease/service/chart/ChartViewService.java +++ b/backend/src/main/java/io/dataease/service/chart/ChartViewService.java @@ -600,50 +600,54 @@ public class ChartViewService { } private String calcLastTime(String cTime, String type, String dateStyle) throws Exception { - String lastTime = null; - Calendar calendar = Calendar.getInstance(); - if (StringUtils.equalsIgnoreCase(type, ChartConstants.YEAR_MOM)) { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy"); - Date date = simpleDateFormat.parse(cTime); - calendar.setTime(date); - calendar.add(Calendar.YEAR, -1); - lastTime = simpleDateFormat.format(calendar.getTime()); - } else if (StringUtils.equalsIgnoreCase(type, ChartConstants.MONTH_MOM)) { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM"); - Date date = simpleDateFormat.parse(cTime); - calendar.setTime(date); - calendar.add(Calendar.MONTH, -1); - lastTime = simpleDateFormat.format(calendar.getTime()); - } else if (StringUtils.equalsIgnoreCase(type, ChartConstants.YEAR_YOY)) { - SimpleDateFormat simpleDateFormat = null; - if (StringUtils.equalsIgnoreCase(dateStyle, "y_M")) { - simpleDateFormat = new SimpleDateFormat("yyyy-MM"); - } else if (StringUtils.equalsIgnoreCase(dateStyle, "y_M_d")) { - simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + try { + String lastTime = null; + Calendar calendar = Calendar.getInstance(); + if (StringUtils.equalsIgnoreCase(type, ChartConstants.YEAR_MOM)) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy"); + Date date = simpleDateFormat.parse(cTime); + calendar.setTime(date); + calendar.add(Calendar.YEAR, -1); + lastTime = simpleDateFormat.format(calendar.getTime()); + } else if (StringUtils.equalsIgnoreCase(type, ChartConstants.MONTH_MOM)) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM"); + Date date = simpleDateFormat.parse(cTime); + calendar.setTime(date); + calendar.add(Calendar.MONTH, -1); + lastTime = simpleDateFormat.format(calendar.getTime()); + } else if (StringUtils.equalsIgnoreCase(type, ChartConstants.YEAR_YOY)) { + SimpleDateFormat simpleDateFormat = null; + if (StringUtils.equalsIgnoreCase(dateStyle, "y_M")) { + simpleDateFormat = new SimpleDateFormat("yyyy-MM"); + } else if (StringUtils.equalsIgnoreCase(dateStyle, "y_M_d")) { + simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + } + Date date = simpleDateFormat.parse(cTime); + calendar.setTime(date); + calendar.add(Calendar.YEAR, -1); + lastTime = simpleDateFormat.format(calendar.getTime()); + } else if (StringUtils.equalsIgnoreCase(type, ChartConstants.DAY_MOM)) { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + Date date = simpleDateFormat.parse(cTime); + calendar.setTime(date); + calendar.add(Calendar.DAY_OF_MONTH, -1); + lastTime = simpleDateFormat.format(calendar.getTime()); + } else if (StringUtils.equalsIgnoreCase(type, ChartConstants.MONTH_YOY)) { + SimpleDateFormat simpleDateFormat = null; + if (StringUtils.equalsIgnoreCase(dateStyle, "y_M")) { + simpleDateFormat = new SimpleDateFormat("yyyy-MM"); + } else if (StringUtils.equalsIgnoreCase(dateStyle, "y_M_d")) { + simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + } + Date date = simpleDateFormat.parse(cTime); + calendar.setTime(date); + calendar.add(Calendar.MONTH, -1); + lastTime = simpleDateFormat.format(calendar.getTime()); } - Date date = simpleDateFormat.parse(cTime); - calendar.setTime(date); - calendar.add(Calendar.YEAR, -1); - lastTime = simpleDateFormat.format(calendar.getTime()); - } else if (StringUtils.equalsIgnoreCase(type, ChartConstants.DAY_MOM)) { - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); - Date date = simpleDateFormat.parse(cTime); - calendar.setTime(date); - calendar.add(Calendar.DAY_OF_MONTH, -1); - lastTime = simpleDateFormat.format(calendar.getTime()); - } else if (StringUtils.equalsIgnoreCase(type, ChartConstants.MONTH_YOY)) { - SimpleDateFormat simpleDateFormat = null; - if (StringUtils.equalsIgnoreCase(dateStyle, "y_M")) { - simpleDateFormat = new SimpleDateFormat("yyyy-MM"); - } else if (StringUtils.equalsIgnoreCase(dateStyle, "y_M_d")) { - simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); - } - Date date = simpleDateFormat.parse(cTime); - calendar.setTime(date); - calendar.add(Calendar.MONTH, -1); - lastTime = simpleDateFormat.format(calendar.getTime()); + return lastTime; + } catch (Exception e) { + return cTime; } - return lastTime; } private boolean checkDrillExist(List xAxis, List extStack, ChartViewFieldDTO dto, ChartViewWithBLOBs view) {