日期计算工具类

package com.jp.tech.artisan_cms.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@SuppressWarnings("ALL")
public class DateUtil {
    public static final String DATE_FORMAT_1 = "yy-MM-dd";
    public static final String DATE_FORMAT_2 = "yyyy-MM-dd";
    public static final String DATE_FORMAT_3 = "yyMMddHHmmss";
    public static final String DATE_FORMAT_4 = "yy/MM/dd";
    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";

    /**
     * 计算两个时间多少分钟
     *
     * @param endDate
     * @param nowDate
     * @return
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        long sec = diff % nd % nh % nm / ns; //秒
        long between = diff / 1000;//除以1000是为了转换成秒
        long min = between / 60;
        return String.valueOf(min + "." + sec);
    }

    /**
     * @Describe 日期加减
     * @Param Calendar.MONTH YERR DATE etc
     * @Auther xingyu.lu
     * @Date 18/3/29 09:18
     */
    public static Date dateAdd(Date date, int field, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(field, amount);
        return cal.getTime();
    }

    /**
     * @Describe 当前日期字符串
     * @Auther xingyu.lu
     * @Date 18/3/29 09:18
     */
    public static String getNowDateStr(String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(new Date());
    }

    /**
     * @Describe Date 转 String
     * @Auther xingyu.lu
     * @Date 18/3/29 09:17
     */
    public static String formatDateToStr(Date date, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(date);
    }

    /**
     * @Describe String 转 Date
     * @Auther xingyu.lu
     * @Date 18/4/8 16:27
     */
    public static Date parseStrToDate(String date, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date dt = null;
        try {
            dt = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * @Describe 获取两个日期期间的所有天数(包含开始结束)
     * @Param Date begin
     * @Param Date end
     * @Return List<String> 日期
     * @Auther xingyu.lu
     * @Date 18/3/28 19:56
     */
    public static List<String> getBetweenDates(Date begin, Date end) {
        List<String> result = new ArrayList<String>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(begin);
        while (begin.getTime() <= end.getTime()) {
            result.add(formatDateToStr(tempStart.getTime(), DATE_FORMAT_2));
            tempStart.add(Calendar.DATE, 1);
            begin = tempStart.getTime();
        }
        return result;
    }

    
    /**
     * @Describe 获取两日期相差天数
     * @Param start - end
     * @Return
     * @Auther xingyu.lu
     * @Date 18/3/29 18:16
     */
    public static int dateDiffer(Date start, Date end) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        Long diff = start.getTime() - end.getTime();
        // 计算差多少天
        Long day = diff / nd;
        // 计算差多少小时
//        long hour = diff % nd / nh;
        // 计算差多少分钟
//        long min = diff % nd % nh / nm;
        // 计算差多少秒
        // long sec = diff % nd % nh % nm / ns;
        return day.intValue();
    }


  
    /**
     * 计算相差多少天,多少小时,多少分钟,多少秒
     *
     * @param endDate
     * @param nowDate
     * @return
     */
    public static String getDateSec(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
/*        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;*/
        // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
        return String.valueOf(sec);
//        return day + "天" + hour + "小时" + min + "分钟" +sec + "秒";
    }

    public static void main(String[] args) {

    /*    System.out.println(
                getDateCycle(parseStrToDate("2018-04-22", DateUtil.DATE_FORMAT_2),
                        parseStrToDate("2018-04-30", DateUtil.DATE_FORMAT_2),
                        1, 1, 1, 2));

        System.out.println(getRoundDate(parseStrToDate("2018-04-22", DateUtil.DATE_FORMAT_2),
                parseStrToDate("2018-04-30", DateUtil.DATE_FORMAT_2), true,
                1, 1, 1, 2, 1));*/

        System.out.println(getDatePoor(new Date(),
                parseStrToDate("2018-08-17 14:21:12", yyyyMMddHHmmss)));
        System.out.println(getDateSec(new Date(),
                parseStrToDate("2018-08-17 14:21:12", yyyyMMddHHmmss)));

    }

}

猜你喜欢

转载自blog.csdn.net/weixin_34503526/article/details/81811479