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