时间处理小工具集合

时间各种处理工具集合实现:

先自定义一些时间格式样式

 public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYYMMDDHHMM = "yyyyMMddHHmm";
    public static final String YYYYMMDDHH_MM = "yyyyMMddHH:mm";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYYMM = "yyyyMM";
    public static final String HHMMSS = "HHmmss";
    public static final String HH_MM = "HH:mm";
    public static final String YYYY = "yyyy";
    public static final Long DAY = 24 * 3600000L;
    public static final Long HOURS = 3600000L;
    public static final Long MINUTE = 60000L;
    public static final String HHMM = "HHmm";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String YYYY年MM月DD日 = "yyyy年MM月dd日";

格式化时间String为Date 类型

public static Date formatTimeToDate(String date, String patten) {
        if (patten == null) {
            patten = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(patten);
        try {
            if (date == null) {
                return null;
            } else {
                return sdf.parse(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

格式化时间Date为String 类型

public static String formatDateToString(Date date, String patten) {
        if (patten == null) {
            patten = "yyyy-MM-dd HH:mm:ss";
        }
        if (date == null) {
            date = Calendar.getInstance().getTime();
        }
        SimpleDateFormat sdf = new SimpleDateFormat(patten);
        return sdf.format(date);
    }

在某个时间点上,想加n小时后的时间:

/**
*date 时间
*hours 相加几小时
*/
public static Date dateAddHours(Date date, Integer hours) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) + hours);
        return cal.getTime();
    }

13位时间戳转换

public static String timeStamp2Date(String time) {
        Long timeLong = Long.parseLong(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
        Date date;
        try {
            date = sdf.parse(sdf.format(timeLong));
            return sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

获取两者时间相隔多少天,两个方法不同实现

 public static Integer getdaysFromTowDate(Date begin, Date end) {
        begin = formatTimeToDate(formatDateToString(begin, "yyyyMMdd"), "yyyyMMdd");
        end = formatTimeToDate(formatDateToString(end, "yyyyMMdd"), "yyyyMMdd");
        return Integer.parseInt((end.getTime() - begin.getTime()) / (3600000 * 24) + "") + 1;

    }


public static Integer daysBetween(Date smdate, Date bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
        } catch (ParseException e) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (24000 * 3600);
        return Integer.parseInt(String.valueOf(between_days));
    }

两个时间对比

 /**
     * 日期对比
     * @param date1 开始时间
     * @param date2 结束时间
     * @return 当, 开始时间 > 结束时间 = false ,反之为true
     */
    public static Boolean compareDateByGetTime(Date date1, Date date2) {
        if (date1.getTime() < date2.getTime()) {
            return true;
        } else if (date1.getTime() > date2.getTime()) {
            return false;
        }
        return null;
    }
功能:得到当月份第n个月初时间
public static Date thisMonth(Date validatetime, int day) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = null;
        try {
            now = sdf.parse(sdf.format(validatetime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.MONTH, day);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }
判断两个时间,是否在同一个月内
public static boolean isBeforeMonth(Date beforeMonth, Date currentMonth) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        long bigtime = 0;
        long endtime = 0;
        try {
            endtime = sdf.parse(sdf.format(beforeMonth)).getTime();
            bigtime = sdf.parse(sdf.format(currentMonth)).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (bigtime == endtime) {
            return true;
        } else {
            return false;
        }
    }

在当前时间向后相加天数

 public static Date getPlusDay(Date toDay, Long agoX) {
        return new Date(toDay.getTime() + (agoX * 86400000L));
    }

 /**
     * <p>Description: 把指定日期相加指定的天数</p>
     *
     * @param date 时间
     * @param day  相加的天数
     * @return
     * @date 2019年3月4日
     * @version 1.1.2
     */
    public static Date dateAddDay(Date date, Integer day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + day);
        return cal.getTime();
    }
得到当前时间月份的第一天
 /**
     * <p>Description: 得到当前时间月份的第一天</p>
     *
     * @param date
     * @return
     * @date 2019年3月4日
     * @version 1.1.2
     */
    public static Date getFirstDaysByDate(Date date) {
        Calendar a = Calendar.getInstance();
        a.setTime(date);
        a.set(Calendar.DAY_OF_MONTH, 1);
        return a.getTime();
    }
把指定日期相加指定的月份
/**
     * <p>Description: 把指定日期相加指定的月份</p>
     *
     * @param date  时间
     * @param month 相加的月份
     * @return
     * @date 2019年3月4日
     * @version 1.1.2
     */
    public static Date dateAddMonth(Date date, Integer month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + month);
        return cal.getTime();
    }
获取今年的二月份
  /**
     * 获取今年的二月份
     * @return date
     */
    public static Date getFebOfThisYear(){
        Calendar calendar = Calendar.getInstance();
        //如果当前月份小于二月,取去年的年份
        if (calendar.get(Calendar.MONTH) < 1){
            calendar.set(Calendar.YEAR,calendar.get(Calendar.YEAR) - 1);
        }
        //calendar month 从0开始
        calendar.set(Calendar.MONTH,1);
        return calendar.getTime();
    }
根据年月,得到该月的天数
/**
     * <p>Description: 根据年月,得到该月的天数</p>
     *
     * @param year
     * @param month
     * @return
     * @date 2019年3月4日
     * @version 1.1.2
     */
    public static Integer getDaysByYearMonth(Integer year, Integer month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }
得到当前时间月份的第一天
  /**
     * <p>Description: 得到当前时间月份的第一天</p>
     *
     * @param date
     * @return
     * @date 2019年3月4日
     * @version 1.1.2
     */
    public static Date getFirstDaysByDate(Date date) {
        Calendar a = Calendar.getInstance();
        a.setTime(date);
        a.set(Calendar.DAY_OF_MONTH, 1);
        return a.getTime();
    }
得到当前时间月份的最后一天
/**
     * <p>Description: 得到当前时间月份的最后一天</p>
     *
     * @param date
     * @return
     * @date 2019年3月4日
     * @version 1.1.2
     */
    public static Date getLastDaysByDate(Date date) {
        Calendar a = Calendar.getInstance();
        a.setTime(date);
        a.add(Calendar.MONTH, 1);
        a.set(Calendar.DAY_OF_MONTH, 0);
        return a.getTime();
    }
判断两个时间段,是否存在交集和包含
/**
     * 判断两个时间段,是否存在交集和包含
     *
     * @param leftStartDate
     * @param leftEndDate
     * @param rightStartDate
     * @param rightEndDate
     * @return 存在交集 false ,不存在交集 true
     */
    public static Boolean getDatePeriod(Date leftStartDate, Date leftEndDate, Date rightStartDate, Date rightEndDate) {
        if (((leftStartDate.getTime() >= rightStartDate.getTime())
                && leftStartDate.getTime() < rightEndDate.getTime())
                || ((leftStartDate.getTime() > rightStartDate.getTime())
                && leftStartDate.getTime() <= rightEndDate.getTime())
                || ((rightStartDate.getTime() >= leftStartDate.getTime())
                && rightStartDate.getTime() < leftEndDate.getTime())
                || ((rightStartDate.getTime() > leftStartDate.getTime())
                && rightStartDate.getTime() <= leftEndDate.getTime())) {
            return false;
        } else {
            return true;
        }
    }
获取两个日期相隔的自然月数
 /**
     * <p>Description: 获取两个日期相隔的自然月数</p>
     *
     * @param begin 开始日期
     * @param end   结束日期
     * @return
     * @date 2019年3月28日
     * @version 1.1.2
     */
    public static Integer getMouthFromTowDate(Date begin, Date end) {
        Integer obj = null;
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(begin);
        c2.setTime(end);
        if (formatDateToString(begin, YYYY).equals(formatDateToString(end, YYYY))) {
            obj = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH) + 1;
        } else {
            obj = 13 - c1.get(Calendar.MONTH) + c1.get(Calendar.MONTH);
            Integer year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
            if (year > 1) {
                obj = obj + 12 * (year - 1);
            }
        }
        return obj;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值