Return the week, year, and day according to the incoming start time and end time

According to the start date and end date, the results returned by year, week, and day:

Member methods of the Calendar class
static Calendar getInstance() Get a calendar using the default timezone and locale. Calendar object is generated by this method. As follows: Calendar cr=Calendar.getInstance();
public void set(int year,int month,int date ,int hourofday,int minute,int second) Set the year, month, day, hour, minute, and second of the calendar.
public int get(int field) Returns the value of the given calendar field. The so-called fields are year, month, day and so on.
public void setTime(Date date) Sets the time for this calendar with the given Date. Date——Calendar
public Date getTime() Back A Date representing the time for this calendar. Calendar—–Date
abstract void add(int field,int amount) Adds or subtracts the amount of time to the specified field, according to the rules of the calendar.
public long getTimeInMillies() Returns the time value of this calendar in milliseconds.
time unit field
YEAR year MINUTE minute DAY_OF_WEEK_IN_MONTH
MONTH month SECOND/MILLISECOND seconds/milliseconds WEEK_OF_MONTH
DATE day DAY_OF_MONTH Same as Date DAY_OF_YEAR
HOUR_OF_DAY hour DAY_OF_WEEK Day of the week WEEK_OF_YEAR

query
DAY_OF_WEEK, Monday is 2, Sunday is 1

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class DateRange {
    // Format the year, month, day, hour, minute, and second
    public static String DATE_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String DATE_YYYY_MM = "yyyy-MM-dd";

    public static List getNextWeekDateList(String startDate, String endDate) throws ParseException {
        List<String> dateList = new ArrayList<>();
        List<String> list = new ArrayList<>();

        // format format
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_YYYY_MM_DD_HH_MM_SS);
        SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_YYYY_MM);
            //Convert string type to Date type
            Calendar start = Calendar. getInstance();
            Date startDate1 = sdf. parse(startDate);
            start.setTime(startDate1);

            Calendar end = Calendar. getInstance();
            Date endDate1 = sdf. parse(endDate);
            end.setTime(endDate1);

            //Calculate the difference in days between two days
            long time = (endDate1.getTime() - startDate1.getTime()) / 1000 / 3600 / 24;

            for (int i = 0; i <= time; i ++ ) {
                //Create a calendar instance
                Calendar start1 = Calendar. getInstance();
                start1.setTime(startDate1);
                start1.add(Calendar.DAY_OF_MONTH, i);
                //Clear the hours, minutes and seconds
                start1.set(Calendar.HOUR_OF_DAY, 00);
                start1.set(Calendar. MINUTE, 00);
                start1.set(Calendar.SECOND, 00);
                //store start time
                if (i == 0){
                    start1.setTimeInMillis(start.getTimeInMillis());
                }
                dateList.add(sdf.format(start1.getTime()));
                //Create end time
                Calendar end1 = Calendar. getInstance();
                end1.setTime(start1.getTime());
                //Clear the minutes and seconds to zero when the end time
                end1.set(Calendar.HOUR_OF_DAY, 23);
                end1.set(Calendar. MINUTE, 59);
                end1.set(Calendar.SECOND, 59);
                //deposit end time
                if (i == time){
                    end1.setTimeInMillis(end.getTimeInMillis());
                }
                dateList.add(sdf.format(end1.getTime()));
            }
            for (int i = 0; i<dateList. size(); i ++ ){
                Calendar calendar = Calendar. getInstance();
                calendar.setTime(sdf.parse(dateList.get(i)));
                Calendar calendar2 = Calendar. getInstance();
                //Judge whether the current time year is inconsistent with the next year
                if (i + 1 != dateList. size()){
                    calendar2.setTime(sdf.parse(dateList.get(i + 1)));
                    if (calendar.get(Calendar.YEAR) != calendar2.get(Calendar.YEAR)){
                        list.add(dateList.get(i));
                        list.add(dateList.get(i + 1));
                    }
                }
                // save start time
                list.add(dateList.get(0));
                //Judge whether it is Monday of this week, judge whether the start time and deadline are on the same day, judge whether the previous number is equal to the second number, if they are equal, stay
                //Monday is 2
                if (i > dateList.size() -14 & amp; & amp; calendar.get(Calendar.DAY_OF_WEEK) == 2 & amp; & amp; i + 1!=dateList.size() & amp; & amp; sdf1.parse(dateList.get(i)).equals(sdf1.parse(dateList.get(i+1)))){
                    //Judge whether Monday is the starting time, if not, save it to the day before Monday
                    if (i-1>=0){
                        list.add(dateList.get(i-1));
                    }
                    list.add(dateList.get(i));
                }
                //Today's date + previous day's date
                if (i == dateList.size() -2 || i == dateList.size() -1 || i == dateList.size() -3){
                    list.add(dateList.get(i));
                }
              }
        List<String> myList = list. stream(). distinct(). collect(Collectors. toList());
        List<DateInfo> list1 = new ArrayList<>();
        System.out.println(myList.size());
        for (int i = 0; i < myList. size() - 1; i ++ ) {
            list1. add(new DateInfo(myList. get(i), myList. get(i + 1)));
            i + + ;
        }
        return list1;
    }
 public static void main(String[] args) throws ParseException {
      List<DateInfo> list = DateRange.getNextWeekDateList("2022-12-09 12:12:12","2023-01-12 21:12:12");
      for (DateInfo l : list){
          System.out.println(l);
      }
    }
//result set
DateInfo(startDate=2022-12-09 12:12:12, endDate=2022-12-31 23:59:59)
DateInfo(startDate=2023-01-01 00:00:00, endDate=2023-01-08 23:59:59)
DateInfo(startDate=2023-01-09 00:00:00, endDate=2023-01-11 23:59:59)
DateInfo(startDate=2023-01-12 00:00:00, endDate=2023-01-12 21:12:12)

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeHome pageOverview 108557 people are studying systematically