Date工具类-往后/往前移指定天数/判断是否是周末

1、计算两个Date日期相差多少天、时、分、秒、毫秒等

public static int getDiffDays(Date start, Date end) {
		long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff = end.getTime() - start.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    // 计算差多少分钟
	    long min = diff % nd % nh / nm;
	    // 计算差多少秒//输出结果
        long sec = diff % nd % nh % nm / ns;
	    return Integer.valueOf(String.valueOf(day));
	}

2、日期Date往后推或往前移指定天数

public static Date getNewDate(Date startDate, int i) {
		// TODO Auto-generated method stub
		Calendar calendar = new GregorianCalendar(); 
		calendar.setTime(startDate); 
		calendar.add(calendar.DATE, i); //把日期往后增加一天,整数  往后推,负数往前移动 
		return calendar.getTime();
	}

3、判断Date是否是周六或周天

public static boolean isWeekend(Date newDate) {
		// TODO Auto-generated method stub
		boolean result = false;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(newDate);
		int week_index = calendar.get(Calendar.DAY_OF_WEEK);
		if(week_index==1 || week_index == 7){
            return true;
        }
		return result;
	}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值