Search code examples
javaformatterlocaldatedatetimeformatter

How to format range of two LocalDate variables in Java?


I'm familiar with the DateTime Formatter in regards to applying LocalDate to a single variable, but don't know how to handle the following situation: I've two LocalDate variables, for example startOfWeek and endOfWeek. And I need to create String result with following rules:

  1. if startOfWeek and endOfWeek have same month and year, the result should be like 3 - 9 Jan 2021 (just an example with format)
  2. if startOfWeek and endOfWeek have not same month, the result should be like 31 Oct - 6 Nov 2021
  3. if startOfWeek and endOfWeek have not same year, the result should be like 27 Dec 2020 - 2 Jan 2021

I didn't find how to apply DateTime Formatter working with two of more LocalDate variables, so I've made this way:

if ((startOfWeek.getYear() != endOfWeek.getYear())){
                result.append(startOfWeek.getDayOfMonth() + " " + startOfWeek.getMonth() + " " + startOfWeek.getYear() +
                        " - " + endOfWeek.getDayOfMonth() + " " + endOfWeek.getMonth() + " " + startOfWeek.getYear());
                return  result.toString();
            } else if ((startOfWeek.getMonth() != endOfWeek.getMonth())) {
                result.append(startOfWeek.getDayOfMonth() + " " + startOfWeek.getMonth() +
                        " - " + endOfWeek.getDayOfMonth() + " " + endOfWeek.getMonth() + " " + startOfWeek.getYear());
                return  result.toString();
            } else {
                result.append(startOfWeek.getDayOfMonth() +
                        " - " + endOfWeek.getDayOfMonth() + " " + startOfWeek.getMonth() + " " + startOfWeek.getYear());
                return  result.toString();

But it return information about months in default format, for example SEPTEMBER

Full example:

Does anybody now how to fix month data format or how to use DateTime Formatter with two LocalDate variables at one time? Thank you in advance for your help.


Solution

  • You could use a DateTimeFormatter to get a formated month string

    DateTimeFormatter month  = DateTimeFormatter.ofPattern("MMM");
    

    In addition to that I would do the task with 3 DateTimeFormatters and the YearMonth and Year classes from java.time API which could make your if else blocks a bit more readable:

    import java.time.LocalDate;
    import java.time.Year;
    import java.time.YearMonth;
    import java.time.format.DateTimeFormatter;
    import java.util.List;
    
    ....
    
    
    static String formattedRange (LocalDate start, LocalDate end){
        DateTimeFormatter onlyDate      = DateTimeFormatter.ofPattern("d");
        DateTimeFormatter dateAndMonth  = DateTimeFormatter.ofPattern("d MMM");
        DateTimeFormatter dateMonthYear = DateTimeFormatter.ofPattern("d MMM yyyy");
    
        StringBuilder result = new StringBuilder();
        if (YearMonth.from(start).equals(YearMonth.from(end))){
            result.append(onlyDate.format(start)).append(" - ")
                  .append(dateMonthYear.format(end));
        }
        else if (Year.from(start).equals(Year.from(end))){
            result.append(dateAndMonth.format(start)).append(" - ")
                  .append(dateMonthYear.format(end));
        }
        else {
            result.append(dateMonthYear.format(start)).append(" - ")
                  .append(dateMonthYear.format(end));
        }
        return result.toString();
    }