`

Date相关功能实现

阅读更多
一、同一天

需求:用户记录跟踪列表展示,要求用户数据同一天内的修改覆盖更新,即每天只保留一条数据

方法:需要将数据表中存放的数据中的操作时间与当前时间进行比较,确认是否是同一天

解决:
1.时间相差24小时
Date date1 = new Date();
Date date2 = new Date();

long  between = date2.getTime() - date1.getTime();

if(between > (24* 3600000)){
    return true;

}
return false;

参考代码:http://zhidao.baidu.com/question/1574925715843819380.html?fr=iks&word=java%D6%D0Date%CA%FD%BE%DD%C0%E0%D0%CD%B1%C8%BD%CF&ie=gbk

后来发现不对,要求是同一天,而不是相差24小时啊

2.将时间格式化,比较字符串
	public boolean isInTheSameDay(String merchantId){
		Date date = new Date();
		Date operateTime = selectScheduleTracking(merchantId).getOperateTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(sdf.format(date).equals(sdf.format(operateTime))){
			return true ;
		}
		return false;
	}


二、倒计时

系统当前时间距离明天0时0分0秒剩余多长时间

	public void countDownTime(){
		
		Date currentTime = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(currentTime);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		long ns = 1000 ;
		Long timeInterval = calendar.getTime().getTime() - currentTime.getTime();
		// 计算差多少小时
	    Long hour = timeInterval % nd / nh;
	    // 计算差多少分钟
	    Long min = timeInterval % nd % nh / nm;
	    // 计算差多少秒
	    Long sec = timeInterval % nd % nh % nm / ns;

	}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics