import cn.hutool.core.util.StrUtil; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; public class DateUtils { //计算两个时间相差的天数 public static Integer timeInterval(String a, String b) { // 自定义时间格式 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 获取日历对象 Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); Date date1 = null; Date date2 = null; try { //字符串转Date date1 = simpleDateFormat.parse(a); date2 = simpleDateFormat.parse(b); // 设置日历 calendar1.setTime(date1); calendar2.setTime(date2); } catch (Exception e) { e.printStackTrace(); } long time1 = calendar1.getTimeInMillis(); long time2 = calendar2.getTimeInMillis(); //计算相差天数 Integer timeInterval = Math.toIntExact((time2 - time1) / (1000 * 3600 * 24)); return timeInterval; } //计算两个日期相差年数 public static int yearDateDiff(String startDate,String endDate){ Calendar calBegin = Calendar.getInstance(); //获取日历实例 Calendar calEnd = Calendar.getInstance(); calBegin.setTime(stringTodate(startDate,"yyyy")); //字符串按照指定格式转化为日期 calEnd.setTime(stringTodate(endDate,"yyyy")); return calEnd.get(Calendar.YEAR) - calBegin.get(Calendar.YEAR); } //字符串按照指定格式转化为日期 public static Date stringTodate(String dateStr, String formatStr) { // 如果时间为空则默认当前时间 Date date = null; SimpleDateFormat format = new SimpleDateFormat(formatStr); if (dateStr != null && !dateStr.equals("")) { String time = ""; try { Date dateTwo = format.parse(dateStr); time = format.format(dateTwo); date = format.parse(time); } catch (ParseException e) { e.printStackTrace(); } } else { String timeTwo = format.format(new Date()); try { date = format.parse(timeTwo); } catch (ParseException e) { e.printStackTrace(); } } return date; } //Date转为LocalDate public static LocalDate date2LocalDate(Date date) { if(null == date) { return null; } return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); } //Date转为LocalDateTime public static LocalDateTime date2LocalDateTime(Date date) { if(null == date) { return null; } Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime(); return localDateTime; } //LocalDate转换成Date public static Date localDate2Date(LocalDate localDate) { if(null == localDate) { return null; } ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault()); return Date.from(zonedDateTime.toInstant()); } //LocalDateTime转换成Date public static Date localDateTime2Date(LocalDateTime localDateTime) { return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); } //LocalDate格式化 public static String formatDate(Date date) { LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); } //1.LocalDateTime转为"yyyy/MM/dd" public static String localDateTime2String(LocalDateTime date) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); return date.format(dtf); } //2.LocalDateTime转为"yyyy-MM-dd" public static String localDateTime2StringTime(LocalDateTime date) { DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); return date.format(dtf); } /** * 计算两个日期之间相差的天数 * @param smdate 较小的时间 * @param bdate 较大的时间 * @return 相差天数 * @throws ParseException */ public static int daysBetween(Date smdate,Date bdate) throws ParseException { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); smdate=sdf.parse(sdf.format(smdate)); bdate=sdf.parse(sdf.format(bdate)); Calendar cal = Calendar.getInstance(); cal.setTime(smdate); long time1 = cal.getTimeInMillis(); cal.setTime(bdate); long time2 = cal.getTimeInMillis(); long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days)); } public static String getWeek(String dates) { Calendar cal = Calendar.getInstance(); SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); Date d = null; try { d = f.parse(dates); } catch (ParseException e) { e.printStackTrace(); } cal.setTime(d); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; String week = " "; if (w == 1) { week = "一"; } else if (w == 2) { week = "二"; } else if (w == 3) { week = "三"; } else if (w == 4) { week = "四"; } else if (w == 5) { week = "五"; } else if (w == 6) { week = "六"; } else if (w == 0) { week = "日"; } return ("周" + week); } //字符串转Date public static Date StringToDate(String time){ try { if(StrUtil.isEmpty(time)){ return null; } SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.parse(time); } catch (ParseException e) { e.printStackTrace(); } return null; } //Date转字符串 public static String dateToString(Date date){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(date); } //获取本月的天数 public static Integer getDayOfMonth(String time) throws ParseException { SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); calendar.setTime(sm.parse(time)); int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); return maxDay; } //获取传入日期加指定天数后的日期 public static Date getAddDate(Date date, Integer plusDay) throws ParseException { LocalDateTime localDateTime = DateUtils.date2LocalDateTime(date); LocalDateTime addDateTime = LocalDateTime.now(); if (null != plusDay){ addDateTime = localDateTime.plusDays(plusDay); } Date addDate = DateUtils.localDateTime2Date(addDateTime); return addDate; } /** 比较两个时间相差小时 */ public static double calculatetimeGapHour(Date time1, Date time2) { //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); double hour = 0; try { //Date date1, date2; //date1 = simpleDateFormat.parse(time1); // date2 = simpleDateFormat.parse(time2); //double millisecond = date2.getTime() - date1.getTime(); double millisecond = time2.getTime() - time1.getTime(); hour = millisecond / (60 * 60 * 1000); } catch (Exception e) { e.printStackTrace(); } return hour; } //获取当前时间的前一天 public static String getNextDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, -1); date = calendar.getTime(); SimpleDateFormat strTime = new SimpleDateFormat("yyyy-MM-dd"); String time = strTime.format(date); return time; } //日期加指定天数(yyyy-MM-dd) public static String dateMinusDay(String str,Integer day) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date dt = sdf.parse(str);//将字符串生成Date Calendar rightNow = Calendar.getInstance(); rightNow.setTime(dt);//使用给定的 Date 设置此 Calendar 的时间。 rightNow.add(Calendar.DAY_OF_YEAR,+(day));// 日期减1天 Date dt1 = rightNow.getTime();//返回一个表示此 Calendar 时间值的 Date 对象。 String reStr = sdf.format(dt1);//将给定的 Date 格式化为日期/时间字符串,并将结果添加到给定的 StringBuffer。 return reStr; } catch (ParseException e) { e.printStackTrace(); return null; } } }
JAVA日期工具类方法
于 2023-07-19 17:17:41 首次发布