How do you format a date range in Java?

I have two dates - the beginning and the end. I would like to format them so that if the months coincide, they collapse to something like “20-23 AUG” and still format correctly if they break through at the end of the month, for example, “20 SEP - 1 OCT”. Are there libraries for this, or will I need to pass code rules to display date ranges using separate DateFormats?

+3
source share
5 answers

I was disappointed with the other answers. Joda's time did not work in GWT, and SimpleDateFormat did not work either. Anyway, I already knew about DateTimeFormat in GWT. The main problem is that getMonth () date functions are deprecated and there seems to be no good way to compare months and / or years. This solution does not include checking the year (which can be easily added by changing the monthFormatter), but this is 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;
    }
}
+3
source

Here's a solution using JodaTime - the best library for working with dates in Java (the last time I checked). Formatting is simple and can no doubt be improved by using custom DateFormatter implementations. It also checks that the year is the same, but does not deduce the year, which can be confusing.

import org.joda.time.DateTime;

public class DateFormatterTest {

    public static void main(String[] args) {

        DateTime august23rd = new DateTime(2010, 8, 23, 0, 0, 0, 0);
        DateTime august25th = new DateTime(2010, 8, 25, 0, 0, 0, 0);
        DateTime september5th = new DateTime(2010, 9, 5, 0, 0, 0, 0);

        DateFormatterTest tester = new DateFormatterTest();
        tester.outputDate(august23rd, august25th);
        tester.outputDate(august23rd, september5th);

    }

    private void outputDate(DateTime firstDate, DateTime secondDate) {
        if ((firstDate.getMonthOfYear() == secondDate.getMonthOfYear()) && (firstDate.getYear() == secondDate.getYear())) {
            System.out.println(firstDate.getDayOfMonth() + " - " + secondDate.getDayOfMonth() + " " + firstDate.monthOfYear().getAsShortText());
        } else {
            System.out.println(firstDate.getDayOfMonth() + " " + firstDate.monthOfYear().getAsShortText() + " - " + secondDate.getDayOfMonth() + " " + secondDate.monthOfYear().getAsShortText());
        }
    }
}

:

23 - 25

23 - 5

+7

Checkout java SimpleDateFormat . SimpleDateFormat, .

+3

Jeroen , SimpleDateFormat , .

+1

, Java, DateFormats .

, , TimeSpan TimeDiff . , DateFormmater, .

0

Source: https://habr.com/ru/post/1761110/


All Articles