This is the first article I participated in beginners’ introduction

preface

Sometimes we have a period of time, and we need to break it up into months. For example, the start time from 2020/07/15 to 2021/07/05 is divided into the following sections:

  1. 2020/07/15 ~ 2020/08/01
  2. 2020/08/01 ~ 2020/09/01
  3. 2020/09/01 ~ 2020/10/01
  4. .
  5. 2021/06/01 ~ 2021/07/01
  6. 2021/07/01 ~ 2021/07/05

I. Knowledge points

The classification of time is mainly based on the Calendar in Java, which is generally needed for time processing. You can use the Calendar class to set or get the time in the Calendar. Some common apis are as follows:

Initialize the Calendar object

Calendar calendar = Calendar.getInstance();
Copy the code

Set the time for the calendar

Date time = new Date;
calendar.setTime(time);
Copy the code

Get the time in the calendar (year month day)

calendar.set(Calendar.YEAR, 2021); / / year
calendar.add(Calendar.MONTH, 1); / / month
calendar.set(Calendar.DAY_OF_MONTH, 1); / /,
Copy the code

Second, train of thought analysis

The basic idea is to start at the start time and add increments month by month until the current time is greater than the end time.

Note that the end time needs to be added to the result set even if the current time is greater than the end time. Because the last period is from the beginning of the month to the end date.

Iii. Implementation scheme

The implementation is the same as in the idea, some comments are written in the code, as shown below:

/** * Divide a period of time by month **@paramStartTime startTime stamp (ms) *@paramEndTime endTime stamp (ms) */
public static List<Long> getIntervalTimeByMonth(Date startTime, Date endTime) {
    List<Long> dateList = new ArrayList<>();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(startTime);
    dateList.add(calendar.getTimeInMillis());
    while (calendar.getTimeInMillis() < endTime.getTime()) {
        / / end of the month
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        if(calendar.getTimeInMillis() >= endTime.getTime()){
            dateList.add(endTime.getTime());
            break;
        }
        / / early next month
        calendar.add(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        dateList.add(calendar.getTimeInMillis());
    }
    return dateList;
}
Copy the code

The test code looks like this:

public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date beginDate = sdf.parse("2020-07-15");
    Date endDate = sdf.parse("2021-07-05");
    List<Long> endDateList = DateUtil.getIntervalTimeByMonth(beginDate, endDate);
    for (int i = 0; i < endDateList.size(); i++) {
        Long beginStr = endDateList.get(i);
        String begin1 = sdf1.format(new Date(beginStr));
        System.out.println("The first" + i + "Time interval :"+ begin1); }}Copy the code

The test results are as follows:

Four,

This is my first post, thanks for watching! 😊