import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeMinus {
public String operateTime(String date,String etime)
{
try
{
String[] str=etime.split(":");
String s=str[0];
Date today=new Date();
SimpleDateFormat df=new SimpleDateFormat("yy-mm-dd");
Date d=df.parse(date);
long diff=d.getTime()-today.getTime();
String day1=String.valueOf(diff/(24*60*60*1000));
int day=Integer.parseInt(day1);
String hour1=String.valueOf((diff/(60*60*1000)-Long.parseLong(day1)*24));
int h=Integer.parseInt(hour1)+Integer.parseInt(s);
if(h>24)
{
h-=24;
day+=1;
}
String hour=String.valueOf(h);
return day+"天"+hour+"小时";
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public boolean isLater(Date d)
{
Date today =new Date();
long diff=today.getTime()-d.getTime();
if(diff<300000)
return true;
else return false;
}
}
body中:
<%
com.lhkj.dao.TimeMinus tm=new com.lhkj.dao.TimeMinus();
String star=ld.getLawDocuments().getPublicDateStar().toString().substring(0,19);
if(tm.isLater(ld.getLawDocuments().getPublicDateStar()))
{
%>
<font color="red"><%out.print(star);}else{%></font>
<font color="black"><%out.print(star);}%></font>
</td>
<td width="10%" align="left" style="padding-left:15px" class="line">
<%
String endTime=ld.getLawDocuments().getEndTime();
String eTime=ld.getLawDocuments().getPublicDateEnd().toString().substring(0,11);
String str=tm.operateTime(eTime,endTime);
String[] day=str.split("天");
int index=day[1].lastIndexOf("小时");
int hour=Integer.parseInt(day[1].substring(0,index));
if(Integer.parseInt(day[0])==0)
{
if(hour<0)
{
%><font color="green">已过期</font>
<%}else{
%>
<font color=red><%=day[1]%></font>
<%}}
else if(Integer.parseInt(day[0])<0)
{
%><font color="green">已过期</font>
<%}
else{ %>
<%out.print(str); %>
<%}%>
</td>