• Java实用的DateUtils时间工具类介绍


    简介: 判断两个时间段是否重叠、上个月的最后一天、构造一个时间段、将单元格数据转换为日期样式的字符串、将单元格数据转换为日期样式的字符串、计算字符型日期天数差

    /**
     * 日期工具类
     * 

    * 使用LocalDateTime存取或判断 *

    */ @Component public class DateUtils { /** * 判断两个时间段是否重叠 * @param slot1 * @param slot2 * @return */ public static boolean overlapped(TimeSlot slot1, TimeSlot slot2) { TimeSlot previous, next; previous = slot1.startTime.isBefore(slot2.startTime) ? slot1 : slot2; next = slot2.startTime.isAfter(slot1.startTime) ? slot2 : slot1; // 这里业务需要,允许时间点的重叠 // 例如某个时间段的起始时间:2020-06-29 00:00:00 // 和另一个时间段的终止时间:2020-06-29 00:00:00 // 它们俩可以有交点。如果不需要这种逻辑只把le改成lt // ,ge改成gt就可 return !(lt(previous, next) || gt(previous, next)); } /** * 上个月的最后一天 */ public static String afterMonthLastDay() { Calendar c = Calendar.getInstance(); //当前日期设置为指定日期 c.setTime(new Date()); //指定日期月份减去一 c.add(Calendar.MONTH, -1); //指定日期月份减去一后的 最大天数 c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE)); //获取上给月最后一天的日期 Date lastDateOfPrevMonth = c.getTime(); java.text.DateFormat formater = new SimpleDateFormat("yyyy-MM-dd"); return formater.format(lastDateOfPrevMonth); } /** * 构造一个时间段 * @param startTime * @param endTime * @return */ public static TimeSlot buildSlot(LocalDateTime startTime, LocalDateTime endTime) { return new TimeSlot(startTime, endTime); } /** * less equal * 小于等于 * @param prev * @param next * @return */ private static boolean le(TimeSlot prev, TimeSlot next) { return lt(prev, next) || next.endTime.isEqual(prev.startTime); } /** * greater equal * 大于等于 * @param prev * @param next * @return */ private static boolean ge(TimeSlot prev, TimeSlot next) { return gt(prev, next) || prev.endTime.isEqual(next.startTime); } /** * greater than * 大于 * @param prev * @param next * @return */ private static boolean gt(TimeSlot prev, TimeSlot next) { return prev.endTime.isBefore(next.startTime); } /** * less than * 小于 * @param prev * @param next * @return */ private static boolean lt(TimeSlot prev, TimeSlot next) { return next.endTime.isBefore(prev.startTime); } /** * 将单元格数据转换为日期样式的字符串 * * @param cell * @return */ public static String convertDate(Cell cell) { String value = ""; if (cell.getCellType() == CellType.NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(cell)) { //用于转化为日期格式 Date d = cell.getDateCellValue(); java.text.DateFormat formater = new SimpleDateFormat("yyyy-MM-dd"); value = formater.format(d); } } return value; } /** * 将单元格数据转换为日期样式的字符串 * * @param cell * @return */ public static String convertString(Cell cell) { String value = ""; if (cell.getCellType() == CellType.STRING) { value = cell.getStringCellValue(); } return value; } /** * 将单元格数据转换为金额样式的字符串 * @author:xhmaca * @param cell * @return */ public static String convertBigDecimal(Cell cell) { String value = ""; if (cell.getCellType() == CellType.NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(cell)) { } else { BigDecimal bigDecimal = new BigDecimal(cell.getNumericCellValue()); value = bigDecimal.toString(); } } return value; } /** * 计算字符型日期天数差 * @param dateStart * @param dateStop */ public static Integer dateDifferent(String dateStart, String dateStop) { long diff,diffSeconds,diffMinutes,diffHours,diffDays = 0; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date d1 = format.parse(dateStart);//毫秒ms Date d2 = format.parse(dateStop);//毫秒ms diff = d2.getTime() - d1.getTime(); diffSeconds = diff / 1000 % 60; diffMinutes = diff / (60 * 1000) % 60; diffHours = diff / (60 * 60 * 1000) % 24; diffDays = diff / (24 * 60 * 60 * 1000); System.out.print("两个时间相差:"); System.out.print(diffDays + "天,"); System.out.print(diffHours + "小时,"); System.out.print(diffMinutes + "分钟,"); System.out.print(diffSeconds + "秒."); } catch (ParseException e) { e.printStackTrace(); } int days = (int) diffDays; return days; } /** * 计算字符型日期天数差 * @param dateStart * @param dateStop */ public static long datelongDifferent(String dateStart, String dateStop) { long diff,diffSeconds,diffMinutes,diffHours,diffDays = 0; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date d1 = format.parse(dateStart);//毫秒ms Date d2 = format.parse(dateStop);//毫秒ms diff = d2.getTime() - d1.getTime(); diffSeconds = diff / 1000 % 60; diffMinutes = diff / (60 * 1000) % 60; diffHours = diff / (60 * 60 * 1000) % 24; diffDays = diff / (24 * 60 * 60 * 1000); System.out.print("两个时间相差:"); System.out.print(diffDays + "天,"); System.out.print(diffHours + "小时,"); System.out.print(diffMinutes + "分钟,"); System.out.print(diffSeconds + "秒."); } catch (ParseException e) { e.printStackTrace(); } return diffDays; } /** * 获取当前日期 */ public static String getCurrectDate() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date();//当前日期 return df.format(date); } /** * 获取当前日期 */ public static String getLongFormatDate(String date) { SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return df1.format(date); } /** * 获取当前日期 */ public static String getShortFormatDate(String date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); return df.format(date); } /** * 计算年份 * @param dateStart * */ public static Integer getYear(String dateStart) { long diff,diffSeconds,diffMinutes,diffHours,diffDays = 0; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date d1 = format.parse(dateStart);//毫秒ms return d1.getYear()+1900; } catch (ParseException e) { e.printStackTrace(); } return 0; } /** * 时间段类 */ static class TimeSlot{ private LocalDateTime startTime; private LocalDateTime endTime; public TimeSlot(LocalDateTime startTime, LocalDateTime endTime) { if (startTime.isAfter(endTime)) { this.startTime = endTime; this.endTime = startTime; } else { this.startTime = startTime; this.endTime = endTime; } } } }
  • 相关阅读:
    Android向服务器的数据库MySQL传输数据:经过修正的 Android + HTTP + xampp +mysql : Post / Get
    torch.nn.functional.grid_sample(F.grid_sample)函数的说明 & 3D空间中的点向图像投影的易错点
    css 左右宽固定,中间自适应——双飞翼布局
    NodeJS校园快递智能互助平台-计算机毕业设计源码58554
    网络安全(黑客)自学笔记
    岩藻多糖-聚已内酯 Fucoidan-PCL 聚已内酯-PEG-岩藻多糖
    WPF文本框中加提示语
    CentOS 7.6使用mysql-8.0.31-1.el7.x86_64.rpm-bundle.tar安装Mysql 8.0
    m基于GA遗传优化的BP神经网络时间序列预测算法matlab仿真
    SpringBoot启动调用类方法 【实现数据加载等操作】
  • 原文地址:https://blog.csdn.net/qq_25580555/article/details/128130505