Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3217334
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:23:56+00:00 2026-05-17T15:23:56+00:00

I have two dates – a start and an end. I’d like to format

  • 0

I have two dates – a start and an end. I’d like to format them so that if the months match, they collapse to something like “20-23 AUG” and still format correctly if they break over the end of the month, like “20 SEP – 1 OCT”. Are there any libraries out there for accomplishing this, or would I have to hand code rules for displaying ranges of dates using individual DateFormats?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:23:57+00:00Added an answer on May 17, 2026 at 3:23 pm

    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.

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two dates in the format below: Start Date = 30-10-2009 End Date
I have two dates of the form: Start Date: 2007-03-24 End Date: 2009-06-26 Now
I have two dates - a start date and an end date. I need
I would like to compare two dates in javascript. I have been doing some
I have two Ajax Toolkit calendar extenders. One of them is a start date
I have two dates in format(MM/dd/yyyy hh:mm:ss:SS). For the both dates I have converted
I have two dates 18-Aug-2010 and 19-Aug-2010 of this format. How to find whether
I have two dates, formated like "Y-m-d H:i:s". I need to compare these two
I have two dates: 10-11-2010 and 17-11-2010 Now i would like to SELECT all
I have two dates (parsed using str2time ). How can I tell if one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.