java计算日期相差的天、时、分、秒

DateSpaceHelper工具类

为了便于使用封装成DateSpaceHelper工具类:

package com.date.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/** * * @author http://blog.csdn.net/u010989191 * */
public final class DateSpaceHelper {
    //定义好查询的类型type
    public static String SECOND = "second";
    public static String MINUTE = "minute";
    public static String HOUR = "hour";
    public static String DAY = "day";

    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//格式化日期 

    //禁止实例化
    private DateSpaceHelper(){}
    /** * 重新设置日期格式 * @param format */
    public static void setFormat(SimpleDateFormat format) {
        DateSpaceHelper.format = format;
    }
    /** * 获取相差的月份 * 秒:/1000 * 分:/(1000*60) * 时:/(1000*60*60) * 天:/(1000*60*60*24) * @param type 指定是查相差的秒 分 时 还是 天 * DateSpaceHelper.SECOND 查询相差的秒 * DateSpaceHelper.MINUTE 查询相差的分 * DateSpaceHelper.HOUR 查询相差的小时 * DateSpaceHelper.DAY 查询相差的天 * @param sourceDateStr 如"2016-06-01" * @param compareDateStr 如"2016-06-01" * @return */
    public static long getDateSpace(String type,String sourceDateStr,String compareDateStr)
    {
        Integer second = 1000;
        Integer minute = second*60;
        Integer hour = minute*60;
        Integer day = hour*24;
        try {
            Calendar sourceDate = Calendar.getInstance();
            Calendar compareDate = Calendar.getInstance();
            sourceDate.setTime(format.parse(sourceDateStr));
            compareDate.setTime(format.parse(compareDateStr));
            //获取每个日期的毫秒数
            Long sourceLong = sourceDate.getTimeInMillis();
            Long compareLong = compareDate.getTimeInMillis();
            long result = -1;
            switch (type) {
            case "second":
                result = Math.abs((sourceLong - compareLong)/second);
                break;
            case "minute":
                result = Math.abs((sourceLong - compareLong)/minute);
                break;
            case "hour":
                result = Math.abs((sourceLong - compareLong)/hour);
                break;
            case "day":
                result = Math.abs((sourceLong - compareLong)/day);
                break;
            }
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }
}

测试类

package com.date.test;

import java.text.ParseException;

public class DateTest {
    public static void main(String[] args) throws ParseException {
        //测试相差的月份
        System.out.println("2015-06-01 2014-06-01 相差的天数:"+DateSpaceHelper.getDateSpace(DateSpaceHelper.DAY,"2015-06-01", "2014-06-01"));
        System.out.println("2015-06-01 2014-06-01 相差的小时数:"+DateSpaceHelper.getDateSpace(DateSpaceHelper.HOUR,"2014-06-01", "2015-06-01"));
        System.out.println("2015-06-01 2014-06-01 相差的分钟数:"+DateSpaceHelper.getDateSpace(DateSpaceHelper.MINUTE,"2014-06-01", "2015-06-01"));
        System.out.println("2015-06-01 2014-06-01 相差的秒数:"+DateSpaceHelper.getDateSpace(DateSpaceHelper.SECOND,"2014-06-01", "2015-06-01"));
    }

}

测试结果

这里写图片描述

转载于:https://my.oschina.net/u/3702584/blog/1547138

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值