Created
June 21, 2014 02:55
-
-
Save adamcmwilson/35fa22049ab3dea0e0ca to your computer and use it in GitHub Desktop.
Creates an English phrase for time intervals such as "in 2 weeks" or "5 minutes ago"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class DateTimeIntervalPhraser { | |
| public static class Values { | |
| public static final Long SECOND = 1000l; | |
| public static final Long MINUTE = 60 * SECOND; | |
| public static final Long HOUR = 60 * MINUTE; | |
| public static final Long DAY = 24 * HOUR; | |
| public static final Long WEEK = 7 * DAY; | |
| public static final Long YEAR = 365 * DAY; | |
| public static final Long MONTH = YEAR / 12; | |
| private static final Long[] INTERVAL = new Long[]{ | |
| YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND | |
| }; | |
| private static final String[] LABEL = new String[]{ | |
| "year", "month", "week", "day", "hour", "minute", "second" | |
| }; | |
| } | |
| private static final String PREFIX_IN = "in "; | |
| private static final String PREFIX_WAS = "was "; | |
| private static final String SUFFIX_AGO = " ago"; | |
| public static String from(final Calendar event, final boolean applyPrefix, final Long maxInterValue) throws DateTimeIntervalPhraserException { | |
| if (event == null) | |
| throw new DateTimeIntervalPhraserException("Event date cannot be null...."); | |
| if (!Arrays.asList(Values.INTERVAL).contains(maxInterValue)) | |
| throw new DateTimeIntervalPhraserException("MaxIntervalValue must be in DateTimeIntervalPhraser.Values.INTERVAL"); | |
| Calendar now = Calendar.getInstance(TimeZone.getTimeZone("utc")); | |
| if (event.before(now)) return formatPastDate(event, now, applyPrefix, maxInterValue); | |
| else return formatFutureDate(event, now, applyPrefix, maxInterValue); | |
| } | |
| private static String formatPastDate(Calendar event, Calendar now, boolean applyPrefix, final long max) { | |
| return getUnitAndValue(now.getTimeInMillis() - event.getTimeInMillis(), | |
| applyPrefix ? PREFIX_WAS : "", | |
| applyPrefix ? SUFFIX_AGO : "", max); | |
| } | |
| private static String formatFutureDate(Calendar event, Calendar now, boolean applyPrefix, final long max) { | |
| return getUnitAndValue(event.getTimeInMillis() - now.getTimeInMillis(), applyPrefix ? PREFIX_IN : "", "", max); | |
| } | |
| private static String getUnitAndValue(final long diff, final String prefix, final String suffix, final long max) { | |
| for (int i = 0; i < Values.INTERVAL.length; ++i) { | |
| final long interval = Values.INTERVAL[i]; | |
| final long val = diff / interval; | |
| if (interval == max || val >= 1) | |
| return getFormatted(Values.LABEL[i], val, prefix, suffix); | |
| } | |
| return ""; | |
| } | |
| private static String getFormatted(final String unit, final long count, final String prefix, final String suffix) { | |
| return prefix + String.valueOf(count) + " " + unit + (count != 1 ? "s" : "") + suffix; | |
| } | |
| public static class DateTimeIntervalPhraserException extends RuntimeException { | |
| public DateTimeIntervalPhraserException(String detailMessage) { | |
| super(detailMessage); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment