因为两个项目都需要将日期字符格式为:2小时前,2天前这样的显示效果,因此分别制作 了两个函数。代码如下:
Asp日期格式函数:
function FormTime(t)
dim ret
y=cint(t/(365*24*3600))
if(y>0) then
ret=y&"年"
else
m=cint(t/(30*3600*24))
if(m>0) then
ret=m&"月"
else
w=cint(t/(7*3600*24))
if(w>0) then
ret=w&"周"
else
d=cint(t/(3600*24))
if(d>0) then
ret=d&"天"
else
h=cint(t/3600)
if(h>0) then
ret=h&"小时"
else
mi=cint(t/60)
if(mi>0) then
ret=mi&"分钟"
else
ret="刚刚"
end if
end if
end if
end if
end if
end if
FormTime=ret
end function
Java日期格式函数:
public static final long SECOND = 1000;
public static final long MINUTE = SECOND * 60;
public static final long HOUR = MINUTE * 60;
public static final long DAY = HOUR * 24;
public static final long WEEK = DAY * 7;
public static final long MONTH = DAY * 30;
public static final long YEAR = DAY * 365;
public static String <span style="font-family: Arial, Helvetica, sans-serif;">FormTime</span>(java.sql.Timestamp t){
String ret="";
if(t!=null){
long now=System.currentTimeMillis();
long st=t.getTime();
long vers=(now-st);
long y=vers/YEAR;
if(y>0){
ret=y+"年";
}else{
long m=vers/MONTH;
if(m>0){
ret=m+"月";
}else{
long w=vers/WEEK;
if(w>0){
ret=w+"周";
}else{
long d=vers/DAY;
if(d>0){
ret=d+"天";
}else{
long h=vers/HOUR;
if(h>0){
ret=h+"小时";
}else{
long mi=vers/MINUTE;
if(mi>0){
ret=mi+"分钟";
}else{
ret="刚刚";
}
}
}
}
}
}
}
return ret;
}