1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.util;
22
23 import java.text.DecimalFormat;
24 import java.text.NumberFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Calendar;
27 import java.util.Date;
28 import java.util.GregorianCalendar;
29
30 import net.sf.jour.log.Logger;
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class TimeUtil {
45
46 protected static final Logger log = Logger.getLogger();
47
48 public static final boolean debug = true;
49
50 public static int string2TimeSec(String str) {
51 if (str.length() != 8) {
52 return -1;
53 }
54 int h = new Integer(str.substring(0, 2)).intValue();
55 int m = new Integer(str.substring(3, 5)).intValue();
56 int s = new Integer(str.substring(6, 8)).intValue();
57 return s + m * 60 + h * 60 * 60;
58 }
59
60 public static double string2MTimeSec(String str) {
61 if (str.length() != 12) {
62 return string2TimeSec(str);
63 }
64 int h = new Integer(str.substring(0, 2)).intValue();
65 int m = new Integer(str.substring(3, 5)).intValue();
66 int s = new Integer(str.substring(6, 8)).intValue();
67 double ms = new Integer(str.substring(9, 12)).intValue();
68 return s + m * 60 + h * 60 * 60 + (ms / 1000.0);
69 }
70
71 public static String timeSec2string(long sec) {
72
73 long h = sec / (60 * 60);
74 long m = (sec - h * 60 * 60) / 60;
75 long s = (sec - h * 60 * 60 - m * 60);
76
77 DecimalFormat nf = new DecimalFormat("00");
78
79 return nf.format(h) + ":" + nf.format(m) + ":" + nf.format(s);
80 }
81
82 public static String formatDurationHHMM(double milliseconds) {
83 double hours = milliseconds / (1000D * 60D * 60D);
84 int h = (int) Math.floor(hours);
85 int m = (int) Math.ceil((hours - (double) h) * 60D);
86 if (m == 60) {
87 h++;
88 m = 0;
89 }
90 NumberFormat nf = NumberFormat.getInstance();
91 nf.setMinimumIntegerDigits(2);
92 return nf.format(h) + ":" + nf.format(m);
93 }
94
95 public static String formatDurationHHMMSS(double milliseconds) {
96 return timeSec2string((long)(milliseconds / 1000D));
97 }
98
99 public static void trunc(Calendar calendar) {
100 dayStart(calendar);
101 }
102
103 public static void dayStart(Calendar calendar) {
104 calendar.set(Calendar.SECOND, 0);
105 calendar.set(Calendar.MINUTE, 0);
106 calendar.set(Calendar.HOUR_OF_DAY, 0);
107 }
108
109 public static void dayEnd(Calendar calendar) {
110 dayStart(calendar);
111 calendar.add(Calendar.DATE, 1);
112 }
113
114 public static double string2TimeStamp(String str) {
115 if (str == null) {
116 return 0;
117 }
118 String sTime = str.trim();
119 double sec = string2MTimeSec(sTime);
120 if (sec != -1) {
121 Calendar calendar = new GregorianCalendar();
122 trunc(calendar);
123 int s = (int)Math.ceil(sec);
124 calendar.add(Calendar.SECOND, s);
125 return calendar.getTime().getTime() + (s - sec);
126 }
127 try {
128 sec = Double.valueOf(str).doubleValue();
129 if (debug) {
130 log.debug(str + "->" + timeStamp2dateString(sec));
131 }
132 return sec;
133 } catch (NumberFormatException e) {
134 }
135
136 return detectTimeformat(str);
137 }
138
139 static String timeStampFormat = "0000000000000.000";
140 static DecimalFormat timeStampFormater = new DecimalFormat(timeStampFormat);
141
142 public static String timeStamp2string(double timeStamp) {
143 return timeStampFormater.format(timeStamp);
144 }
145
146 static String timeStampDateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
147 static SimpleDateFormat timeStampDateFormater = new SimpleDateFormat(timeStampDateFormat);
148
149 public static String timeStamp2dateString(double timeStamp) {
150 return timeStampDateFormater.format(timeStamp2calendar(timeStamp).getTime());
151 }
152
153 public static Calendar timeStamp2calendar(double timeStamp) {
154 Calendar calendar = new GregorianCalendar();
155 calendar.setTime(new Date(new Double(timeStamp).longValue()));
156 return calendar;
157 }
158
159 public static double calendar2timeStamp(Calendar calendar) {
160 return calendar.getTime().getTime();
161 }
162
163
164 private static double detectTimeformat(String str) {
165 final String[] formats = {
166 "yyyy-MM-dd HH:mm:ss.SSS",
167 "yyyy-MM-dd HH:mm:ss",
168 "yyyy-MM-dd HH:mm",
169
170 "MM-dd-yyyy HH:mm:ss.SSS",
171 "MM-dd-yyyy HH:mm:ss",
172 "MM-dd-yyyy HH:mm",
173 "MM/dd/yyyy HH:mm:ss.SSS",
174 "MM/dd/yyyy HH:mm:ss",
175 "MM/dd/yyyy HH:mm",
176 "MM-dd-yyyy",
177 "yyyy-MM-dd",
178 "MM/dd/yyyy",
179 };
180
181 boolean addOneSecond = true;
182 if (str.endsWith("24:00:00")) {
183 int idx = str.indexOf("24:00:00");
184 str = str.substring(0, idx) + "23:59:59";
185 addOneSecond = true;
186 } else if (str.endsWith("24:00")) {
187 int idx = str.indexOf("24:00");
188 str = str.substring(0, idx) + "23:59:59";
189 addOneSecond = true;
190 }
191
192 for (int i = 0; i < formats.length; i++) {
193 try {
194 SimpleDateFormat aFormat = new SimpleDateFormat(formats[i]);
195 Date dateObj = aFormat.parse(str);
196 if (!str.equals(aFormat.format(dateObj))) {
197 continue;
198 }
199 if (debug) {
200 log.debug("date :" + str);
201 log.debug("format:" + formats[i] + " " + aFormat.format(dateObj));
202 }
203 if (addOneSecond) {
204 Calendar calendar = new GregorianCalendar();
205 calendar.setTime(dateObj);
206 calendar.add(Calendar.SECOND, 1);
207 return calendar.getTime().getTime();
208 }
209 return dateObj.getTime();
210 } catch (Exception ignore) {
211
212 }
213 }
214 log.warn("undetected date format [" + str + "]");
215 return 0;
216 }
217
218 private static String dateRE = "((\\d\\d-\\d\\d-\\d\\d\\d\\d)|(\\d\\d\\d\\d-\\d\\d-\\d\\d)|(\\d\\d/\\d\\d/\\d\\d\\d\\d))";
219 private static String intervalOneDayPattern = "^" + dateRE + "$";
220
221
222
223
224 private static String secRE = "\\d\\d:\\d\\d:\\d\\d";
225 private static String intervalPattern = "^(" + secRE + ")\\s*-\\s*(" + secRE + ")$";
226
227 private static String msecRE = "\\d\\d:\\d\\d:\\d\\d\\.\\d\\d\\d";
228 private static String intervalPattern1 = "^(" + msecRE + ")\\s*-\\s*(" + msecRE + ")$";
229
230 public static double[] string2TimeInterval(String comp) {
231 if (comp == null) {
232 return null;
233 }
234 comp = comp.trim();
235 String[] fullDay = RegExUtil.match(comp, intervalOneDayPattern);
236 if (fullDay.length >= 1) {
237 double day = string2TimeStamp(fullDay[0]);
238 Calendar dc = timeStamp2calendar(day);
239 double[] rc = new double[2];
240 dayStart(dc);
241 rc[0] = calendar2timeStamp(dc);
242 dayEnd(dc);
243 rc[1] = calendar2timeStamp(dc);
244 return rc;
245 }
246
247 String[] result = RegExUtil.match(comp, intervalPattern);
248 if (result.length < 2) {
249 result = RegExUtil.match(comp, intervalPattern1);
250 }
251
252 if (result.length >= 2) {
253 if (debug) {
254 log.debug(comp + "->[" + result[0] + "]-[" + result[1] + "]" + result.length);
255 }
256 double[] rc = new double[2];
257 rc[0] = string2TimeStamp(result[0]);
258 rc[1] = string2TimeStamp(result[1]);
259 return rc;
260 } else {
261 int idx = comp.indexOf(" - ");
262 if (idx > 0) {
263 double[] rc = new double[2];
264 rc[0] = string2TimeStamp(comp.substring(0, idx));
265 rc[1] = string2TimeStamp(comp.substring(idx + 3));
266 return rc;
267 }
268 return null;
269 }
270
271 }
272 }