您的位置:首页 > 数据库 > Oracle

Oracle中关于计算时间差的例子:

2013-07-11 16:52 387 查看
今天在做项目的时候遇到了这样一个问题:就是要得到在线时长:

截图如下:最后要达到如下效果:



我之前想了一种方案:我想通过SQL语句查库,但是最后发现这种办法还解决不了,最后我又想了一种:

就是通过一个计算时间差的方法直接在那个在线时长的get()方法中给它赋值,这样就可以很简单的得到这个值了。

废话不多说了,直接上代码:

1.这是你计算时间差的类:

public class DateFormat {

/**

* 计算两个时间的时间差

* @param from

* @param to

* @return

*/

public static String timeBetweens(Date from, Date to) {

final long ONEDAY = 86400000L; // 1天=86400000毫秒

final long ONEHOUR = 3600000L; // 1小时=3600000毫秒

final long ONEMIN = 60000L; // 1分钟=60000毫秒

long days = 0, hours = 0, mins = 0, secs = 0;

if (to.after(from)) {

secs = to.getTime() - from.getTime();

days = secs / ONEDAY;

secs = secs % ONEDAY;

hours = secs / ONEHOUR;

secs = secs % ONEHOUR;

mins = secs / ONEMIN;

}

String rtnStr = days + "天" + hours + "小时" + mins + "分钟";

return rtnStr;

}

public static void main(String[] args) {

Date date = new Date();

Date date1 = new Date();

String string = timeBetweens(date, date1);

System.out.println(string);

}

}

2. 这是你的实体类:

private String wmistiming;

public String getWmistiming() {

return DateFormat.timeBetweens(wlogdate, wupdate);

}

public void setWmistiming(String wmistiming) {

this.wmistiming = wmistiming;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: