| 1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
| 2 | * |
| 3 | * Copyright 2000-2014, 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 | 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 | public static final String getIsoTimestamp() { |
| 60 | 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 | public static final String getIsoTimestamp(final Date date) { |
| 70 | 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 | public static final String getIsoTimestamp(final long millis) { |
| 80 | 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 | public static final String getIsoTime(final long millis) { |
| 90 | 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 | public static final String getTimestamp() { |
| 99 | 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 | } |