java中与时间有关的一些常用方法

/**
	 * 获取两个日期之间相差的月数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long monthsBetween(Date date1, Date date2) {
		Calendar cal1 = new GregorianCalendar();
		cal1.setTime(date1);
		Calendar cal2 = new GregorianCalendar();
		cal2.setTime(date2);
		long c = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12 + cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
		return c;
	}
	
	/**
	 * 获取两个日期之间相差的天数
	 * @param begin
	 * @param end
	 * @return
	 */
	public static long daysBetween(long begin,long end){
		long btn = end - begin;
		long day = btn/(24*60*60*1000);
		return day;
	}
	
	/**
	 * 获取两个日期之间相差的小时数
	 * @param begin
	 * @param end
	 * @return
	 */
	public static long hoursBetween(long begin,long end){
		long btn = end - begin;
		long day = btn/(24*60*60*1000);
		long hour = (btn/(60*60*1000)-day*24);
		return hour;
	}
	
	/**
     * 根据日期获得所在周的日期 
     * @param mdate
     * @return
     */
    @SuppressWarnings("deprecation")
    public static List<Date> dateToWeek(Date mdate) {
        int b = mdate.getDay();
        Date fdate;
        List<Date> list = new ArrayList<Date>();
        Long fTime = mdate.getTime() - b * 24 * 3600000;
        for (int a = 1; a <= 7; a++) {
            fdate = new Date();
            fdate.setTime(fTime + (a * 24 * 3600000));
            list.add(a-1, fdate);
        }
        return list;
    }
    
    /**
	 * 取得当前系统时间,格式为yyyyMMddHHmm
	 * 
	 * @return 当前系统时间,格式为yyyyMMddHHmm
	 */
	public static String getDate12() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
		return formatter.format(new Date());
	}
	
	/**  
     * 得到指定时间的几分钟前的时间 
     * @param d 指定时间
     * @param minute 分钟数
     * @return
     */  
    public static Date getMinuteBefore(Date d, int minute) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) - minute);
        return now.getTime();
    }
    
    /**  
     * 得到指定时间的几个小时后的时间 
     * @param d 指定时间
     * @param hour 小时
     * @return
     */  
    public static Date getHourAfter(Date d, int hour) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.HOUR, now.get(Calendar.HOUR) + hour);
        return now.getTime();
    }
    
     
    /**  
     * 得到指定时间的几个小时前的时间 
     * @param d 指定时间
     * @param hour 小时
     * @return
     */  
    public static Date getHourBefore(Date d, int hour) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.HOUR, now.get(Calendar.HOUR) - hour);
        return now.getTime();
    }
    
    /**  
     * 得到指定时间的几天前的时间 
     * @param d 指定时间
     * @param hour 小时
     * @return
     */  
    public static Date getDayBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH) - day);
        return now.getTime();
    }
    
    /**  
     * 得到指定时间的几月前的时间 
     * @param d 指定时间
     * @param hour 小时
     * @return
     */  
    public static Date getMonthBefore(Date d, int month) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.MONTH, now.get(Calendar.MONTH) - month);
        return now.getTime();
    }
    
    /**  
     * 得到指定时间的几年前的时间 
     * @param d 指定时间
     * @param hour 小时
     * @return
     */  
    public static Date getYearBefore(Date d, int year) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.YEAR, now.get(Calendar.YEAR) - year);
        return now.getTime();
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值