From d07b02f12c47f0e513d98a49d1106d22416868c0 Mon Sep 17 00:00:00 2001 From: CeDo <cedoogle@gmail.com> Date: 星期五, 11 六月 2021 13:37:29 +0800 Subject: [PATCH] bugfixed --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DateUtils.java | 91 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 91 insertions(+), 0 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 e02a10e..f31dc61c 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 @@ -594,6 +594,96 @@ } } + /** + * 日期相加减 + * @param time + * 时间字符串 yyyy-MM-dd HH:mm:ss + * @param num + * 加的数,-num就是减去 + * @return + * 减去相应的数量的年的日期 + * @throws ParseException + */ + public static Date yearAddNum(Date time, Integer num) { + //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + //Date date = format.parse(time); + + Calendar calendar = Calendar.getInstance(); + calendar.setTime(time); + calendar.add(Calendar.YEAR, num); + Date newTime = calendar.getTime(); + return newTime; + } + + /** + * 将日期对象格式化成yyyy-MM-dd格式的字符串 + * + * @param date 日期对象 + * @return String + */ + public static String getDateStringYMD(Date date) { + + String dateString = ""; + if (date != null) { + dateString = yyyyMMdd_format.format(date); + } + return dateString; + } + + /** + * 获取当前月第一天 + * @return + */ + public static Date getFirstDayOfMonth() { + Calendar cal = Calendar.getInstance(); + cal.add(Calendar.MONTH,0); + int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH); + cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00); + return cal.getTime(); + } + + /** + * 获取当前年月的第一天时间 + * @param year 年份 + * @param month 月份 + * @return 时间 + */ + public static Date getYearMonthStart(Integer year,Integer month) { + Calendar cal = Calendar.getInstance(); + // 设置年份 + cal.set(Calendar.YEAR, year); + // 设置月份 + cal.set(Calendar.MONTH, month - 1); + // 获取某月最小天数 + int firstDay = cal.getMinimum(Calendar.DATE); + // 设置日历中月份的最小天数 + cal.set(Calendar.DAY_OF_MONTH, firstDay); + int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH); + cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00); + return cal.getTime(); + } + + /** + * 获取当前年月的最后一天时间 + * @param year 年份 + * @param month 月份 + * @return 时间 + */ + public static Date getYearMonthEnd(Integer year,Integer month) { + Calendar cal = Calendar.getInstance(); + // 设置年份 + cal.set(Calendar.YEAR, year); + // 设置月份 + cal.set(Calendar.MONTH, month - 1); + // 获取某月最大天数 + int lastDay = cal.getMaximum(Calendar.DATE); + // 设置日历中月份的最大天数 + cal.set(Calendar.DAY_OF_MONTH, lastDay); + int actualMinimum = cal.getActualMinimum(Calendar.DAY_OF_MONTH); + cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY),actualMinimum,00,00,00); + return cal.getTime(); + } + @@ -602,6 +692,7 @@ // Date date= new Date(); // Date after = new Date(); // System.out.println(calTimeDifference(date,after)); + System.out.println(getYearMonthStart(2021,2)); } } -- Gitblit v1.7.1