1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package jgroup.util;
20
21 import java.util.Calendar;
22
23
24
25
26
27
28
29
30
31
32 public final class Util
33 {
34
35
36
37
38 private Util() { }
39
40
41
42
43
44
45
46
47
48
49
50
51 public static int valueAt(int[] array, int key)
52 {
53 int i;
54
55 for (i=0; i < array.length && array[i] < key; i++);
56 return ((i < array.length && array[i] == key) ? i : -1);
57 }
58
59 public static int valueAt(Object[] array, Object key)
60 {
61 int i;
62
63 try {
64 for(i=0; !array[i].equals(key); i++);
65 return i;
66 } catch (Exception e) {}
67 return -1;
68 }
69
70 public static int referenceAt(Object[] array, Object key)
71 {
72 int i;
73
74 try {
75 for(i=0; array[i] != key; i++);
76 return i;
77 } catch (Exception e) {}
78 return -1;
79 }
80
81
82
83
84
85
86 public static boolean in(Object[] array, Object obj)
87 {
88 for (int i=array.length-1; i >=0; i--)
89 if (array[i].equals(obj))
90 return true;
91 return false;
92 }
93
94
95
96
97
98
99
100
101
102 public static String getDateString()
103 {
104 Calendar gc = Calendar.getInstance();
105 StringBuilder buffer = new StringBuilder();
106 buffer.append(Integer.toString(gc.get(Calendar.YEAR)));
107 int month = gc.get(Calendar.MONTH) + 1;
108 if (month < 10)
109 buffer.append("0");
110 buffer.append(Integer.toString(month));
111 int day = gc.get(Calendar.DAY_OF_MONTH);
112 if (day < 10)
113 buffer.append("0");
114 buffer.append(Integer.toString(day));
115 return buffer.toString();
116 }
117
118
119
120
121
122
123
124
125
126 public static String getTimeString()
127 {
128 Calendar gc = Calendar.getInstance();
129 StringBuilder buffer = new StringBuilder();
130 int hour = gc.get(Calendar.HOUR_OF_DAY);
131 if (hour < 10)
132 buffer.append("0");
133 buffer.append(hour);
134 int minute = gc.get(Calendar.MINUTE);
135 if (minute < 10)
136 buffer.append("0");
137 buffer.append(minute);
138 int second = gc.get(Calendar.SECOND);
139 if (second < 10)
140 buffer.append("0");
141 buffer.append(second);
142 return buffer.toString();
143 }
144
145
146
147
148
149
150
151
152
153
154 public static String getDateTimeString()
155 {
156 Calendar gc = Calendar.getInstance();
157 StringBuilder buffer = new StringBuilder();
158 buffer.append(Integer.toString(gc.get(Calendar.YEAR)));
159 int month = gc.get(Calendar.MONTH) + 1;
160 if (month < 10)
161 buffer.append("0");
162 buffer.append(Integer.toString(month));
163 int day = gc.get(Calendar.DAY_OF_MONTH);
164 if (day < 10)
165 buffer.append("0");
166 buffer.append(Integer.toString(day));
167 buffer.append("-");
168 int hour = gc.get(Calendar.HOUR_OF_DAY);
169 if (hour < 10)
170 buffer.append("0");
171 buffer.append(Integer.toString(hour));
172 int minute = gc.get(Calendar.MINUTE);
173 if (minute < 10)
174 buffer.append("0");
175 buffer.append(Integer.toString(minute));
176 int second = gc.get(Calendar.SECOND);
177 if (second < 10)
178 buffer.append("0");
179 buffer.append(Integer.toString(second));
180 return buffer.toString();
181 }
182
183
184
185
186
187
188
189 public static String byte2str(byte value)
190 {
191 int val = (value & 0xFF);
192 StringBuilder buf = new StringBuilder();
193 if (val < 100) {
194 buf.append("0");
195 if (val < 10)
196 buf.append("0");
197 }
198 buf.append(val);
199 return buf.toString();
200 }
201
202
203
204
205
206
207
208 public static void sleep(long time)
209 {
210 try {
211 Thread.sleep(time);
212 } catch (InterruptedException e) {
213
214 }
215 }
216
217 }