根据指定日期 获取N天时间

/**
	 * 获取给定日期N天后的日期
	 * @author chenchbj
	 * 根据当前时间,添加或减去指定的时间量。例如,要从当前日历时间减去 5 天,可以通过调用以下方法做到这一点:
	 * add(Calendar.DAY_OF_MONTH, -5)。
	 * @param time 指定时间
	 * @param num  为时间添加或减去的时间天数
	 * @return
	 */
	public static String getBeforeOrAfterDate(String time, int num) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date  =Util.strToDate(time,null);
		Calendar calendar = Calendar.getInstance();//获取日历
		calendar.setTime(date);//当date的值是当前时间,则可以不用写这段代码。
		calendar.add(Calendar.DATE, num);
		Date d = calendar.getTime();//把日历转换为Date
		return formatter.format(d);
	}
/**
	 * String转Date
	 * @param date
	 * @param formate
	 * @return
	 */
	public static Date strToDate(String date,String formate){
		if(Util.isEmpty(formate)){
			formate="yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(formate);
		Date rs;
		try {
			rs = formatter.parse(date);
		} catch (ParseException e) {
			rs = null;
			e.printStackTrace();
		}
		return rs;
	}
/**
	 *
	 * 校验字符串是否为空
	 * 判断是否为空
	 * */
	public static boolean isEmpty(Object obj)  
    {  
        if (obj == null)  
        {  
            return true;  
        }  
        if ((obj instanceof List))  
        {  
            return ((List) obj).size() == 0;  
        }  
        if ((obj instanceof String))  
        {  
            return ("").equals(((String) obj).trim());
        }  
        return false;  
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperChen12356

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值