From cf985b57d178fad1e18b46b9b020548a90d3d915 Mon Sep 17 00:00:00 2001 From: lidongdong <1459917685@qq.com> Date: 星期一, 28 十一月 2022 11:17:56 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/huacheng_test' into huacheng_test --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DateUtils.java | 98 +++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 86 insertions(+), 12 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DateUtils.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DateUtils.java index c07afb2..052832e 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DateUtils.java +++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DateUtils.java @@ -4,11 +4,10 @@ import java.text.SimpleDateFormat; import java.time.*; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.List; +import java.util.*; +import cn.hutool.core.date.DateUnit; +import cn.hutool.core.date.DateUtil; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -416,11 +415,11 @@ long day = diff / nd; - long hour = diff % nd / nh; + long hour = diff / nh; - long min = diff % nd % nh / nm; + long min = diff / nm; - return (int)day; + return (int)hour; } /** @@ -927,11 +926,10 @@ } public static void main(String[] args) throws Exception { - Date date= getLastMonthFirst(); - Date after = getLastMonthEnd(); - System.out.println(date); - System.out.println(after); - +// List<String> beforeDays = getBeforeDays(15); +// System.out.println(beforeDays); + List<String> yearMonths = getYearMonths(); + System.out.println(yearMonths); } @@ -1129,4 +1127,80 @@ return firstDayOfMonth; } + + /** + * 获取20天前那天时间 + * + * @return + */ + public static String getTwentyDay() { + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + cal.add(Calendar.DATE, -20); + + return DateUtil.format(cal.getTime(),"yyyy-MM-dd"); + } + + /** + * 获取之前的日期 + * + * @return + */ + public static String getBeforeDay(Integer amount) { + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + cal.add(Calendar.DATE, -amount); + return DateUtil.format(cal.getTime(),"yyyy-MM-dd"); + } + + /** + * 获取最近本月20天数据 + * + * @return + */ + public static String getMonthTwentyDay() { + Date date = new Date(); + Long offset = DateUtil.between(new Date(), DateUtil.beginOfMonth(date), DateUnit.DAY); + if (offset >= 20) { + return getTwentyDay(); + } else { + //距离月初不足20天 + Calendar calendar = new GregorianCalendar(); + calendar.setTime(date); + //获得本月第一天 + calendar.add(Calendar.MONTH, 0); + calendar.set(Calendar.DAY_OF_MONTH, 1); + return DateUtil.format(calendar.getTime(),"yyyy-MM-dd"); + } + + } + + public static List<String> getBeforeDays(int before) { + List<String> dates = new ArrayList<>(); + for (int i = before; i >= 0; i--) { + dates.add(getBeforeDay(i)); + } + return dates; + } + + /** + * 获取当年所有月份 + * + * @return + */ + public static List<String> getYearMonths() { + Calendar instance = Calendar.getInstance(); + int year = instance.get(Calendar.YEAR); + List<String> months = new ArrayList<>(); + for (int i = 1; i <= 12; i++) { + String result = String.valueOf(year); + if (i < 10) { + result = result + "-0" +i; + } else { + result = result + "-"+i; + } + months.add(result); + } + return months; + } } -- Gitblit v1.7.1