Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../img/srcFileCovDistChart9.png 45% of files have more coverage
49   197   12   4.9
4   86   0.24   10
10     1.2  
1    
 
  DateUtility       Line # 30 49 12 82.5% 0.82539684
 
  (55)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, 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    /** ISO 8601 time format. */
37    public static final FastDateFormat ISO_8601_TIME_FORMATTER = FastDateFormat.getInstance(
38    "HH:mm:ss.SSS");
39   
40    /** Date format YYYYMMDD HHmm. */
41    public static final FastDateFormat NICE_TIMESTAMP_FORMATTER = FastDateFormat.getInstance(
42    "yyyy-MM-dd' 'HH:mm:ss.SSS");
43   
44    /** GMT timezone. */
45    private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
46   
47    /**
48    * Hidden constructor.
49    */
 
50  0 toggle private DateUtility() {
51    // nothing to do
52    }
53   
54    /**
55    * Current timestamp in ISO 8601 format (date and time).
56    *
57    * @return Current timestamp.
58    */
 
59  1 toggle public static final String getIsoTimestamp() {
60  1 return ISO_8601_TIMESTAMP_FORMATTER.format(new Date());
61    }
62   
63    /**
64    * Timestamp in ISO 8601 format (date and time).
65    *
66    * @param date Date to format.
67    * @return Timestamp.
68    */
 
69  0 toggle public static final String getIsoTimestamp(final Date date) {
70  0 return ISO_8601_TIMESTAMP_FORMATTER.format(date);
71    }
72   
73    /**
74    * Timestamp in ISO 8601 format (date and time).
75    *
76    * @param millis Time in UTC milliseconds from the epoch.
77    * @return Timestamp.
78    */
 
79  0 toggle public static final String getIsoTimestamp(final long millis) {
80  0 return ISO_8601_TIMESTAMP_FORMATTER.format(getDate(millis));
81    }
82   
83    /**
84    * Times in ISO 8601 format (time).
85    *
86    * @param millis Time in UTC milliseconds from the epoch.
87    * @return Times.
88    */
 
89  0 toggle public static final String getIsoTime(final long millis) {
90  0 return ISO_8601_TIME_FORMATTER.format(getDate(millis));
91    }
92   
93    /**
94    * Current timestamp as ISO 8601 date and time separated by space.
95    *
96    * @return Current timestamp.
97    */
 
98  454 toggle public static final String getTimestamp() {
99  454 return NICE_TIMESTAMP_FORMATTER.format(new Date());
100    }
101   
102    /**
103    * Current GMT timestamp as ISO 8601 date and time separated by space.
104    *
105    * @return Current GMT timestamp.
106    */
 
107  1 toggle public static final String getGmtTimestamp() {
108  1 return NICE_TIMESTAMP_FORMATTER.format(getCurrentGmtDate());
109    }
110   
111    /**
112    * Returns a current GMT date.
113    *
114    * @return Current GMT date.
115    */
 
116  1 toggle public static final Date getCurrentGmtDate() {
117  1 final Calendar cal = Calendar.getInstance(GMT);
118  1 final Calendar gmtDate = new GregorianCalendar();
119  1 gmtDate.set(Calendar.MONTH, cal.get(Calendar.MONTH));
120  1 gmtDate.set(Calendar.YEAR, cal.get(Calendar.YEAR));
121  1 gmtDate.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
122  1 gmtDate.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
123  1 gmtDate.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
124  1 gmtDate.set(Calendar.SECOND, cal.get(Calendar.SECOND));
125  1 return gmtDate.getTime();
126    }
127   
128    /**
129    * Convert millisecond duration into readable time amount.
130    *
131    * @param milliseconds Duration in milliseconds.
132    * @return Time in format "[d day[s] ]HH:mm:ss.SSS".
133    */
 
134  9 toggle public static final String getDuration(final long milliseconds) {
135  9 final StringBuffer buffer = new StringBuffer();
136  9 long factor = 1000 * 60 * 60 * 24;
137  9 long rest = milliseconds;
138  9 long mod = 0;
139   
140    // days
141  9 mod = rest / factor;
142  9 rest = rest % factor;
143  9 if (mod > 0) {
144  4 buffer.append(mod);
145  4 buffer.append(" day");
146  4 if (mod > 1) {
147  2 buffer.append("s");
148    }
149  4 buffer.append(" ");
150    }
151   
152    // hours
153  9 factor = factor / 24;
154  9 mod = rest / factor;
155  9 rest = rest % factor;
156  9 buffer.append(StringUtility.format(mod, 2));
157   
158  9 buffer.append(":");
159   
160    // minutes
161  9 factor = factor / 60;
162  9 mod = rest / factor;
163  9 rest = rest % factor;
164  9 buffer.append(StringUtility.format(mod, 2));
165   
166  9 buffer.append(":");
167   
168    // seconds
169  9 factor = factor / 60;
170  9 mod = rest / factor;
171  9 rest = rest % factor;
172  9 buffer.append(StringUtility.format(mod, 2));
173   
174  9 buffer.append(".");
175   
176    // milliseconds
177  9 factor = factor / 1000;
178  9 mod = rest / factor;
179    // rest = rest % factor;
180  9 buffer.append(StringUtility.format(mod, 3));
181   
182  9 return buffer.toString();
183    }
184   
185    /**
186    * Transform milliseconds from the epoch into date.
187    *
188    * @param millis Time in UTC milliseconds from the epoch.
189    * @return Date.
190    */
 
191  0 toggle public static final Date getDate(final long millis) {
192  0 final Calendar calendar = Calendar.getInstance();
193  0 calendar.setTimeInMillis(millis);
194  0 return calendar.getTime();
195    }
196   
197    }