获取两日期相差年月日周时分秒(笔记)

    public static void main(String[] args) {
        int sYear = 2016;

        //月份是0-11,换算时+1
        int sMonth = 9;

        int sDay = 10;

        int eYear = 2017;

        int eMonth = 5;

        int eDay = 15;

        Calendar sDate = Calendar.getInstance();
        sDate.set(sYear, sMonth, sDay);
        Calendar eDate = Calendar.getInstance();
        eDate.set(eYear, eMonth, eDay);
        
        /*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String sDateStr = "2016-09-10";
        String eDateStr = "2017-05-15";
        
        Calendar sDate = Calendar.getInstance();
        Calendar eDate = Calendar.getInstance();
        try {
            sDate.setTime(format.parse(sDateStr));
            eDate.setTime(format.parse(eDateStr));
        } catch (ParseException e1) {
            e1.printStackTrace();
        }*/
        
        //一天的毫秒数
        long dayMill = 1000*60*60*24;
        //一小时的毫秒数
        long hourMill = 1000*60*60;
        //一分钟的毫秒数
        long minMill = 1000*60;
        
        try {
            System.out.println("相差年:"+differYear(sDate, eDate));
            System.out.println("相差月:"+differMonth(sDate, eDate));
            System.out.println("相差日:"+differDay(sDate, eDate, dayMill));
            System.out.println("相差周:"+differDay(sDate, eDate, dayMill)/7);
            System.out.println("相差小时:"+differDay(sDate, eDate, hourMill));
            System.out.println("相差分钟:"+differDay(sDate, eDate, minMill));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取两日期相差年数
     * @param sDate 开始日期
     * @param eDate 结束日期
     * @return
     * @throws Exception
     */
    public static int differYear(Calendar sDate,Calendar eDate) throws Exception{
        try {
            return eDate.get(Calendar.YEAR) - sDate.get(Calendar.YEAR);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    
    /**
     * 获取两日期相差月数
     * @param sDate 开始日期
     * @param eDate 结束日期
     * @return
     * @throws Exception
     */
    public static int differMonth(Calendar sDate,Calendar eDate) throws Exception{
        try {
            int eMonth = eDate.get(Calendar.YEAR)*12+eDate.get(Calendar.MONTH);
            int sMonth = sDate.get(Calendar.YEAR)*12+sDate.get(Calendar.MONTH);
            return eMonth - sMonth;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    
    /**
     * 获取两日期相差天数、小时、分钟、秒(将毫秒值相减并换算)
     * @param sDate 开始日期
     * @param eDate 结束日期
     * @param differMill 毫秒换算值
     * @return
     * @throws Exception
     */
    public static int differDay(Calendar sDate,Calendar eDate,Long differMill) throws Exception{
        try {
            int differDay = (int)((eDate.getTimeInMillis()-sDate.getTimeInMillis())/differMill);
            return differDay;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }empty
public static void main(String[] args) {
        int sYear = 2016;

        //月份是0-11,换算时+1
        int sMonth = 9;

        int sDay = 10;

        int eYear = 2017;

        int eMonth = 5;

        int eDay = 15;

        Calendar sDate = Calendar.getInstance();
        sDate.set(sYear, sMonth, sDay);
        Calendar eDate = Calendar.getInstance();
        eDate.set(eYear, eMonth, eDay);
        
        /*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String sDateStr = "2016-09-10";
        String eDateStr = "2017-05-15";
        
        Calendar sDate = Calendar.getInstance();
        Calendar eDate = Calendar.getInstance();
        try {
            sDate.setTime(format.parse(sDateStr));
            eDate.setTime(format.parse(eDateStr));
        } catch (ParseException e1) {
            e1.printStackTrace();
        }*/
        
        //一天的毫秒数
        long dayMill = 1000*60*60*24;
        //一小时的毫秒数
        long hourMill = 1000*60*60;
        //一分钟的毫秒数
        long minMill = 1000*60;
        
        try {
            System.out.println("相差年:"+differYear(sDate, eDate));
            System.out.println("相差月:"+differMonth(sDate, eDate));
            System.out.println("相差日:"+differDay(sDate, eDate, dayMill));
            System.out.println("相差周:"+differDay(sDate, eDate, dayMill)/7);
            System.out.println("相差小时:"+differDay(sDate, eDate, hourMill));
            System.out.println("相差分钟:"+differDay(sDate, eDate, minMill));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


/**
     * 获取两日期相差年数
     * @param sDate 开始日期
     * @param eDate 结束日期
     * @return
     * @throws Exception
     */
    public static int differYear(Calendar sDate,Calendar eDate) throws Exception{
        try {
            return eDate.get(Calendar.YEAR) - sDate.get(Calendar.YEAR);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    
    /**
     * 获取两日期相差月数
     * @param sDate 开始日期
     * @param eDate 结束日期
     * @return
     * @throws Exception
     */
    public static int differMonth(Calendar sDate,Calendar eDate) throws Exception{
        try {
            int eMonth = eDate.get(Calendar.YEAR)*12+eDate.get(Calendar.MONTH);
            int sMonth = sDate.get(Calendar.YEAR)*12+sDate.get(Calendar.MONTH);
            return eMonth - sMonth;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    
    /**
     * 获取两日期相差天数、小时、分钟、秒(将毫秒值相减并换算)
     * @param sDate 开始日期
     * @param eDate 结束日期
     * @param differMill 毫秒换算值
     * @return
     * @throws Exception
     */
    public static int differDay(Calendar sDate,Calendar eDate,Long differMill) throws Exception{
        try {
            int differDay = (int)((eDate.getTimeInMillis()-sDate.getTimeInMillis())/differMill);
            return differDay;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值