From 42239f79490e7130a63d51a67efd1f2c8d23be37 Mon Sep 17 00:00:00 2001 From: xyh <18782104331@139.com> Date: 星期五, 18 六月 2021 09:50:44 +0800 Subject: [PATCH] 平均耗时格式 --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DateUtils.java | 123 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 123 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..deddc1a 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 @@ -33,6 +33,8 @@ public static DateTimeFormatter format_ymd_String = DateTimeFormatter.ofPattern("yyMMdd"); + public static SimpleDateFormat yyyyMM_format = new SimpleDateFormat("yyyy-MM"); + /** @@ -594,6 +596,126 @@ } } + /** + * 日期相加减 + * @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(); + } + + public static Date getDateM(Date start, int min) { + Calendar date1 = Calendar.getInstance(); + date1.setTime(start); + date1.add(Calendar.MONTH, min); + return date1.getTime(); + } + + public static Date parseDateYM(String str) { + try { + return yyyyMM_format.parse(str); + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + + public static int getMonth(Date date){ + Calendar instance = Calendar.getInstance(); + instance.setTime(date); + return instance.get(Calendar.MONTH); + } + + public static boolean before(Date start, Date end){ + Calendar date1 = Calendar.getInstance(); + date1.setTime(start); + Calendar date2 = Calendar.getInstance(); + date2.setTime(end); + return date1.before(date2); + } @@ -602,6 +724,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