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);
// 从datetime到weekEnd变更为上一月的第四周
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);
// 每年1月1日不在周一,则周跨月跨年了
richDate.setSeasonOfYear(4);// 每年的第四季度
richDate.setMonthOfSeason(3);// 第四季度的第三个月
richDate.setWeekOfMonth(4);
richDates.add(richDate);
}
// 从0开始计数
int nums = 0;
// 以下三个变量设置为-1,代表没有具体业务含义的初始值
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);
}
}
周月季年换算(按每年48周)
最新推荐文章于 2023-03-02 09:37:38 发布