|
| 1 | +package org.wordpress.android.util; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.text.format.DateUtils; |
| 5 | + |
| 6 | +import java.text.DateFormat; |
| 7 | +import java.text.ParseException; |
| 8 | +import java.text.SimpleDateFormat; |
| 9 | +import java.util.Date; |
| 10 | +import java.util.Locale; |
| 11 | +import java.util.TimeZone; |
| 12 | + |
| 13 | +public class DateTimeUtils { |
| 14 | + private DateTimeUtils() { |
| 15 | + throw new AssertionError(); |
| 16 | + } |
| 17 | + |
| 18 | + // See http://drdobbs.com/java/184405382 |
| 19 | + private static final ThreadLocal<DateFormat> ISO8601_FORMAT = new ThreadLocal<DateFormat>() { |
| 20 | + @Override |
| 21 | + protected DateFormat initialValue() { |
| 22 | + return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US); |
| 23 | + } |
| 24 | + }; |
| 25 | + |
| 26 | + /** |
| 27 | + * Converts a date to a relative time span ("8h", "3d", etc.) - similar to |
| 28 | + * DateUtils.getRelativeTimeSpanString but returns shorter result |
| 29 | + */ |
| 30 | + public static String javaDateToTimeSpan(final Date date, Context context) { |
| 31 | + if (date == null) { |
| 32 | + return ""; |
| 33 | + } |
| 34 | + |
| 35 | + long passedTime = date.getTime(); |
| 36 | + long currentTime = System.currentTimeMillis(); |
| 37 | + |
| 38 | + // return "now" if less than a minute has elapsed |
| 39 | + long secondsSince = (currentTime - passedTime) / 1000; |
| 40 | + if (secondsSince < 60) { |
| 41 | + return context.getString(R.string.timespan_now); |
| 42 | + } |
| 43 | + |
| 44 | + // less than an hour (ex: 12m) |
| 45 | + long minutesSince = secondsSince / 60; |
| 46 | + if (minutesSince < 60) { |
| 47 | + return Long.toString(minutesSince) + "m"; |
| 48 | + } |
| 49 | + |
| 50 | + // less than a day (ex: 17h) |
| 51 | + long hoursSince = minutesSince / 60; |
| 52 | + if (hoursSince < 24) { |
| 53 | + return Long.toString(hoursSince) + "h"; |
| 54 | + } |
| 55 | + |
| 56 | + // less than a week (ex: 5d) |
| 57 | + long daysSince = hoursSince / 24; |
| 58 | + if (daysSince < 7) { |
| 59 | + return Long.toString(daysSince) + "d"; |
| 60 | + } |
| 61 | + |
| 62 | + // less than a year old, so return day/month without year (ex: Jan 30) |
| 63 | + if (daysSince < 365) { |
| 64 | + return DateUtils.formatDateTime(context, passedTime, DateUtils.FORMAT_NO_YEAR | |
| 65 | + DateUtils.FORMAT_ABBREV_ALL); |
| 66 | + } |
| 67 | + |
| 68 | + // date is older, so include year (ex: Jan 30, 2013) |
| 69 | + return DateUtils.formatDateTime(context, passedTime, DateUtils.FORMAT_ABBREV_ALL); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Given an ISO 8601-formatted date as a String, returns a {@link Date}. |
| 74 | + */ |
| 75 | + public static Date dateFromIso8601(final String strDate) { |
| 76 | + try { |
| 77 | + DateFormat formatter = ISO8601_FORMAT.get(); |
| 78 | + return formatter.parse(strDate); |
| 79 | + } catch (ParseException e) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Given an ISO 8601-formatted date as a String, returns a {@link Date} in UTC. |
| 86 | + */ |
| 87 | + public static Date dateUTCFromIso8601(String iso8601date) { |
| 88 | + try { |
| 89 | + iso8601date = iso8601date.replace("Z", "+0000").replace("+00:00", "+0000"); |
| 90 | + DateFormat formatter = ISO8601_FORMAT.get(); |
| 91 | + formatter.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 92 | + return formatter.parse(iso8601date); |
| 93 | + } catch (ParseException e) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Given a {@link Date}, returns an ISO 8601-formatted String. |
| 100 | + */ |
| 101 | + public static String iso8601FromDate(Date date) { |
| 102 | + if (date == null) { |
| 103 | + return ""; |
| 104 | + } |
| 105 | + DateFormat formatter = ISO8601_FORMAT.get(); |
| 106 | + return formatter.format(date); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Given a {@link Date}, returns an ISO 8601-formatted String in UTC. |
| 111 | + */ |
| 112 | + public static String iso8601UTCFromDate(Date date) { |
| 113 | + if (date == null) { |
| 114 | + return ""; |
| 115 | + } |
| 116 | + TimeZone tz = TimeZone.getTimeZone("UTC"); |
| 117 | + DateFormat formatter = ISO8601_FORMAT.get(); |
| 118 | + formatter.setTimeZone(tz); |
| 119 | + |
| 120 | + String iso8601date = formatter.format(date); |
| 121 | + |
| 122 | + // Use "+00:00" notation rather than "+0000" to be consistent with the WP.COM API |
| 123 | + return iso8601date.replace("+0000", "+00:00"); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Returns the current UTC date |
| 128 | + */ |
| 129 | + public static Date nowUTC() { |
| 130 | + Date dateTimeNow = new Date(); |
| 131 | + return localDateToUTC(dateTimeNow); |
| 132 | + } |
| 133 | + |
| 134 | + public static Date localDateToUTC(Date dtLocal) { |
| 135 | + if (dtLocal == null) { |
| 136 | + return null; |
| 137 | + } |
| 138 | + TimeZone tz = TimeZone.getDefault(); |
| 139 | + int currentOffsetFromUTC = tz.getRawOffset() + (tz.inDaylightTime(dtLocal) ? tz.getDSTSavings() : 0); |
| 140 | + return new Date(dtLocal.getTime() - currentOffsetFromUTC); |
| 141 | + } |
| 142 | + |
| 143 | + // Routines to return a diff between two dates - always return a positive number |
| 144 | + |
| 145 | + public static int daysBetween(Date dt1, Date dt2) { |
| 146 | + long hrDiff = hoursBetween(dt1, dt2); |
| 147 | + if (hrDiff == 0) { |
| 148 | + return 0; |
| 149 | + } |
| 150 | + return (int) (hrDiff / 24); |
| 151 | + } |
| 152 | + |
| 153 | + public static int hoursBetween(Date dt1, Date dt2) { |
| 154 | + long minDiff = minutesBetween(dt1, dt2); |
| 155 | + if (minDiff == 0) { |
| 156 | + return 0; |
| 157 | + } |
| 158 | + return (int) (minDiff / 60); |
| 159 | + } |
| 160 | + |
| 161 | + public static int minutesBetween(Date dt1, Date dt2) { |
| 162 | + long msDiff = millisecondsBetween(dt1, dt2); |
| 163 | + if (msDiff == 0) { |
| 164 | + return 0; |
| 165 | + } |
| 166 | + return (int) (msDiff / 60000); |
| 167 | + } |
| 168 | + |
| 169 | + public static int secondsBetween(Date dt1, Date dt2) { |
| 170 | + long msDiff = millisecondsBetween(dt1, dt2); |
| 171 | + if (msDiff == 0) { |
| 172 | + return 0; |
| 173 | + } |
| 174 | + return (int) (msDiff / 1000); |
| 175 | + } |
| 176 | + |
| 177 | + public static long millisecondsBetween(Date dt1, Date dt2) { |
| 178 | + if (dt1 == null || dt2 == null) { |
| 179 | + return 0; |
| 180 | + } |
| 181 | + return Math.abs(dt1.getTime() - dt2.getTime()); |
| 182 | + } |
| 183 | + |
| 184 | + public static boolean isSameYear(Date dt1, Date dt2) { |
| 185 | + if (dt1 == null || dt2 == null) { |
| 186 | + return false; |
| 187 | + } |
| 188 | + return dt1.getYear() == dt2.getYear(); |
| 189 | + } |
| 190 | + |
| 191 | + public static boolean isSameMonthAndYear(Date dt1, Date dt2) { |
| 192 | + if (dt1 == null || dt2 == null) { |
| 193 | + return false; |
| 194 | + } |
| 195 | + return dt1.getYear() == dt2.getYear() && dt1.getMonth() == dt2.getMonth(); |
| 196 | + } |
| 197 | + |
| 198 | + // Routines involving Unix timestamps (GMT assumed) |
| 199 | + |
| 200 | + /** |
| 201 | + * Given an ISO 8601-formatted date as a String, returns the corresponding UNIX timestamp. |
| 202 | + */ |
| 203 | + public static long timestampFromIso8601(final String strDate) { |
| 204 | + return (timestampFromIso8601Millis(strDate) / 1000); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Given an ISO 8601-formatted date as a String, returns the corresponding timestamp in milliseconds. |
| 209 | + */ |
| 210 | + public static long timestampFromIso8601Millis(final String strDate) { |
| 211 | + Date date = dateFromIso8601(strDate); |
| 212 | + if (date == null) { |
| 213 | + return 0; |
| 214 | + } |
| 215 | + return (date.getTime()); |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Given a UNIX timestamp, returns the corresponding {@link Date}. |
| 220 | + */ |
| 221 | + public static Date dateFromTimestamp(long timestamp) { |
| 222 | + return new java.util.Date(timestamp * 1000); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Given a UNIX timestamp, returns an ISO 8601-formatted date as a String. |
| 227 | + */ |
| 228 | + public static String iso8601FromTimestamp(long timestamp) { |
| 229 | + return iso8601FromDate(dateFromTimestamp(timestamp)); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Given a UNIX timestamp, returns an ISO 8601-formatted date in UTC as a String. |
| 234 | + */ |
| 235 | + public static String iso8601UTCFromTimestamp(long timestamp) { |
| 236 | + return iso8601UTCFromDate(dateFromTimestamp(timestamp)); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Given a UNIX timestamp, returns a relative time span ("8h", "3d", etc.). |
| 241 | + */ |
| 242 | + public static String timeSpanFromTimestamp(long timestamp, Context context) { |
| 243 | + Date dateGMT = dateFromTimestamp(timestamp); |
| 244 | + return javaDateToTimeSpan(dateGMT, context); |
| 245 | + } |
| 246 | +} |
0 commit comments