package com.example.dateinvestigate;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.*;
@SpringBootTest
class DateInvestigateApplicationTests {
@Test
void contextLoads() {
}
@Test
void temp(){
DateTime start = DateTime.of("2022-04-01", "yyyy-MM-dd");
DateTime end = DateTime.of("2022-04-30", "yyyy-MM-dd");
System.out.println(start.weekOfMonth());
System.out.println(end.weekOfMonth());
}
List<DateTime> dateSplit(DateTime startDate, DateTime endDate){
Long spi = endDate.getTime() - startDate.getTime();
Long step = spi / (24 * 60 * 60 * 1000);
List<DateTime> dateList = new ArrayList();
dateList.add(endDate);
for (int i = 1; i <= step; i++) {
dateList.add(new DateTime(dateList.get(i - 1).getTime()- (24 * 60 * 60 * 1000)));
}
Collections.reverse(dateList);
return dateList;
}
@Test
void iterOneYear(){
DateTime start = DateTime.of("2022-01-01", "yyyy-MM-dd");
DateTime end = DateTime.of("2022-12-31", "yyyy-MM-dd");
List<DateTime> dateTimes = dateSplit(start,end);
List<DateTime> lastDates = Lists.newArrayList();
if(start.dayOfMonth() == 1 && start.dayOfWeekEnum()!=Week.MONDAY && start.month() == 0){
int dayWeek = start.dayOfWeek();
if(dayWeek==1){
dayWeek = 8;
}
DateTime weekBegin = DateUtil.offset(start, DateField.DAY_OF_WEEK, start.getFirstDayOfWeek().getValue() - dayWeek);
DateTime weekEnd = DateUtil.offset(weekBegin, DateField.DAY_OF_WEEK, 6);
lastDates = dateSplit(start,weekEnd);
}
List<DateTime> allYear = dateSplit(start, end);
List<DateTime> lastYear = allYear.subList(0, lastDates.size());
List<DateTime> nowYear = allYear.subList(lastDates.size(), allYear.size());
List<RichDate> richDates = Lists.newArrayList();
for (DateTime dateTime : lastYear) {
RichDate richDate = new RichDate();
richDate.setDate(dateTime.toDateStr());
int weekDay = dateTime.dayOfWeek();
if(weekDay == 1){
weekDay = 7;
}else{
weekDay = weekDay - 1;
}
richDate.setWeekday(weekDay);
richDate.setSeasonOfYear(4);
richDate.setMonthOfSeason(3);
richDate.setWeekOfMonth(4);
richDates.add(richDate);
}
int nums = 0;
int weekOfMonth = -1;
int monthOfSeason = -1;
int seasonOfYear = -1;
for (DateTime dateTime : nowYear) {
RichDate richDate = new RichDate();
richDate.setDate(dateTime.toDateStr());
int weekDay = dateTime.dayOfWeek();
if(weekDay == 1){
weekDay = 7;
}else{
weekDay = weekDay - 1;
}
richDate.setWeekday(weekDay);
weekOfMonth = (nums/7)%4+1 ;
richDate.setWeekOfMonth(weekOfMonth);
monthOfSeason = (nums%(3*28)/28)+1;
richDate.setMonthOfSeason(monthOfSeason);
seasonOfYear = (nums/(3*28))%4+1;
richDate.setSeasonOfYear(seasonOfYear);
richDates.add(richDate);
nums++;
}
richDates.forEach(System.out::println);
}
}
|