From f4cdd9b7ff1d4f6c57bb5b3a30b223f441969a7f Mon Sep 17 00:00:00 2001 From: yinq <1345442242@qq.com> Date: Thu, 27 Oct 2022 10:59:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=A5=E6=9C=9F=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mesnac/anomaly/utils/DateReportUtils.java | 102 +++++++++++++++--- 1 file changed, 90 insertions(+), 12 deletions(-) diff --git a/anomaly/src/main/java/com/foreverwin/mesnac/anomaly/utils/DateReportUtils.java b/anomaly/src/main/java/com/foreverwin/mesnac/anomaly/utils/DateReportUtils.java index e9726c4b..f49c4ddc 100644 --- a/anomaly/src/main/java/com/foreverwin/mesnac/anomaly/utils/DateReportUtils.java +++ b/anomaly/src/main/java/com/foreverwin/mesnac/anomaly/utils/DateReportUtils.java @@ -4,6 +4,11 @@ import org.apache.commons.lang.time.DateUtils; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.temporal.TemporalAdjusters; import java.util.*; /** @@ -19,7 +24,7 @@ public class DateReportUtils { * 指定时间计算有几周,并返回每周起止日期 * * @param start 开始时间 - * @param end 结束时间 + * @param end 结束时间 * @return week:第几周 start:每周开始时间 end:每周结束时间 */ public static List> getWeekOfDate(String start, String end) { @@ -51,16 +56,16 @@ public class DateReportUtils { if (i == 1) {//第一周以 start 开始 map.put("start", dateFormat(startDate, sdf2)); - }else { - map.put("start",dateFormat(cal.getTime(),sdf2)); + } else { + map.put("start", dateFormat(cal.getTime(), sdf2)); } if (i == weekNum) {//最后一周以 end 结束 map.put("end", dateFormat(endDate, sdf2)); - }else { + } else { //设置这周的周日日期,1代表周日,取值范围1~7,设置1~7之外会从周日开始往前后推算,负前正后,DAY_OF_WEEK的日期变更范围只会是在当前日期的周 - cal.set(Calendar.DAY_OF_WEEK,1); - map.put("end",dateFormat(cal.getTime(),sdf2)); + cal.set(Calendar.DAY_OF_WEEK, 1); + map.put("end", dateFormat(cal.getTime(), sdf2)); } list.add(map); @@ -101,16 +106,16 @@ public class DateReportUtils { if (i == 1) {//第一周以 start 开始 map.put("start", dateFormat(startDate, sdf3)); - }else { - map.put("start",dateFormat(cal.getTime(),sdf3)); + } else { + map.put("start", dateFormat(cal.getTime(), sdf3)); } if (i == weekNum) {//最后一周以 end 结束 map.put("end", dateFormat(endDate, sdf3)); - }else { + } else { //设置这周的周日日期,1代表周日,取值范围1~7,设置1~7之外会从周日开始往前后推算,负前正后,DAY_OF_WEEK的日期变更范围只会是在当前日期的周 - cal.set(Calendar.DAY_OF_WEEK,1); - map.put("end",dateFormat(cal.getTime(),sdf3)); + cal.set(Calendar.DAY_OF_WEEK, 1); + map.put("end", dateFormat(cal.getTime(), sdf3)); } list.add(map); @@ -147,7 +152,7 @@ public class DateReportUtils { * @throws ParseException */ public static Date stringToDate(String str, String date) { - if (str.indexOf("null") != -1){ + if (str.indexOf("null") != -1) { return null; } try { @@ -190,5 +195,78 @@ public class DateReportUtils { } } + /** + * 获取本月剩余天数(除去星期天) + * @return + */ + public static Integer getMonthRemainingDays() { +// Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:OO")); //这是获得东八区时间,也就是北京时间 +// int day = c.get(Calendar.DAY_OF_MONTH); //获取当前天数 +// int last = c.getActualMaximum(c.DAY_OF_MONTH); //获取本月最大天数 + Integer result = 0; + LocalDate localDate = LocalDate.now(); + LocalDate lastDay = localDate.with(TemporalAdjusters.lastDayOfMonth()); + List dateList = getBetweenDates(localDate2Date(localDate), localDate2Date(lastDay)); + for (Date date : dateList) { + if (!getWeekOfDate(date).equals("星期日")){ + result += 1; + } + } + return result; + + } + + /** + * LocalDate转换成Date + * @param localDate + * @return + */ + public static Date localDate2Date(LocalDate localDate) { + if(null == localDate) { + return null; + } + ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault()); + return Date.from(zonedDateTime.toInstant()); + } + + /** + * 获取当前日期是星期几
+ * + * @param date + * @return 当前日期是星期几 + */ + public static String getWeekOfDate(Date date) { + String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; + Calendar cal = Calendar.getInstance(); + cal.setTime(date); + int w = cal.get(Calendar.DAY_OF_WEEK) - 1; + if (w < 0) + w = 0; + return weekDays[w]; + } + + /** + * 获取日期List + * @param start + * @param end + * @return + */ + public static List getBetweenDates(Date start, Date end) { + List result = new ArrayList(); + Calendar tempStart = Calendar.getInstance(); + tempStart.setTime(start); + result.add(tempStart.getTime()); + tempStart.add(Calendar.DAY_OF_YEAR, 1); + Calendar tempEnd = Calendar.getInstance(); + tempEnd.setTime(end); + while (tempStart.before(tempEnd)) { + result.add(tempStart.getTime()); + tempStart.add(Calendar.DAY_OF_YEAR, 1); + } + result.add(tempEnd.getTime()); + return result; + } + + }