美文网首页
使用总结之Java项目中常用的日期和时间工具类

使用总结之Java项目中常用的日期和时间工具类

作者: 小马将过河 | 来源:发表于2021-04-17 22:39 被阅读0次

    备份2018年海尔创客汇项目上用到的一些日期和时间处理的工具函数,以备不时之需。

    直接贴代码

    package com.hczt.haier.common.utils;
    
    import org.apache.commons.lang3.StringUtils;
    import org.apache.commons.lang3.time.DateUtils;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * 精诚所至,金石为开。
     * 石の上にも三年;陽気の発する所金石亦透る。
     *
     * @Author mahaiqiang
     * @Create 2021/4/17 10:29 下午
     * @Desc TODO
     **/
    public class DateUtil extends DateUtils {
        /**
         * yyyy-MM-dd
         */
        public final static String DATE_FORMAT    = "yyyy-MM-dd";
        /**
         * yyyy年MM月dd日
         */
        public final static String DATE_FORMAT_CN = "yyyy年MM月dd日";
        /**
         * yyyy-MM-dd HH:mm:ss
         */
        public final static String TIME_FORMAT    = "yyyy-MM-dd HH:mm:ss";
    
        /**
         * yyyy-MM-dd HH:mm
         */
        public final static String TIME_FORMAT_MINUTE = "yyyy-MM-dd HH:mm";
    
        /**
         * yyyy年MM月dd日 HH:mm:ss
         */
        public final static String TIME_FORMAT_CN        = "yyyy年MM月dd日 HH:mm:ss";
        /**
         * yyyy-MM
         */
        public final static String MONTH_FORMAT          = "yyyy-MM";
        /**
         * yyyyMMdd
         */
        public final static String DAY_FORMAT            = "yyyyMMdd";
        /**
         * yyyyMMddHHmmss
         */
        public final static String TIME_FORMAT_FOR_NAME  = "yyyyMMddHHmmss";
        /**
         * yyMMddHHmmss
         */
        public final static String TIME_FORMAT_FOR_SHORT = "yyMMddHHmmss";
    
        /**
         * yyyyMMddHHmmss
         */
        public final static String TIME_FORMAT_TIME = "yyyyMMddHHmmss";
    
        public static final long millisOfDay = 86400000L;
    
        public static Date formateData(Date date, String format) throws ParseException {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date2str(date, format));
        }
    
        public static Date str2date(String dateStr, String format) throws ParseException {
            if (StringUtils.isEmpty(dateStr)) {
                return null;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(dateStr);
        }
    
        public static String date2str(Date date, String format) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }
    
        /**
         * date format yyyy-MM-dd
         *
         * @param date
         * @return
         */
        public static String dateToSimpleDateFormat(Date date) {
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
            return sdf.format(date);
        }
    
        public static Date addDate(Date date, int addDay) {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_YEAR, addDay);
            return c.getTime();
        }
        
        public static Date getOffsetDate(Date now, int field, int offset) {
            
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(now);
            
            calendar.add(field, offset);
            
            return calendar.getTime();
        }
    
        public static Date dayStartTime(Date date) throws ParseException {
            String dateStr = date2str(date, DATE_FORMAT);
            return str2date(dateStr, DATE_FORMAT);
        }
    
    
        /**
         * 获取某一天的最后一秒
         *
         * @param date
         * @return
         * @throws Exception
         */
        public static Date dayEndTime(Date date) throws ParseException {
            String dateStr = date2str(date, DATE_FORMAT);
            return str2date(dateStr + " 23:59:59", TIME_FORMAT);
        }
    
        /**
         * start
         * 本周开始时间戳 - 以星期一为本周的第一天
         */
        public static Date getThisMonday(Date date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (day_of_week == 0) {
                day_of_week = 7;
            }
            cal.add(Calendar.DATE, -day_of_week + 1);
            return cal.getTime();
        }
    
        /**
         * 获取某时间段内所有的日期
         *
         * @return
         */
        public static List<Date> findDatesBetweenTwoDate(Date beginDate, Date endDate) throws ParseException {
            List<Date> dates = new LinkedList<>();
            if (!beginDate.after(endDate)) {
                beginDate = DateUtil.formateData(beginDate, DATE_FORMAT);
                endDate = DateUtil.formateData(endDate, DATE_FORMAT);
                dates.add(beginDate);
                Calendar calBegin = Calendar.getInstance();
                calBegin.setTime(beginDate);
                Calendar calEnd = Calendar.getInstance();
                calEnd.setTime(endDate);
                while (endDate.after(calBegin.getTime())) {
                    calBegin.add(Calendar.DAY_OF_MONTH, 1);
                    dates.add(calBegin.getTime());
                }
            }
            return dates;
        }
    
    
        /**
         * 获取某日期的前后的日期
         *
         * @param date         指定的日期
         * @param interval     间隔的时间 正为之后的日期,负为之前的日期
         * @param calendarUnit 间隔单位
         * @return
         */
        public static Date intervalOfDate(Date date, Integer interval, Integer calendarUnit) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(calendarUnit, calendar.get(calendarUnit) + interval);
            return calendar.getTime();
        }
    
    
        /**
         * 判断时间是不是今天
         * @param date
         * @return    是返回true,不是返回false
         */
        public static boolean isToday(Date date) {
            //当前时间
            Date now = new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
            //获取今天的日期
            String nowDay = sf.format(now);
    
            //对比的时间
            String day = sf.format(date);
            return day.equals(nowDay);
        }
    
        public static String formatDateTime(String startTime, String endTime) {
            String startDate = startTime.substring(0, 10);
            String endDate = endTime.substring(0, 10);
            String beginTime = startTime.substring(11, 19);
            String overTime = endTime.substring(11, 19);
            if (startDate.equals(endDate)) {
                return startDate + " " + beginTime + " ~ " + overTime;
            } else {
                return startDate + " " + beginTime + " ~ " + endDate + " " + overTime;
            }
        }
    
        public static void main(String[] args) {
            Date date = DateUtil.intervalOfDate(new Date(), -30, Calendar.MINUTE);
            System.out.println(DateUtil.date2str(date, DateUtil.TIME_FORMAT));
        }
    
        /**
         * 获取每天的开始时间 00:00:00:00
         *
         * @param date
         * @return
         */
        public static Date getStartTime(Date date) {
            Calendar dateStart = Calendar.getInstance();
            dateStart.setTime(date);
            dateStart.set(Calendar.HOUR_OF_DAY, 0);
            dateStart.set(Calendar.MINUTE, 0);
            dateStart.set(Calendar.SECOND, 0);
            return dateStart.getTime();
        }
    
        /**
         * 获取每天的开始时间 23:59:59:999
         *
         * @param date
         * @return
         */
        public static Date getEndTime(Date date) {
            Calendar dateEnd = Calendar.getInstance();
            dateEnd.setTime(date);
            dateEnd.set(Calendar.HOUR_OF_DAY, 23);
            dateEnd.set(Calendar.MINUTE, 59);
            dateEnd.set(Calendar.SECOND, 59);
            return dateEnd.getTime();
        }
    
        /**
         * 获取两个日期相隔天数
         *
         */
        public static Long daysBetweenDates(Date date1, Date date2) {
            long tmp1 = date1.getTime();
            long tmp2 = date2.getTime();
            return (tmp2 - tmp1) / millisOfDay;
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:使用总结之Java项目中常用的日期和时间工具类

          本文链接:https://www.haomeiwen.com/subject/jkoplltx.html