数据库create_date数据格式为yyyyMMdd,图标统计需要查找日期等于最近7天的数据,条件如下
select * from xx_table where create_date >= smallDay and create_date <= maxDay;
获取时间集合方法如下
package com.zcjlq.hutool;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
public class DateUtilTest {
public static void main(String[] args) {
// 需求:获取最近n天日期,格式为yyyyMMdd的long集合
List<String> daysList = getDaysList(-7);
// 20231218
//20231219
//20231220
//20231221
//20231222
//20231223
//20231224
daysList.forEach(System.out::println);
String smallDay = daysList.get(0);
String maxDay = daysList.get(daysList.size() - 1);
System.out.printf("最小日期%s,最大日期%s%n", smallDay, maxDay);
// select * from xx_table where create_date >= smallDay and create_date <= maxDay;
}
/**
* 包含当日
*
* @param offset 偏移量
* @return
*/
private static List<String> getDaysList(int offset) {
DateTime nDaysBefore = DateUtil.offsetDay(new Date(), offset + 1);
List<DateTime> dateTimes = DateUtil.rangeToList(nDaysBefore, new Date(), DateField.DAY_OF_YEAR);
List<String> yyyyMMdd = dateTimes.stream().map(a -> DateUtil.format(a, "yyyyMMdd")).collect(Collectors.toList());
return yyyyMMdd;
}
}
标签:create,hutool,时间,yyyyMMdd,daysList,date,import,工具
From: https://blog.51cto.com/u_15621957/8957015