blob: f11655ff13e38974f29f95ac22f690598e265de0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package com.nis.nmsclient.common;
import java.util.LinkedHashMap;
/**
* 秒表计时器
* @author fang
*
*/
public class StopWatch {
private static final long SEC_MILL = 1000;
private static final long MIN_MILL = 60 * SEC_MILL;
private static final long HOUR_MILL = 60 * MIN_MILL;
private static final long DAY_MILL = 24 * HOUR_MILL;
private long start;
private long end;
private LinkedHashMap<String,Long> tagMap = new LinkedHashMap<String,Long>();
public StopWatch(){
start();
}
public static StopWatch newStopWacth(){
return new StopWatch();
}
/**
* 计时器开始
* @return
*/
public long start(){
this.start = System.currentTimeMillis();
return start;
}
/**
* 计时器结束
* @return
*/
public long end(){
this.end = System.currentTimeMillis();
return end;
}
public long tag(String tag){
long l = System.currentTimeMillis();
this.tagMap.put(tag, l);
return l;
}
/**
* 计算两个 tag 之间的时间差
* @param b
* @param a
* @return
*/
public long between(String b,String a){
Long l1 = this.tagMap.get(b);
Long l2 = this.tagMap.get(a);
if(l1 != null && l2 != null){
return l1-l2;
}
return -1;
}
public static String toString(long l){
StringBuilder sb = new StringBuilder();
if(l >= DAY_MILL){
sb.append((l/DAY_MILL));
sb.append( "天");
l = l % DAY_MILL;
}
if(l >= HOUR_MILL){
sb.append((l/HOUR_MILL));
sb.append( "小时");
l = l % HOUR_MILL;
}
if(l >= MIN_MILL){
sb.append((l/MIN_MILL));
sb.append( "分");
l = l % MIN_MILL;
}
if(l >= SEC_MILL){
sb.append((l/SEC_MILL));
sb.append( "秒");
l = l % SEC_MILL;
}
sb.append((l));
sb.append( "毫秒");
return sb.toString();
}
public String toString(){
return "";
}
/**
* 从开始到结束总耗时
* @return
*/
public long total(){
long temp = System.currentTimeMillis();
if(this.end < this.start){
this.end = temp;
}
return end - start;
}
public void reset(){
this.tagMap.clear();
this.start();
}
public long getStart() {
return start;
}
public void setStart(long start) {
this.start = start;
}
public long getEnd() {
return end;
}
public void setEnd(long end) {
this.end = end;
}
public LinkedHashMap<String, Long> getTag() {
return tagMap;
}
public void LinkedHashMap(LinkedHashMap<String, Long> tag) {
this.tagMap = tag;
}
public static void main(String[] args) {
long s = System.currentTimeMillis();
long end = s +2*DAY_MILL+ 12 * MIN_MILL + 30*SEC_MILL + 388;
String string = StopWatch.toString(end -s);
System.out.println(string);
}
}
|