-2

Here Java need to read a file which has Contract, Member, Start-Date and End-Date. If there are date split/break in dates it need to be populated in a target file at contract level. Below provided the Source file and Target file example and the expected result.

In the example, Contract A has the split in Member 3, so in Target file the remaining date range need to populated at contract level

Contract B has the split in Member 3 and 4, so in Target file the missing date range and remaining date range need to populated at contract level.

Source File:

Contract  Member    StartDate     EndDate
A           1      01-Jan-2020   31-Dec-2020
A           2      01-Jan-2020   31-Dec-2020
A           3      01-Jan-2020   24-Oct-2020
A           4      01-Jan-2020   31-Dec-2020
                                 
B           1      01-Jan-2020   31-Dec-2020
B           2      01-Jan-2020   31-Dec-2020
B           3      01-Jan-2020   04-Mar-2020
B           3      01-Apr-2020   31-Dec-2020
B           4      01-Jan-2020   04-Mar-2020
B           4      01-Apr-2020   31-Dec-2020

Target File:

Contract   StartDate      EndDate
A        01-Jan-2020    24-Oct-2020
A        25-Oct-2020    31-Dec-2020
                        
B        01-Jan-2020    04-Mar-2020
B        05-Mar-2020    31-Mar-2020
B        01-Apr-2020    31-Dec-2020
peterh
  • 11,875
  • 18
  • 85
  • 108
  • 2
    What is the logic of this ranges ? – Youcef LAIDANI Dec 16 '20 at 11:10
  • 1
    Well, write some code. E.g. check if the end of one range matches the start of the next range, if not then you need to calculate the missing range. There are a couple of date apis available for parsing and comparing dates. – luk2302 Dec 16 '20 at 11:11
  • Are the given ranges required to be non-overlapping (as they are in the example)? – Ole V.V. Dec 16 '20 at 13:02
  • 1
    Welcome on the StackOverflow! Your questions looks like a "do my task for free" problem. You need to write the code and ask the questions only if you find a problem. – peterh Dec 18 '20 at 11:25

1 Answers1

0

The next range starts one day after the last date of the current range. You can iterate the list of date range strings and find the consecutive ranges which do not obey this rule. At this point, create the missing range using this rule and break the loop.

Demo:

import java.time.LocalDate;
import java.util.List;

class Main {
    public static void main(String[] args) {
        List<String> dateRangeList = List.of("2019-01-01 2019-02-01", "2019-02-02 2019-04-04", "2019-06-01 2019-07-01");
        for (int i = 0; i < dateRangeList.size() - 1; i++) {
            String[] currentRange = dateRangeList.get(i).split(" ");
            String[] nextRange = dateRangeList.get(i + 1).split(" ");
            LocalDate oneDayAfterLastDateOfCurrentRange = LocalDate.parse(currentRange[1]).plusDays(1);
            LocalDate firstDateOfNextRange = LocalDate.parse(nextRange[0]);
            if (!oneDayAfterLastDateOfCurrentRange.equals(firstDateOfNextRange)) {
                String missingDateRange = oneDayAfterLastDateOfCurrentRange.toString() + " "
                        + firstDateOfNextRange.minusDays(1);
                System.out.println("The missing date range is: " + missingDateRange);
                break;
            }
        }
    }
}

Output:

The missing date range is: 2019-04-05 2019-05-31
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110