使用StopWatch类输出时间戳

2016-02-19 18:56 3 1 收藏

今天图老师小编给大家介绍下使用StopWatch类输出时间戳,平时喜欢使用StopWatch类输出时间戳的朋友赶紧收藏起来吧!记得点赞哦~

【 tulaoshi.com - 编程语言 】


  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";
  
  }
  
  }

来源:https://www.tulaoshi.com/n/20160219/1619559.html

延伸阅读
为了应用方便,您可能需要给数据库的每条记录都添加日期/时间戳,以便确定各个记录添加到数据库的时间。在Access数据库应用中,使用Now()函数能够轻松完成这个任务。本文将一步一步为您介绍整个添加过程,非常简单。本文所使用的Access版本为Access 2007,对于之前的版本,添加步骤类似但不完全一致。 1. 打开包含了您需要添加日期或...
标签: PHP
内容缓存输出 PEAR cache 接下来我们开始探索更常用的缓存 技术 ,这也是本文的重点部份。首先我们使用PEAR中的cache包。PEAR可以将内容缓存于文件, 数据库 或者内存中,我们以文件为例。 下面是一个没有使用缓存的 PHP 小程序: pear_content_cache1.php <?php echo "这是内容。<P>"; echo "...
标签: Web开发
Ajax应用程序中服务器端如果使用ASPX返回XML格式数据,一般将VS生成的HTML字符先清除,代码文件中直接使用Response.Write输出XML字符串。   需要注意的是要设置Response的相应属性客户端才能正确解析字符串。   首先要设置ContentType 属性: Response.ContentType = "text/xml"; 如果xml字符串中包含...
使用MFC的数组类 作者:韩耀旭 下载源代码 MFC的数组类支持的数组类似于C++中的常规数组,可以存放任何数据类型。C++的常规数组在使用前必须将其定义成能够容纳所有可能需要的元素,而MFC数组类创建的对象可以根据需要动态地增大或减小,数组的起始下标是0,而上限可以是固定的,也可以随着元素...
标签: Web开发
今天一个查询需要通过 ExcuteReader  返回结果集,同时又想输出参数,刚开始的时候一直得不到输出参数的值,以为存储过程出错,但是在查询分析器里面测试是正确的,而且输出参数确实已经赋值。 更加让人百思不得其解的是,对出输出强制类型转换丢出异常之后,确又可以得到了,难道是ado.net 的bug,想象页不可能啊,这么常用的API...

经验教程

54

收藏

72
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部