常用日期工具类-DateUtils
常用日期工具类-DateUtils
/** * @Author * @ClassName DateUtil * @Description 时间工具类 * @Date 2018/12/12 17:02 * @Version 1.0 */ public class DateUtil { /** * @return * @Author * @Description 获取当前日期的字符串 * @Date 2019/3/22 14:22 * @Param */ public static String dateToString() { Date date = new Date(); SimpleDateFormat sDformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); String format = sDformat.format(date); return format; } /** * @return * @Author * @Description 将日期字符串yyyy-MM-dd 转换成 yyyy年MM月dd日 * @Date 2018/12/12 17:02 * @Param */ public static String dateStrToStr(String dateStr) { if (dateStr == null || dateStr.equals("")) { return null; } else { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CANADA); try { Date date = simpleDateFormat.parse(dateStr); SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); String dateFormat = format.format(date); return dateFormat; } catch (ParseException e) { e.printStackTrace(); } } return null; } /** * @return * @Author * @Description 把两个字符串时间转换成时间段(xxxx年xx月xx日--xxxx年xx月xx日) * @Date 2019/1/24 15:10 * @Param */ public static String dateStrSplicing(String startTime, String endTime) { String sTime = dateStrToStr(startTime); String eTime = dateStrToStr(endTime); return sTime + "--" + eTime; } /** * @return * @Author * @Description 通过两个日期字符串(eg:2019-1-12)计算相差天数 * @Date 2019/1/25 13:45 * @Param */ public static String differDate(String startTime, String endTime) throws ParseException { if (startTime == null || endTime == null) { return "0"; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CANADA); Date d1 = sdf.parse(startTime); Date d2 = sdf.parse(endTime); long daysBetween = (d2.getTime() - d1.getTime() + 60 * 60 * 24 * 1000) / (60 * 60 * 24 * 1000); return String.valueOf(daysBetween); } /** * @param time 时间 * @param num 加的数,-num就是减去 * @return 减去相应的数量的天的日期 * @throws ParseException Date */ public static String dayAddNum(String time, Integer num) throws ParseException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = format.parse(time); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, num); Date newTime = calendar.getTime(); String format1 = format.format(newTime); return format1; } /** * @return * @param null * @Author * @Description 获取当前年的第当前季度 * @Date 2019/5/15 15:07 * @Param */ public static String getCurrentYearQuarter() { Calendar cal = Calendar.getInstance(); //获取年份 int year = cal.get(Calendar.YEAR); //当前月份 int m = cal.get(Calendar.MONTH) + 1; String quarter = " "; if (m >= 1 && m == 3) { quarter = "1"; } if (m >= 4 && m <= 6) { quarter = "2"; } if (m >= 7 && m <= 9) { quarter = "3"; } if (m >= 10 && m <= 12) { quarter = "4"; } return String.valueOf(year) + "年第" + quarter + "季度"; } /** * @Author * @Description 根据开始时间、结束时间计算周数 * @Date 2019/6/21 10:18 * @Param * @return * @param null */ public static float countWeekNum(String strDateStar,String strDateEnd) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date dateStart = sdf.parse(strDateStar ); Date dateEnd = sdf.parse(strDateEnd ); float weekNumF = ((float) (dateEnd.getTime() - dateStart.getTime() + 86400000)) / 86400000 / 7; //取一位小数 float weekNum = (float)(Math.round(weekNumF*10))/10; return weekNum; } /** * 按照自然日、周、月、年分割时间周期 * * @param dateUnitEnum 拆分时间单位 * @param startDate 开始日期 * @param endDate 结束日期 * @return * @throws ParseException */ public static List<String> splitDate(DateUnitEnum dateUnitEnum, String startDate, String endDate) throws ParseException { List<String> listWeekOrMonth = new ArrayList<>(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date sDate = dateFormat.parse(startDate); Calendar sCalendar = Calendar.getInstance(); sCalendar.setFirstDayOfWeek(Calendar.MONDAY); sCalendar.setTime(sDate); Date eDate = dateFormat.parse(endDate); Calendar eCalendar = Calendar.getInstance(); eCalendar.setFirstDayOfWeek(Calendar.MONDAY); eCalendar.setTime(eDate); boolean bool = true; switch (dateUnitEnum) { case DAY: while (sCalendar.getTime().getTime() < eCalendar.getTime().getTime()) { listWeekOrMonth.add(dateFormat.format(sCalendar.getTime())); sCalendar.add(Calendar.DATE, 1); } listWeekOrMonth.add(dateFormat.format(eCalendar.getTime())); if (listWeekOrMonth.size() % 2 != 0) { listWeekOrMonth.add(dateFormat.format(eCalendar.getTime())); } break; case WEEK: while (sCalendar.getTime().getTime() < eCalendar.getTime().getTime()) { if (bool || sCalendar.get(Calendar.DAY_OF_WEEK) == 2 || sCalendar.get(Calendar.DAY_OF_WEEK) == 1) { listWeekOrMonth.add(dateFormat.format(sCalendar.getTime())); bool = false; } sCalendar.add(Calendar.DAY_OF_MONTH, 1); } listWeekOrMonth.add(dateFormat.format(eCalendar.getTime())); if (listWeekOrMonth.size() % 2 != 0) { listWeekOrMonth.add(dateFormat.format(eCalendar.getTime())); } break; case MONTH: while (sCalendar.getTime().getTime() < eCalendar.getTime().getTime()) { if (bool || sCalendar.get(Calendar.DAY_OF_MONTH) == 1 || sCalendar.get(Calendar.DAY_OF_MONTH) == sCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)) { listWeekOrMonth.add(dateFormat.format(sCalendar.getTime())); bool = false; } sCalendar.add(Calendar.DAY_OF_MONTH, 1); } listWeekOrMonth.add(dateFormat.format(eCalendar.getTime())); if (listWeekOrMonth.size() % 2 != 0) { listWeekOrMonth.add(dateFormat.format(eCalendar.getTime())); } break; case YEAR: // 按年 while (sCalendar.getTime().getTime() < eCalendar.getTime().getTime()) { if (bool || sCalendar.get(Calendar.DAY_OF_YEAR) == 1 || sCalendar.get(Calendar.DAY_OF_YEAR) == sCalendar.getActualMaximum(Calendar.DAY_OF_YEAR)) { listWeekOrMonth.add(dateFormat.format(sCalendar.getTime())); bool = false; } sCalendar.add(Calendar.DAY_OF_YEAR, 1); } listWeekOrMonth.add(dateFormat.format(eCalendar.getTime())); if (listWeekOrMonth.size() % 2 != 0) { listWeekOrMonth.add(dateFormat.format(eCalendar.getTime())); } break; default: throw new RuntimeException("拆分时间单位错误!"); } return listWeekOrMonth; } /** * 拆分时间单位 */ @AllArgsConstructor @Getter private enum DateUnitEnum { /** * 天 */ DAY, /** * 周 */ WEEK, /** * 月 */ MONTH, /** * 年 */ YEAR } }