001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2011, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.base.utility;
017
018 import java.util.Calendar;
019 import java.util.Date;
020 import java.util.GregorianCalendar;
021 import java.util.TimeZone;
022
023 import org.apache.commons.lang.time.FastDateFormat;
024
025 /**
026 * Various methods for date and time handling.
027 *
028 * @author Michael Meyling
029 */
030 public final class DateUtility {
031
032 /** ISO 8601 date and time format. */
033 public static final FastDateFormat ISO_8601_TIMESTAMP_FORMATTER = FastDateFormat.getInstance(
034 "yyyy-MM-dd'T'HH:mm:ss.SSS");
035
036 /** ISO 8601 time format. */
037 public static final FastDateFormat ISO_8601_TIME_FORMATTER = FastDateFormat.getInstance(
038 "HH:mm:ss.SSS");
039
040 /** Date format YYYYMMDD HHmm. */
041 public static final FastDateFormat NICE_TIMESTAMP_FORMATTER = FastDateFormat.getInstance(
042 "yyyy-MM-dd' 'HH:mm:ss.SSS");
043
044 /** GMT timezone. */
045 private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
046
047 /**
048 * Hidden constructor.
049 */
050 private DateUtility() {
051 // nothing to do
052 }
053
054 /**
055 * Current timestamp in ISO 8601 format (date and time).
056 *
057 * @return Current timestamp.
058 */
059 public static final String getIsoTimestamp() {
060 return ISO_8601_TIMESTAMP_FORMATTER.format(new Date());
061 }
062
063 /**
064 * Timestamp in ISO 8601 format (date and time).
065 *
066 * @param date Date to format.
067 * @return Timestamp.
068 */
069 public static final String getIsoTimestamp(final Date date) {
070 return ISO_8601_TIMESTAMP_FORMATTER.format(date);
071 }
072
073 /**
074 * Timestamp in ISO 8601 format (date and time).
075 *
076 * @param millis Time in UTC milliseconds from the epoch.
077 * @return Timestamp.
078 */
079 public static final String getIsoTimestamp(final long millis) {
080 return ISO_8601_TIMESTAMP_FORMATTER.format(getDate(millis));
081 }
082
083 /**
084 * Times in ISO 8601 format (time).
085 *
086 * @param millis Time in UTC milliseconds from the epoch.
087 * @return Times.
088 */
089 public static final String getIsoTime(final long millis) {
090 return ISO_8601_TIME_FORMATTER.format(getDate(millis));
091 }
092
093 /**
094 * Current timestamp as ISO 8601 date and time separated by space.
095 *
096 * @return Current timestamp.
097 */
098 public static final String getTimestamp() {
099 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 public static final String getGmtTimestamp() {
108 return NICE_TIMESTAMP_FORMATTER.format(getCurrentGmtDate());
109 }
110
111 /**
112 * Returns a current GMT date.
113 *
114 * @return Current GMT date.
115 */
116 public static final Date getCurrentGmtDate() {
117 final Calendar cal = Calendar.getInstance(GMT);
118 final Calendar gmtDate = new GregorianCalendar();
119 gmtDate.set(Calendar.MONTH, cal.get(Calendar.MONTH));
120 gmtDate.set(Calendar.YEAR, cal.get(Calendar.YEAR));
121 gmtDate.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
122 gmtDate.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
123 gmtDate.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
124 gmtDate.set(Calendar.SECOND, cal.get(Calendar.SECOND));
125 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 public static final String getDuration(final long milliseconds) {
135 final StringBuffer buffer = new StringBuffer();
136 long factor = 1000 * 60 * 60 * 24;
137 long rest = milliseconds;
138 long mod = 0;
139
140 // days
141 mod = rest / factor;
142 rest = rest % factor;
143 if (mod > 0) {
144 buffer.append(mod);
145 buffer.append(" day");
146 if (mod > 1) {
147 buffer.append("s");
148 }
149 buffer.append(" ");
150 }
151
152 // hours
153 factor = factor / 24;
154 mod = rest / factor;
155 rest = rest % factor;
156 buffer.append(StringUtility.format(mod, 2));
157
158 buffer.append(":");
159
160 // minutes
161 factor = factor / 60;
162 mod = rest / factor;
163 rest = rest % factor;
164 buffer.append(StringUtility.format(mod, 2));
165
166 buffer.append(":");
167
168 // seconds
169 factor = factor / 60;
170 mod = rest / factor;
171 rest = rest % factor;
172 buffer.append(StringUtility.format(mod, 2));
173
174 buffer.append(".");
175
176 // milliseconds
177 factor = factor / 1000;
178 mod = rest / factor;
179 // rest = rest % factor;
180 buffer.append(StringUtility.format(mod, 3));
181
182 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 public static final Date getDate(final long millis) {
192 final Calendar calendar = Calendar.getInstance();
193 calendar.setTimeInMillis(millis);
194 return calendar.getTime();
195 }
196
197 }
|