java中时间处理的工具类
package com.itun.cloud.hospital.utils; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; /** * @description: 时间处理工具类 * @author wxs * @date: 2023-08-11 16:45 */ public class DateTimeUtils { //获取最近6个月及日期 public static List<String> getMonthDay(String date) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); List<String> list = new ArrayList<String>(); int month = Integer.parseInt(date.substring(5, 7)); int year = Integer.parseInt(date.substring(0, 4)); for (int i = 5; i >= 0; i--) { if (month > 6) { if (month - i >= 10) { list.add(year + "-" + String.valueOf(month - i) + "-" + getDaysOfMonth(sdf.parse(year + "-" + String.valueOf(month - i)))); } else { list.add(year + "-0" + String.valueOf(month - i) + "-" + getDaysOfMonth(sdf.parse(year + "-" + String.valueOf(month - i)))); } } else { if (month - i <= 0) { if (month - i + 12 >= 10) { list.add(String.valueOf(year - 1) + "-" + String.valueOf(month - i + 12) + "-" + getDaysOfMonth(sdf.parse(year + "-" + String.valueOf(month - i)))); } else { list.add(String.valueOf(year - 1) + "-0" + String.valueOf(month - i + 12) + "-" + getDaysOfMonth(sdf.parse(year + "-" + String.valueOf(month - i)))); } } else { if (month - i >= 10) { list.add(String.valueOf(year) + "-" + String.valueOf(month - i) + "-" + getDaysOfMonth(sdf.parse(year + "-" + String.valueOf(month - i)))); } else { list.add(String.valueOf(year) + "-0" + String.valueOf(month - i) + "-" + getDaysOfMonth(sdf.parse(year + "-" + String.valueOf(month - i)))); } } } } return list; } //获取最近7天 public static List<String> getDay() { List<String> dayList = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = null; for (int i = 6; i >= 0; i--) { c = Calendar.getInstance(); c.add(Calendar.DAY_OF_MONTH, -i); dayList.add(sdf.format(c.getTime())); } return dayList; } //获取当月天数 public static int getDaysOfMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); } // 获取几小时之前的时间 public static Date getOverHourTime(int hour, Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.HOUR, hour); return calendar.getTime(); } // 获取几天之前的时间 public static Date getOverDayTime(int day, Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, day); return calendar.getTime(); } // 获取几天之前的时间 public static String getOverDayStr(int day, Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, day); return fomatDateStr(calendar.getTime()); } // 获取几天之前的时间 public static String getOverDayTimeStr(int day, Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, day); return fomatDateTimeStr(calendar.getTime()); } // 获取几月之前的时间 public static Date getOverMonthTime(int month, Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, month); return calendar.getTime(); } // 获取几年之前的时间 public static Date getOverYearTime(int year, Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_YEAR, year); return calendar.getTime(); } // 获取几年之前的时间 public static String getOverYearStr(int year, Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_YEAR, year); Date newDate = calendar.getTime(); return fomatDateStr(newDate); } // 获取几年之前的时间 public static String getOverYearStrNew(int year, String date) { String substring = date.substring(0, 4); int nowYear = Integer.parseInt(substring); int newYear = nowYear + year; String endDate = date.substring(4); return newYear + endDate; } public static Date fomatDate(String date) { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); try { return fmt.parse(date); } catch (ParseException var3) { var3.printStackTrace(); return null; } } public static Date fomatDate(String date, String pattern) { SimpleDateFormat fmt = new SimpleDateFormat(pattern); try { return fmt.parse(date); } catch (ParseException var3) { var3.printStackTrace(); return null; } } public static Date fomatDateTime(String date) { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return fmt.parse(date); } catch (ParseException var3) { var3.printStackTrace(); return null; } } public static String fomatDateStr(String date) { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Date date1 = DateTimeUtils.fomatDate(date); return fmt.format(date1); } public static String fomatDateTimeStr(String date) { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = DateTimeUtils.fomatDate(date); return fmt.format(date1); } public static String fomatDateStr(Date date) { try { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); return fmt.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static String fomatDateTimeStr(Date date) { try { SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return fmt.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static String fomatDateStr(Date date, String pattern) { try { SimpleDateFormat fmt = new SimpleDateFormat(pattern); return fmt.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } public static String fomatDateTimeStr(Date date, String pattern) { try { SimpleDateFormat fmt = new SimpleDateFormat(pattern); return fmt.format(date); } catch (Exception e) { e.printStackTrace(); return null; } } // 获取本周开始时间 public static Date getBeginDayOfWeek(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); int dayofweek = cal.get(Calendar.DAY_OF_WEEK); if (dayofweek == 1) { dayofweek += 7; } cal.add(Calendar.DATE, 2 - dayofweek); return getDayStartTime(cal.getTime()); } // 获取本周结束时间 public static Date getEndDayOfWeek(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(getBeginDayOfWeek(date)); cal.add(Calendar.DAY_OF_WEEK, 6); Date weekEndSta = cal.getTime(); return getDayEndTime(weekEndSta); } // 获取本周开始时间 public static String getBeginDayOfWeekStr(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); int dayofweek = cal.get(Calendar.DAY_OF_WEEK); if (dayofweek == 1) { dayofweek += 7; } cal.add(Calendar.DATE, 2 - dayofweek); return fomatDateStr(getDayStartTime(cal.getTime())); } // 获取本周结束时间 public static String getEndDayOfWeekStr(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(getBeginDayOfWeek(date)); cal.add(Calendar.DAY_OF_WEEK, 6); Date weekEndSta = cal.getTime(); return fomatDateStr(getDayEndTime(weekEndSta)); } //获取某个日期的开始时间 public static Timestamp getDayStartTime(Date d) { Calendar calendar = Calendar.getInstance(); if (null != d) calendar.setTime(d); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0); calendar.set(Calendar.MILLISECOND, 0); return new Timestamp(calendar.getTimeInMillis()); } //获取某个日期的结束时间 public static Timestamp getDayEndTime(Date d) { Calendar calendar = Calendar.getInstance(); if (null != d) calendar.setTime(d); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59); calendar.set(Calendar.MILLISECOND, 999); return new Timestamp(calendar.getTimeInMillis()); } // 获取昨天日期 public static Date getYesToday(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); return cal.getTime(); } // 获取昨天日期 public static String getYesTodayStr(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); return fomatDateStr(cal.getTime()); } // 某月的开始时间 public static Date getMonthStartDate(Date date) throws ParseException { SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-"); String startTime = sdfTwo.format(date) + "01 00:00:00"; return sdfTwo.parse(startTime); } // 某月的结束日期 public static Date getMonthEndDate(Date date) throws ParseException { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_MONTH, 1); cal.roll(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } // 某月的开始时间 public static String getMonthStartDateStr(Date date) { try { SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-"); String startTime = sdfTwo.format(date) + "01 00:00:00"; return fomatDateStr(sdfTwo.parse(startTime)); } catch (Exception e) { e.printStackTrace(); return null; } } // 某月的开始时间 public static String getMonthEndDateStr(int year, int month) { try { Calendar a = Calendar.getInstance(); a.set(Calendar.YEAR, year);//把日期设置为当月第一天 a.set(Calendar.MONTH, month);//把日期设置为当月第一天 a.set(Calendar.DATE, 1);//把日期设置为当月第一天 //当月有多少天 int maxDate = a.get(Calendar.DATE); SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-"); return sdfTwo.format(new Date()) + maxDate; } catch (Exception e) { e.printStackTrace(); return null; } } // 某月的结束日期 public static String getMonthEndDateStr(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_MONTH, 1); cal.roll(Calendar.DAY_OF_MONTH, -1); return fomatDateStr(cal.getTime()); } //获取季度第一天 public static Date getQuarterStartDate(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.MONTH, (((int) cal.get(Calendar.MONTH)) / 3) * 3); cal.set(Calendar.DAY_OF_MONTH, 1); return cal.getTime(); } //获取季度最后一天 public static Date getQuarterEndDate(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); //计算季度数:由于月份从0开始,即1月份的Calendar.MONTH值为0,所以计算季度的第三个月份只需 月份 / 3 * 3 + 2 cal.set(Calendar.MONTH, (((int) cal.get(Calendar.MONTH)) / 3) * 3 + 2); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); return cal.getTime(); } //获取季度第一天 public static String getQuarterStartDateStr(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.MONTH, (((int) cal.get(Calendar.MONTH)) / 3) * 3); cal.set(Calendar.DAY_OF_MONTH, 1); return fomatDateStr(cal.getTime()); } //获取季度最后一天 public static String getQuarterEndDateStr(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); //计算季度数:由于月份从0开始,即1月份的Calendar.MONTH值为0,所以计算季度的第三个月份只需 月份 / 3 * 3 + 2 cal.set(Calendar.MONTH, (((int) cal.get(Calendar.MONTH)) / 3) * 3 + 2); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); return fomatDateStr(cal.getTime()); } // 获取本年第一天 public static Date getYearStartDate(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_YEAR, 1); return cal.getTime(); } // 获取本年最后一天 public static Date getYearEndDate(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR)); return cal.getTime(); } // 获取本年第一天 public static String getYearStartDateStr(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_YEAR, 1); return fomatDateStr(cal.getTime()); } public static String getTodaystr() { Date date = new Date(); return fomatDateStr(date); } // 获取本年最后一天 public static String getYearEndDateStr(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR)); return fomatDateStr(cal.getTime()); } /** * 获取周的开始日期 * * @param year * @param weekNo * @return */ public static String getStartDayOfWeekNo(int year, int weekNo) { Calendar cal = getCalendarFormYear(year); cal.set(Calendar.WEEK_OF_YEAR, weekNo); String start = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH); return fomatDateStr(start); } /** * 获取周的结束日期 * * @param year * @param weekNo * @return */ public static String getEndDayOfWeekNo(int year, int weekNo) { Calendar cal = getCalendarFormYear(year); cal.set(Calendar.WEEK_OF_YEAR, weekNo); cal.add(Calendar.DAY_OF_WEEK, 6); String end = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" + cal.get(Calendar.DAY_OF_MONTH); return fomatDateStr(end); } /** * get Calendar of given year * * @param year * @return */ private static Calendar getCalendarFormYear(int year) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); cal.set(Calendar.YEAR, year); return cal; } // 获取第几周 public static int getWeeks(Date date) { Calendar calendar = Calendar.getInstance(); //设置星期一为一周开始的第一天 calendar.setFirstDayOfWeek(Calendar.MONDAY); //获得当前的时间戳 calendar.setTime(date); //获得当前日期属于今年的第几周 return calendar.get(Calendar.WEEK_OF_YEAR); } // 获取几月 public static int getMonth(Date date) { Calendar calendar = Calendar.getInstance(); //获得当前的时间戳 calendar.setTime(date); //获得当前日期属于今年的第几周 return calendar.get(Calendar.MONTH) + 1; } // 获取季度 public static int getQuarter(Date date) { Calendar calendar = Calendar.getInstance(); //获得当前的时间戳 calendar.setTime(date); //获得当前日期属于今年的第几周 int month = calendar.get(Calendar.MONTH) + 1; Double ceil = Math.ceil((double) month / 3); int result = ceil.intValue(); return result; } // 获取年度 public static int getYear(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); return year; } /** * @param type date * @Author wxs * @Description 获取dateContent * @Date 2022/10/13 8:40 * @Param **/ public static Integer getDateCount(String type, String date) { switch (type) { case "day": return 1; case "week": return DateTimeUtils.getWeeks(DateTimeUtils.fomatDate(date)); case "month": return DateTimeUtils.getMonth(DateTimeUtils.fomatDate(date)); case "quarter": return DateTimeUtils.getQuarter(DateTimeUtils.fomatDate(date)); case "year": return DateTimeUtils.getYear(DateTimeUtils.fomatDate(date)); default: return 0; } } public static List<String> getDateList(String dateType, String startTime, String endTime) { try { List<String> dateList = new ArrayList<>(); String year = startTime.substring(0, 4); if ("quarter".equals(dateType)) { if ((year + "-01").equals(startTime)) { dateList.add(year + "-01"); dateList.add(year + "-02"); dateList.add(year + "-03"); } if ((year + "-04").equals(startTime)) { dateList.add(year + "-04"); dateList.add(year + "-05"); dateList.add(year + "-06"); } if ((year + "-07").equals(startTime)) { dateList.add(year + "-07"); dateList.add(year + "-08"); dateList.add(year + "-09"); } if ((year + "-10").equals(startTime)) { dateList.add(year + "-10"); dateList.add(year + "-11"); dateList.add(year + "-12"); } } else if ("month".equals(dateType)) { String newDate = startTime; dateList.add(newDate); while (!newDate.equals(endTime)) { newDate = DateTimeUtils.fomatDateStr(DateTimeUtils.getOverMonthTime(1, DateTimeUtils.fomatDate(newDate, "yyyy-MM")), "yyyy-MM"); dateList.add(newDate); } } else { String newDate = startTime; dateList.add(newDate); while (!newDate.equals(endTime)) { newDate = DateTimeUtils.fomatDateStr(DateTimeUtils.getOverDayTime(1, DateTimeUtils.fomatDate(newDate))); dateList.add(newDate); } } return dateList; } catch (Exception e) { e.printStackTrace(); } return null; } //获取两个传入时间相隔的天数 public static long getInterval(Date begin_date, Date end_date) throws ParseException { long day = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); if (begin_date != null) { String begin = sdf.format(begin_date); begin_date = sdf.parse(begin); } else { begin_date = sdf.parse(sdf.format(new Date())); } if (end_date != null) { String end = sdf.format(end_date); end_date = sdf.parse(end); } else { end_date = sdf.parse(sdf.format(new Date())); } day = (end_date.getTime() - begin_date.getTime()) / (24 * 60 * 60 * 1000); return day >= 0 ? day : -day; } //获取两个传入时间相隔的天数 public static int getInterval(String start, String end) throws ParseException { Date begin_date = fomatDate(start); Date end_date = fomatDate(end); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); if (begin_date != null) { String begin = sdf.format(begin_date); begin_date = sdf.parse(begin); } else { begin_date = sdf.parse(sdf.format(new Date())); } if (end_date != null) { String end2 = sdf.format(end_date); end_date = sdf.parse(end2); } else { end_date = sdf.parse(sdf.format(new Date())); } long day = (end_date.getTime() - begin_date.getTime()) / (24 * 60 * 60 * 1000); day = day >= 0 ? day : -day; return Integer.parseInt(String.valueOf(day)); } //获取传入时间与当前时间相隔的天数 public static long getInterval(Date date) throws ParseException { long day = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); if (date != null) { String end = sdf.format(date); date = sdf.parse(end); } else { throw new NullPointerException(); } Date nowDate = sdf.parse(sdf.format(new Date())); day = (date.getTime() - nowDate.getTime()) / (24 * 60 * 60 * 1000); return day >= 0 ? day : -day; } public static void main(String[] args) throws ParseException { Date yesToday = DateTimeUtils.getYearEndDate(new Date()); String s = DateTimeUtils.fomatDateStr(yesToday); System.out.println(s); } }