Clover Coverage Report
Coverage timestamp: Sat Sep 18 2010 04:09:52 UTC
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
43   151   8   7.17
4   70   0.19   6
6     1.33  
1    
 
  DateUtility       Line # 30 43 8 98.1% 0.9811321
 
  (56)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2010, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.base.utility;
17   
18    import java.util.Calendar;
19    import java.util.Date;
20    import java.util.GregorianCalendar;
21    import java.util.TimeZone;
22   
23    import org.apache.commons.lang.time.FastDateFormat;
24   
25    /**
26    * Various methods for date and time handling.
27    *
28    * @author Michael Meyling
29    */
 
30    public final class DateUtility {
31   
32    /** ISO 8601 date and time format. */
33    public static final FastDateFormat ISO_8601_TIMESTAMP_FORMATTER = FastDateFormat.getInstance(
34    "yyyy-MM-dd'T'HH:mm:ss.SSS");
35   
36    /** Date format YYYYMMDD HHmm. */
37    public static final FastDateFormat NICE_TIMESTAMP_FORMATTER = FastDateFormat.getInstance(
38    "yyyy-MM-dd' 'HH:mm:ss.SSS");
39   
40    /** GMT timezone. */
41    private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
42   
43    /**
44    * Hidden constructor.
45    */
 
46  0 toggle private DateUtility() {
47    // nothing to do
48    }
49   
50    /**
51    * Current timestamp in ISO 8601 format (date and time).
52    *
53    * @return Current timestamp.
54    */
 
55  1 toggle public static final String getIsoTimestamp() {
56  1 return ISO_8601_TIMESTAMP_FORMATTER.format(new Date());
57    }
58   
59    /**
60    * Current timestamp as ISO 8601 date and time separated by space.
61    *
62    * @return Current timestamp.
63    */
 
64  1840 toggle public static final String getTimestamp() {
65  1840 return NICE_TIMESTAMP_FORMATTER.format(new Date());
66    }
67   
68    /**
69    * Current GMT timestamp as ISO 8601 date and time separated by space.
70    *
71    * @return Current GMT timestamp.
72    */
 
73  1 toggle public static final String getGmtTimestamp() {
74  1 return NICE_TIMESTAMP_FORMATTER.format(getCurrentGmtDate());
75    }
76   
77    /**
78    * Returns a current GMT date.
79    *
80    * @return Current GMT date.
81    */
 
82  1 toggle public static final Date getCurrentGmtDate() {
83  1 final Calendar cal = Calendar.getInstance(GMT);
84  1 final Calendar gmtDate = new GregorianCalendar();
85  1 gmtDate.set(Calendar.MONTH, cal.get(Calendar.MONTH));
86  1 gmtDate.set(Calendar.YEAR, cal.get(Calendar.YEAR));
87  1 gmtDate.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
88  1 gmtDate.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
89  1 gmtDate.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
90  1 gmtDate.set(Calendar.SECOND, cal.get(Calendar.SECOND));
91  1 return gmtDate.getTime();
92    }
93   
94    /**
95    * Convert millisecond duration into readable time amount.
96    *
97    * @param milliseconds Duration in milliseconds.
98    * @return Time in format "[d day[s] ]HH:mm:ss.SSS".
99    */
 
100  9 toggle public static final String getDuration(final long milliseconds) {
101  9 final StringBuffer buffer = new StringBuffer();
102  9 long factor = 1000 * 60 * 60 * 24;
103  9 long rest = milliseconds;
104  9 long mod = 0;
105   
106    // days
107  9 mod = rest / factor;
108  9 rest = rest % factor;
109  9 if (mod > 0) {
110  4 buffer.append(mod);
111  4 buffer.append(" day");
112  4 if (mod > 1) {
113  2 buffer.append("s");
114    }
115  4 buffer.append(" ");
116    }
117   
118    // hours
119  9 factor = factor / 24;
120  9 mod = rest / factor;
121  9 rest = rest % factor;
122  9 buffer.append(StringUtility.format(mod, 2));
123   
124  9 buffer.append(":");
125   
126    // minutes
127  9 factor = factor / 60;
128  9 mod = rest / factor;
129  9 rest = rest % factor;
130  9 buffer.append(StringUtility.format(mod, 2));
131   
132  9 buffer.append(":");
133   
134    // seconds
135  9 factor = factor / 60;
136  9 mod = rest / factor;
137  9 rest = rest % factor;
138  9 buffer.append(StringUtility.format(mod, 2));
139   
140  9 buffer.append(".");
141   
142    // milliseconds
143  9 factor = factor / 1000;
144  9 mod = rest / factor;
145    // rest = rest % factor;
146  9 buffer.append(StringUtility.format(mod, 3));
147   
148  9 return buffer.toString();
149    }
150   
151    }