StopWatch.java 713 B

1234567891011121314151617181920212223242526272829
  1. package com.alvin.util;
  2. class StopWatch {
  3. private String name = "";
  4. private long startTime;
  5. public StopWatch() {
  6. startTime = System.currentTimeMillis();
  7. }
  8. public StopWatch(String name) {
  9. this();
  10. this.name = name;
  11. }
  12. public String stop() {
  13. long endTime = System.currentTimeMillis();
  14. long spendTime = endTime - this.startTime;
  15. String s = DateUtil.formatTime(spendTime);
  16. System.out.println(this.name + " 用时:" + s);
  17. return s;
  18. }
  19. public static void main(String[] args) throws InterruptedException {
  20. StopWatch stopWatch = new StopWatch("A");
  21. Thread.sleep(100);
  22. stopWatch.stop();
  23. }
  24. }