Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I was disappointed with the other answers. Joda time didn't work in GWT and neither does SimpleDateFormat. Anyway, I already knew about DateTimeFormat in GWT. The main issue is that date objects getMonth() function is deprecated, and there doesn't seem to be a good way to compare months and/or years. This solution doesn't include year checking (which could be easily added by changing the monthFormatter), but that's not important for my case.</p> <pre><code>public final class DateUtility { public static final DateTimeFormat MONTH_FORMAT = DateTimeFormat.getFormat("MMM"); public static final DateTimeFormat DAY_FORMAT = DateTimeFormat.getFormat("dd"); public static final DateTimeFormat DAY_MONTH_FORMAT = DateTimeFormat.getFormat("dd MMM"); public static final String DASH = " - "; /** * Returns a "collapsed" date range String representing the period of time * between two Date parameters. Example: August 19 as a {@code startDate} * and August 30 as an {@code endDate} will return "19 - 30 AUG", August 28 * as a {@code startDate} and September 7 as an {@code endDate} will return * "28 AUG - 07 SEP". This means if you pass this two dates which cannot * collapse into a shorter form, then the longer form is returned. Years * are ignored, and the start and end dates are not checked to ensure we * are not going backwards in time (Aug 10 - July 04 is not checked). * * @param startDate * the start Date in the range. * @param endDate * the end Date in the range. * @return a String representation of the range between the two dates given. */ public static String collapseDate(Date startDate, Date endDate) { String formattedDateRange; // Get a comparison result to determine if the Months are the same String startDateMonth = MONTH_FORMAT.format(startDate); String endDateMonth = MONTH_FORMAT.format(endDate); if (startDateMonth.equals(endDateMonth)) { formattedDateRange = DAY_FORMAT.format(startDate) + DASH + DAY_MONTH_FORMAT.format(endDate).toUpperCase(); } else { // Months don't match, split the string across both months formattedDateRange = DAY_MONTH_FORMAT.format(startDate).toUpperCase() + DASH + DAY_MONTH_FORMAT.format(endDate).toUpperCase(); } return formattedDateRange; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload