From 179c4d64313c9b7572778da4aaaf6c6584fe457d Mon Sep 17 00:00:00 2001 From: mitao <2763622819@qq.com> Date: 星期二, 20 五月 2025 23:48:08 +0800 Subject: [PATCH] 修改文件上传类型限制 --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DateUtils.java | 367 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 326 insertions(+), 41 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 d220a6d..71bfc6d 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,9 +4,10 @@ import java.text.SimpleDateFormat; import java.time.*; import java.time.format.DateTimeFormatter; -import java.util.Calendar; -import java.util.Date; +import java.util.*; +import cn.hutool.core.date.DateUnit; +import cn.hutool.core.date.DateUtil; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -15,6 +16,7 @@ public static DateTimeFormatter format_ymdhms = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public static DateTimeFormatter format_ymd = DateTimeFormatter.ofPattern("yyyy-MM-dd"); public static DateTimeFormatter format_ymdhms_string = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); + public static DateTimeFormatter format_ymdhms_yyyyMMddmmHHssSSS = DateTimeFormatter.ofPattern("yyyyMMddmmHHssSSS"); public static DateTimeFormatter format_ymdhms_no_signal = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); public static SimpleDateFormat yyyyMMdd_format = new SimpleDateFormat("yyyy-MM-dd"); public static SimpleDateFormat yyyy_MM_dd_format = new SimpleDateFormat("yyyy/MM/ddHH:mm:ss"); @@ -23,6 +25,8 @@ public static DateTimeFormatter format_ymd_String = DateTimeFormatter.ofPattern("yyMMdd"); public static SimpleDateFormat yyyyMM_format = new SimpleDateFormat("yyyy-MM"); public static SimpleDateFormat format_yyymmdd = new SimpleDateFormat("yyyyMMdd"); + public static SimpleDateFormat format_yyyy = new SimpleDateFormat("yyyy"); + public static SimpleDateFormat format_yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss"); private static DateTimeFormatter format_ymdhmssss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); private static DateTimeFormatter format_ymds = DateTimeFormatter.ofPattern("yyyyMMdd"); private static DateTimeFormatter format_yms = DateTimeFormatter.ofPattern("yyyyMM"); @@ -49,7 +53,7 @@ /** * 字符串转date - * + * * @param date * 时间戳 * @param format @@ -70,7 +74,7 @@ /** * 字符串转date - * + * * @param date * 时间戳 * @return 时间 @@ -96,7 +100,7 @@ /** * 字符串转date - * + * * @param date * 时间戳 * @return 时间 @@ -115,7 +119,7 @@ /** * 获取当前时间 - * + * * @return * @throws Exception */ @@ -126,7 +130,7 @@ /** * 返回当前事件的时间戳 - * + * * @return */ public static Long getCurrentDateTimeStamp() { @@ -135,7 +139,7 @@ /** * 获取当前时间字符串,格式为yyyy-MM-dd HH:mm:ss - * + * * @return */ public static String getCurrentDateStr() { @@ -155,7 +159,7 @@ /** * 获取当前时间字符串,格式为yyyyMMdd - * + * * @return */ public static String getCurrentDateString() { @@ -165,7 +169,7 @@ /** * 获取当前时间字符串,格式为yyyy-MM-dd - * + * * @return */ public static String getCurrentDateStrymd() { @@ -180,7 +184,7 @@ /** * 获取当前时间long值 - * + * * @return */ public static long getCurrentDataLong() { @@ -189,7 +193,7 @@ /** * 获取当前时间 - * + * * @param format * @return * @throws Exception @@ -294,7 +298,7 @@ /** * 对日期进行加法操作 - * + * * @param date * @param hours * @return @@ -311,7 +315,7 @@ /** * 对日期的分钟进行加法操作 - * + * * @param date * @param minutes * @return @@ -386,7 +390,7 @@ /** * 计算两个日期先差多少秒 - * + * * @param pre * 前一个日期 * @param after @@ -399,7 +403,7 @@ /** * 计算两个时间相差的天数 - * + * * @param stardate * @param enddate * @return @@ -412,16 +416,16 @@ 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; } /** * 结束时间与当前时间计算相差的月数 - * + * * @param enddate * @return */ @@ -440,7 +444,7 @@ /** * 获取某个时间段之前的时间点 - * + * * @return */ public static String getSomeTimeStapStr(String sign) { @@ -485,7 +489,7 @@ /** * 获取失效时间点(在什么时间失效) - * + * * @param minutes * 有效分钟数 * @return @@ -496,7 +500,7 @@ /** * 获取当前年份 - * + * * @return */ public static String getCurrentYear() { @@ -506,7 +510,7 @@ /** * 获取当前月份 - * + * * @return */ public static String getCurrentMonth() { @@ -523,7 +527,7 @@ /** * 获取无符号的当前时间 - * + * * @return */ public static String getCurrentDateStrNoSignal() { @@ -532,7 +536,7 @@ /** * 获取前几个小时的时间 - * + * * @param hours * @return */ @@ -548,7 +552,7 @@ /** * 小时取整 - * + * * @param date * @param hour * @return @@ -569,7 +573,7 @@ /** * 分钟取整 以十分钟为单位 ,去除尾端,加上 参数 46->40+minutes*10 - * + * * @param date * @param minutes * @return @@ -597,7 +601,7 @@ /** * 天数取整 - * + * * @param date * @param day * @return @@ -618,7 +622,7 @@ /** * 日期相加减 - * + * * @param time * 时间字符串 yyyy-MM-dd HH:mm:ss * @param num @@ -680,7 +684,7 @@ /** * 获取当前月第一天 - * + * * @return */ public static Date getFirstDayOfMonth() { @@ -693,7 +697,7 @@ /** * 获取当前年月的第一天时间 - * + * * @param year * 年份 * @param month @@ -717,7 +721,7 @@ /** * 获取当前年月的最后一天时间 - * + * * @param year * 年份 * @param month @@ -777,7 +781,7 @@ /** * date2比date1多的天数 - * + * * @param date1 * @param date2 * @return @@ -815,8 +819,28 @@ } /** + * 计算剩余天数,不足一天按一天算 + * @param expireDate + * @return + */ + public static int retrieveRemainingDays(Date expireDate) { + Date nowDate = new Date(); + Calendar startDate = Calendar.getInstance(); + startDate.setTime(nowDate); + Calendar endDate = Calendar.getInstance(); + endDate.setTime(expireDate); + Calendar date = (Calendar) startDate.clone(); + int daysBetween = 0; + while (date.before(endDate)) { + date.add(Calendar.DAY_OF_MONTH, 1); + daysBetween++; + } + return daysBetween; + } + + /** * 获取当前月第一天 - * + * * @return */ public static String getFirstDayOfMonthString() { @@ -832,7 +856,7 @@ /** * 获取当前月最后一天 - * + * * @return */ public static String getLastDayOfMonthString() { @@ -845,10 +869,25 @@ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(calendar.getTime()) + " 23:59:59"; } + /** + * 获取当前月最后一天 + * + * @return + */ + public static Date getLastDayOfMonth() { + Calendar calendar = Calendar.getInstance(); + + calendar.add(Calendar.MONTH, 1); + + calendar.set(Calendar.DAY_OF_MONTH, 0); + // 格式化日期 + + return calendar.getTime(); + } /** * 获取当前时间(年月日) - * + * * @return */ public static String getDayOfMonthString() { @@ -888,10 +927,13 @@ } public static void main(String[] args) throws Exception { - // Date date= new Date(); - // Date after = new Date(); - // System.out.println(getFirstDayOfMonthString()); - // System.out.println(getDayOfMonthString()); +// List<String> beforeDays = getBeforeDays(15); +// System.out.println(beforeDays); + List<String> yearMonths = getYearMonths(); + System.out.println(yearMonths); + List<String> latest12Month = getLatest12Month(LocalDate.now(), 10); + System.out.println(latest12Month); + } /** @@ -934,4 +976,247 @@ Date currYearFirst = calendar.getTime(); return currYearFirst; } + + /** + * 获取上月第一天时间 + * @return 上月第一天时间 + */ + public static Date getLastMonthFirst(){ + // 上月起始 + Calendar lastMonthFirstDateCal = Calendar.getInstance(); + lastMonthFirstDateCal.add(Calendar.MONTH,-1); + lastMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1); + String lastMonthFirstTime = yyyyMMdd_format.format(lastMonthFirstDateCal.getTime()) + " 00:00:00"; + return stringToDateStandard(lastMonthFirstTime); + } + + /** + * 获取上月最后一天时间 + * @return 最后一天时间 + */ + public static Date getLastMonthEnd(){ + // 上月末尾 + Calendar lastMonthEndDateCal = Calendar.getInstance(); + lastMonthEndDateCal.add(Calendar.MONTH,-1); + lastMonthEndDateCal.set(Calendar.DAY_OF_MONTH, lastMonthEndDateCal.getActualMaximum(Calendar.DAY_OF_MONTH)); + String lastMonthEndTime = yyyyMMdd_format.format(lastMonthEndDateCal.getTime()) + " 23:59:59"; + return stringToDateStandard(lastMonthEndTime); + } + + /** + * 获取近20天日期 + * @return 近20天日期 + */ + public static List<Date> getOldDays() { + List<Date> dates = new ArrayList<>(); + Date date = new Date(); + for (int i = 19; i >= 0; i--) { + Date date1 = DateUtils.addDay(date, -i); + dates.add(date1); + } + return dates; + } + + /** + * 获取上月最后一天时间 + * @return 上月最后一天时间 + */ + public static String getOldMonthLastDay(){ + Calendar calendar=Calendar.getInstance(); + int month=calendar.get(Calendar.MONTH); + calendar.set(Calendar.MONTH, month-1); + calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); + return format_yyymmdd.format(calendar.getTime()); + } + + /** + * 获取上月第一天时间 + * @return 上月第一天时间 + */ + public static String getOldMonthFirstDay(){ + Calendar calendar=Calendar.getInstance(); + calendar.add(Calendar.MONTH, -1); + calendar.set(Calendar.DAY_OF_MONTH, 1); + return format_yyymmdd.format(calendar.getTime()); + } + + /** + * 获取某年的月份最后一天时间 + * @param year 年份 + * @return 月份list + */ + public static List<String> getYearLastMonths(Integer year){ + List<String> list = new ArrayList<>(); + list.add(getLastDayOfMonth(year,1)); + list.add(getLastDayOfMonth(year,2)); + list.add(getLastDayOfMonth(year,3)); + list.add(getLastDayOfMonth(year,4)); + list.add(getLastDayOfMonth(year,5)); + list.add(getLastDayOfMonth(year,6)); + list.add(getLastDayOfMonth(year,7)); + list.add(getLastDayOfMonth(year,8)); + list.add(getLastDayOfMonth(year,9)); + list.add(getLastDayOfMonth(year,10)); + list.add(getLastDayOfMonth(year,11)); + list.add(getLastDayOfMonth(year,12)); + return list; + } + + /** + * 获取某年的月份第一天时间 + * @param year 年份 + * @return 月份list + */ + public static List<String> getYearFirstMonths(Integer year){ + List<String> list = new ArrayList<>(); + list.add(getFirstDayOfMonth(year,1)); + list.add(getFirstDayOfMonth(year,2)); + list.add(getFirstDayOfMonth(year,3)); + list.add(getFirstDayOfMonth(year,4)); + list.add(getFirstDayOfMonth(year,5)); + list.add(getFirstDayOfMonth(year,6)); + list.add(getFirstDayOfMonth(year,7)); + list.add(getFirstDayOfMonth(year,8)); + list.add(getFirstDayOfMonth(year,9)); + list.add(getFirstDayOfMonth(year,10)); + list.add(getFirstDayOfMonth(year,11)); + list.add(getFirstDayOfMonth(year,12)); + return list; + } + + /** + * 获取某年某月最后一天 + * @param year 年份 + * @param month 月份 + * @return 某年某月最后一天 + */ + public static String getLastDayOfMonth(int year,int month) + { + Calendar cal = Calendar.getInstance(); + //设置年份 + cal.set(Calendar.YEAR,year); + //设置月份 + cal.set(Calendar.MONTH, month-1); + //获取某月最大天数 + int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); + //设置日历中月份的最大天数 + cal.set(Calendar.DAY_OF_MONTH, lastDay); + //格式化日期 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + String lastDayOfMonth = sdf.format(cal.getTime()); + return lastDayOfMonth; + } + + /** + * 获取某年某月的第一天 + * @param year 年份 + * @param month 月份 + * @return 某年某月第一天 + */ + public static String getFirstDayOfMonth(int year,int month) + { + Calendar cal = Calendar.getInstance(); + //设置年份 + cal.set(Calendar.YEAR,year); + //设置月份 + cal.set(Calendar.MONTH, month-1); + //获取某月最小天数 + int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); + //设置日历中月份的最小天数 + cal.set(Calendar.DAY_OF_MONTH, firstDay); + //格式化日期 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + String firstDayOfMonth = sdf.format(cal.getTime()); + + 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; + } + + //获取当前12个月 + public static List<String> getLatest12Month(LocalDate date,int num){ + List<String> monthList = new ArrayList<>(); + for(int i = 0;i <= num-1; i++){ + LocalDate localDate = date.minusMonths(i); + String month = localDate.toString().substring(0,7); + monthList.add(month); + } + return monthList; + } + + } -- Gitblit v1.7.1