栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

封装 时间工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

封装 时间工具类

前言

工作开发中对时间类型 Date 和 LocalDateTime 时间类型处理蛮多的,所以自己封装了一个工具类

目录:
  • Date类型的str 转 时间戳
  • Date转string 字符
  • LocalDate 转 string
  • String转 LocalDate
  • String 转 Date
  • 时间戳转 Date 类型
  • 比较两个时间,如果endtime晚于begintime,则返回true,否则返回false
  • 比较两个时间,如果endtime晚于begintime,则返回true,否则返回false
  • string转date加日期
  • 某个时间往后推迟 num 日/月/年
  • LocalDateTime 转为string
  • LocalDateTime 转 Date
public class DateTimeUtil {

    public static final String DEFAULT_FORMAT_DATE = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_YMD = "yyyyMMdd";
    public static final String FORMAT_YMDHM = "yyyyMMddHHmm";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String FORMAT_YMDHMS = "yyyyMMddHHmmss";


    
    public static long dateTimeParse(String datetime, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date date = sdf.parse(datetime);
            return date.getTime();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return 0;
    }

    
    public static String formatDateToString(Date date, String formatStr) {
        return (new SimpleDateFormat((formatStr == null ? DEFAULT_FORMAT_DATE : formatStr))).format(date);
    }

    
    public static String formatLocalDateToString(LocalDate date, String formatStr) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(formatStr == null ? "yyyy-MM-dd" : formatStr);
        return date.format(fmt);
    }

    
    public static LocalDate formatStringToLocalDate(String dateStr, String formatStr) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(formatStr == null ? "yyyy-MM-dd" : formatStr);
        return LocalDate.parse(dateStr, fmt);
    }

    
    public static Date formatStringToDate(String dateStr, String formatStr) throws ParseException {
        return (new SimpleDateFormat((formatStr == null ? DEFAULT_FORMAT_DATE
                : formatStr))).parse(dateStr);
    }

    
    public static Date stringStamp2Date(String time) {
        Long longTime = Long.valueOf(time);
        Date date = new Date(longTime);
        return date;
    }

    
    public static boolean beginTimeisBeforeEndTime(String beginTime, String endTime) {
        long begin = dateTimeParse(beginTime, DEFAULT_FORMAT_DATE);
        long end = dateTimeParse(endTime, DEFAULT_FORMAT_DATE);
        return end > begin;
    }

    public static boolean beginTimeisBeforeEndTime(Date beginTime, Date endTime) {
        long begin = beginTime.getTime();
        long end = endTime.getTime();
        return end > begin;
    }

    
    public static boolean beginTimeBeforeEndTime(LocalDateTime beginTime, LocalDateTime endTime) {
        return beginTime.isBefore(endTime);
    }

    
    public static Date formatStringToDateAddDay(String dateStr, Integer daynum) throws ParseException {
        if (dateStr.length() <= SystemConstants.DATE_LENGTH) {
            dateStr = dateStr + " 00:00:00";
        } else {
            return (new SimpleDateFormat(DEFAULT_FORMAT_DATE)).parse(dateStr);
        }
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_FORMAT_DATE);
        Calendar calendar = new GregorianCalendar();
        Date date = sdf.parse(dateStr);
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, daynum);
        return calendar.getTime();
    }

    
    public static Date dateDelayDayOrMonthOrYear(Date dateStr, Integer num, Integer type) {
        Calendar calendar = new GregorianCalendar();
        Date date = dateStr;
        calendar.setTime(date);
        switch (type) {
            case 1:
                calendar.add(Calendar.DATE, num);
                break;
            case 2:
                calendar.add(Calendar.MONTH, num);
                break;
            case 3:
                calendar.add(Calendar.YEAR, num);
                break;
        }
        return calendar.getTime();
    }


    
    public static LocalDateTime strToLocalDateTime(String dateStr) {
        if (StringUtils.isBlank(dateStr)) {
            return LocalDateTime.now();
        }
        try {
            return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(DEFAULT_FORMAT_DATE));
        } catch (Exception ignored) {
        }
        return LocalDateTime.now();
    }

    
    public static String localDateTimeToStr(LocalDateTime time) {
        if (time == null) {
            return null;
        }
        try {
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            return df.format(time);
        } catch (Exception ignored) {

        }
        return null;
    }

    
    public static Date localDateTimeToDate(LocalDateTime time) {
        Date date = null;
        if (time != null) {
            String localTimeStr = localDateTimeToStr(time);
            try {
                date = formatStringToDate(localTimeStr, "yyyy-MM-dd HH:mm:ss");
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/307245.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号