jsp代码:
String activationkey=request.getParameter("activationkey");
System.out.println("key is=="+activationkey);
CustinfoDao custinfoDao = new CustinfoDaoImpl();
CustomerInfo cust=custinfoDao.getDateByActivationKey(activationkey);
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
//get current date time with Date()
Date date = new Date();
Date d1=new Date();
System.out.println("d1================="+d1.getTime());
Date d2= cust.getCreatedDate();
System.out.println("d2================="+d2.getTime());
long d3=d1.getTime() - d2.getTime();
System.out.println("d3 is =="+d3);
long diffHours = TimeUnit.MILLISECONDS.toHours(d3);
System.out.println("diff in hours=="+diffHours);
System.out.println("diff in hours=="+(diffHours<48));
if(diffHours < 48){
%>
<script type="text/javascript">
window.open("http://bdisys.com/Download/BizViz/BizVizPackage.zip");
</script>
<%
}
else{
%>
<div>Your Link has been Expired....</div>
<% }
%>输出:
key is==L2NWL49fx5
d1=================1399901683076
d2=================1399833000000
d3 is ==68683076
diff in hours==19
diff in hours==true我想要得到的时间之间的差异,在当前日期和日期,用户已通过表单注册,并希望过期的链接,如果差异是大于48小时,否则想执行其他块的code.but,我不能得到确切的时间difference.Any帮助将被取代。
发布于 2014-05-12 22:01:34
我不确定我是否完全理解你的问题,但在我看来,d3有确切的时差。所以我想你的问题是当你
long diffHours = TimeUnit.MILLISECONDS.toHours(d3);
你没有得到足够的精确度。
如果是这样的话,你可以这样做
用于测试d3是否小于48小时(毫秒)的if (d3 < 48*3600*1000)。
希望我理解你的问题:)
发布于 2014-05-12 23:16:07
new Date()将返回当前时间,包括小时、分钟、秒、毫秒。
custinfoDao.getDateByActivationKey(activationkey)似乎返回添加记录时的时间。
这些时间肯定会有所不同,因为您不仅要比较日期(YYYY/MM/DD),还要比较时间(YYYY/MM/DD HH:mm:ss)。
https://stackoverflow.com/questions/23610680
复制相似问题