- 周期字符格式化
/** * 获取周期时间字符 * * @title * @description 周期类型 -1:一次性,0:每日,1:每周,2:每月,3:每季度,4:每半年,5:每年 * @author fjh * @date 2023年5月31日上午11:29:54 * @param reportCycle * @return */ public static String getTimeType(String reportCycle) { LocalDate now = LocalDate.now(); DateTimeFormatter dfDate = DateTimeFormatter.ofPattern("yyyy-MM-dd"); if ("0".equals(reportCycle)||"-1".equals(reportCycle)) { return dfDate.ofPattern("yyyy年MM月dd日").format(now); } else if ("1".equals(reportCycle)) { return dfDate.ofPattern("yyyy年ww周").format(now); } else if ("2".equals(reportCycle)) { return dfDate.ofPattern("yyyy年MM月").format(now); } else if ("3".equals(reportCycle)) { return dfDate.ofPattern("yyyy年q季度").format(now); } else if ("4".equals(reportCycle)) { int m = now.getMonth().getValue(); int y = now.getYear(); if (m <= 6) { return y + "年上半年"; } return y + "年下半年"; } else if ("5".equals(reportCycle)) { return dfDate.ofPattern("yyyy年").format(now); } return dfDate.format(now); }
- 获取周期截止时间
/** * 截至时间 * * @title * @description 周期类型 -1:一次性,0:每日,1:每周,2:每月,3:每季度,4:每半年,5:每年 * @author fjh * @date 2023年5月31日下午3:04:39 * @param reportCycle * @return */ public static Date getDeadline(String reportCycle,Integer delayNum) { Date lastDate = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(lastDate); if ("0".equals(reportCycle)) {//天 cal.add(Calendar.DATE, 0); } else if ("1".equals(reportCycle)) {//周 int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 1; if (day_of_week == 0) day_of_week = 7; cal.add(Calendar.DATE, -day_of_week + 7); } else if ("2".equals(reportCycle)) {//月 cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); } else if ("3".equals(reportCycle)) {//季度 int lastMonth = getQuarter(cal.get(cal.MONTH) + 1) * 3; cal.set(Calendar.MONTH, lastMonth - 1); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); } else if ("4".equals(reportCycle)) {//半年 int m = cal.get(cal.MONTH) + 1; if (m <= 6) { cal.set(Calendar.MONTH, 6 - 1); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); } else { cal.set(Calendar.MONTH, 12 - 1); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); } } else if ("5".equals(reportCycle)) {//年 cal.set(Calendar.MONTH, 12 - 1); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); } else if ("-1".equals(reportCycle)) {//一次性 if(delayNum!=null)cal.add(Calendar.DATE, delayNum); } cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); lastDate = cal.getTime(); return lastDate; } /** * 获取季度 * * @title * @description * @author fjh * @date 2023年5月31日下午3:01:09 * @return */ public static int getQuarter(int month) { int quarter = 0; if (month >= 1 && month <= 3) { quarter = 1; } else if (month >= 4 && month <= 6) { quarter = 2; } else if (month >= 7 && month <= 9) { quarter = 3; } else { quarter = 4; } return quarter; }
标签:周期,cal,equals,MONTH,获取,reportCycle,Calendar,now,截止 From: https://www.cnblogs.com/my-secret-base/p/17460604.html