科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件使用StopWatch类输出时间戳

使用StopWatch类输出时间戳

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文是关于如何使用StopWatch类输出时间戳的源码

作者:NET CHINA  来源:NET CHINA  2007年9月3日

关键字: Stopwatch 时间戳

  • 评论
  • 分享微博
  • 分享邮件
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";

}

}

查看本文来源
    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章