java.time
How to find the cut dates:
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("d-[MMMM][MMM]-uuuu", Locale.ENGLISH);
String inputStartDate = "5-Jan-2019";
String inputEndDate = "13-March-2019";
LocalDate startDate = LocalDate.parse(inputStartDate, inputFormatter);
LocalDate endDate = LocalDate.parse(inputEndDate, inputFormatter);
LocalDate[] cutDates = startDate.datesUntil(endDate, Period.ofMonths(1))
.toArray(LocalDate[]::new);
System.out.println("Cut dates: " + Arrays.toString(cutDates));
Output is:
Cut dates: [2019-01-05, 2019-02-05, 2019-03-05]
Now you can query your Oracle database about each interval from one cut date inclusive to the next cut date exclusive, and finally from the last cut date to your end date (inclusive or exclusive depending on your requirements).
The result may surprise a bit if start date is 29, 30 or 31 of a month. For example:
String inputStartDate = "31-Jan-2019";
String inputEndDate = "9-April-2019";
Cut dates: [2019-01-31, 2019-02-28, 2019-03-31]
One challenge was parsing your dates since it seems you sometimes get a month abbreviation (Jan rather than January), sometimes a full month name (March, not Mar). The square brackets in the format pattern string denote optional parts, so our formatter will accept either a full month name through MMMM
or an abbreviation through MMM
.
For how to pass your LocalDate
objects to SQL see for example the link at the bottom.
Links