查看: 1535|回复: 0

使用StopWatch类输出时间戳

[复制链接]
论坛徽章:
66
ERP板块每日发贴之星
日期:2005-08-18 01:01:39生肖徽章2007版:兔
日期:2008-01-02 17:35:53生肖徽章2007版:牛
日期:2008-01-02 17:35:53生肖徽章2007版:蛇
日期:2008-04-07 19:42:14体育版块博采纪念徽章
日期:2008-07-03 19:47:13CTO参与奖
日期:2009-02-20 09:44:20生肖徽章2007版:狗
日期:2009-09-07 16:03:53ITPUB9周年纪念徽章
日期:2010-10-08 09:28:522013年新春福章
日期:2013-02-25 14:51:24生肖徽章:鸡
日期:2006-09-07 17:09:37
跳转到指定楼层
1#
发表于 2006-7-17 21:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
package com.generationjava.test;

/**

* Useful when doing timings in a debug or test situation.

*/

public class StopWatch {

static public int AN_HOUR = 60 * 60 * 1000;

static public int A_MINUTE = 60 * 1000;

private long startTime = -1;

private long stopTime = -1;

/**

* Start the stopwatch.

*/

public void start() {

this.startTime = System.currentTimeMillis();

}

/**

* Stop the stopwatch.

*/

public void stop() {

this.stopTime = System.currentTimeMillis();

}

/**

* Reset the stopwatch.

*/

public void reset() {

this.startTime = -1;

this.stopTime = -1;

}

/**

* Split the time.

*/

public void split() {

this.stopTime = System.currentTimeMillis();

}

/**

* Remove a split.

*/

public void unsplit() {

this.stopTime = -1;

}

/**

* Get the time on the stopwatch. This is either the

* time between start and latest split, between start and stop,

* or the time between the start and the moment this method is called.

*/

public long getTime() {

if(stopTime != -1) {

return (System.currentTimeMillis() - this.startTime);

} else {

return this.stopTime - this.startTime;

}

}

public String toString() {

return getTimeString();

}

/**

* Get the time gap as a String.

* In hours, minutes, seconds and milliseconds.

*/

public String getTimeString() {

int hours, minutes, seconds, milliseconds;

long time = getTime();

hours = (int) (time / AN_HOUR);

time = time - (hours * AN_HOUR);

minutes = (int) (time / A_MINUTE);

time = time - (minutes * A_MINUTE);

seconds = (int) (time / 1000);

time = time - (seconds * 1000);

millis = (int) time;

return hours + "h:" + minutes + "m:" + seconds + "s:" + millis + "ms";

}

}

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表