Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a date into equal intervals based on a given frequency

Tags:

java

apex

I'm trying to break a range of dates into equal intervals. Here is the code that I'm using. (Dates are in YYYY-MM-dd format)

    Integer frequency= 4;
    Date startDate = '2020-02-27';
    Date endDate = '2022-02-26';
    String[] startD = string.valueOf(behadelPlan.Start_datum__c).split('-');
    String[] endD = string.valueOf(behadelPlan.Einddatum__c).split('-');

    //String[] dates;
    Integer x = 0;
    Integer startYear = Integer.valueof(startD[0]);
    Integer endYear = Integer.valueof(endD[0]);
    //while (x < 4) {
    for (Integer i = startYear; i <= endYear; i++) {
        Integer endMonth = i != endYear ? 11 : Integer.valueof(endD[1]) - 1;
        Integer startMon = i == startYear ? Integer.valueof(startD[1]) - 1 : 0;
        for (Integer j = startMon; j <= endMonth  && x < frequency; j = j + 1) {
            Integer month = j + 1;
            String displayMonth;
            if (month < 10) {
                displayMonth = '0' + month;
            } else {
                displayMonth = String.valueOf(month);
            }
            List<string> slist = new string[]{string.valueOf(i), displayMonth, '01'};
                string allstring = string.join(sList,'-');
           System.debug(slist);
            x+=1;
        }

    }

when I run this I get the output as

2020-02-01
2020-03-01
2020-04-01
2020-05-01

Here In my case, I want to generate the dates at equal(monthly with the day being start dates day) intervals. In the above example, I should be getting the resultant dates as 4 equal intervals (as I've given my frequency in a number of months).

Please let me know on how can I achieve this.

Here is a simple example.

startdate = '2020-01-01'
endDate = '2020-12-01'
frequency = 4

output should be

2020-01-01
2020-03-01
2020-06-01
2020-09-01
like image 372
user3872094 Avatar asked Feb 02 '26 02:02

user3872094


1 Answers

Sample code ,

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SOTest {

    static DateFormat dateFormat = new SimpleDateFormat("yyy-MM-dd");

    public static void main(String[] args) {
        try {
            String startDateString = "2020-01-01";
            String endDateString = "2020-12-01";

            int frequency = 4;
            Date startDate = (Date)dateFormat.parse(startDateString);
            Date endDate = (Date)dateFormat.parse(endDateString);
            Long intervalSize = (endDate.getTime()-startDate.getTime())/frequency;

            for(int i=0; i<= frequency && intervalSize > 0; i++) {
                Date date = new Date(startDate.getTime()+intervalSize*i);
                System.out.println("Date :: "+dateFormat.format(date));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }   
    }
}

output for above ::

Date :: 2020-01-01
Date :: 2020-03-24
Date :: 2020-06-16
Date :: 2020-09-08
Date :: 2020-12-01

For input :: startDate = '2020-02-27' , endDate = '2022-02-26', interval = 4

Date :: 2020-02-27
Date :: 2020-08-27
Date :: 2021-02-26
Date :: 2021-08-27
Date :: 2022-02-26
like image 89
Amit Kumar Lal Avatar answered Feb 04 '26 15:02

Amit Kumar Lal