获取日期段天数

方法1:相关代码如下:

private static DateFormat norDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);

public static String normalizeDate2String(Date date) {
		return norDateFormat.format(date);
	}
	
	/**
	 * 
	 * @param preOrNextData 1表示后一天,0表示当天,-1表示前一天
	 * @return
	 */
	public static String getpreOrNextDate(Date date, int preOrNextDate){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE,preOrNextDate);
		Date dd = cal.getTime();
		return norDateFormat.format(dd);
	}

	public static Date normalizeString2Date(String date) throws ParseException {
		try {
			return norDateFormat.parse(date);
		} catch (ParseException x) {
			return norDateFormatOld.parse(date);
		}
	}


实现代码:

/**
	 * 获取时间段天数
	 */
	private int checkDate(String startDate, String endDate){
		int dateCount = 0;
		try {
			Date date = Formats.normalizeString2Date(startDate);
			while(!startDate.equalsIgnoreCase(endDate)){
				dateCount++;
				startDate = Formats.getpreOrNextDate(date, dateCount);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateCount;
	}

方法2:

/** 
     * 获取两个日期之间的间隔天数 
     * @return 
     */  
    public static int getGapCount(Date startDate, Date endDate) {  
           Calendar fromCalendar = Calendar.getInstance();    
           fromCalendar.setTime(startDate);    
           fromCalendar.set(Calendar.HOUR_OF_DAY, 0);    
           fromCalendar.set(Calendar.MINUTE, 0);    
           fromCalendar.set(Calendar.SECOND, 0);    
           fromCalendar.set(Calendar.MILLISECOND, 0);    
       
           Calendar toCalendar = Calendar.getInstance();    
           toCalendar.setTime(endDate);    
           toCalendar.set(Calendar.HOUR_OF_DAY, 0);    
           toCalendar.set(Calendar.MINUTE, 0);    
           toCalendar.set(Calendar.SECOND, 0);    
           toCalendar.set(Calendar.MILLISECOND, 0);    
       
           return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));  
    }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值