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;

参考代码:[url]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[/url]

后来发现不对,要求是同一天,而不是相差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;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值