1
2
3
4
5
6
7
8
9
10
11 package de.jaret.util.date;
12
13 import java.text.ParseException;
14 import java.util.StringTokenizer;
15
16 import de.jaret.util.misc.FormatHelper;
17
18
19
20
21
22
23
24 public class TimeHelper {
25
26 public static String secondsToString(int sec, boolean includeSeconds) {
27 int hours = sec / 3600;
28 int minutes = (sec % 3600) / 60;
29 int seconds = (sec % 60);
30 String str = FormatHelper.NFInt2Digits().format(hours) + ":" + FormatHelper.NFInt2Digits().format(minutes);
31 if (includeSeconds) {
32 str = str + ":" + FormatHelper.NFInt2Digits().format(seconds);
33 }
34 return str;
35 }
36
37 public static int timeStringToSeconds(String str) throws ParseException {
38 try {
39 StringTokenizer tokenizer = new StringTokenizer(str, ":");
40 int h = Integer.parseInt(tokenizer.nextToken());
41 int m = Integer.parseInt(tokenizer.nextToken());
42 int s = 0;
43 if (tokenizer.hasMoreTokens()) {
44 s = Integer.parseInt(tokenizer.nextToken());
45 }
46 return h * 3600 + m * 60 + s;
47 } catch (Exception e) {
48 throw new ParseException(str, 0);
49 }
50 }
51
52 }