当前位置:网站首页>获取间隔的日期列表工具类
获取间隔的日期列表工具类
2022-08-02 06:21:00 【蓝天⊙白云】
public class RangeDateUtil {
/** * 获取间隔的月份列表 * @param preYear 开始月份 * @param endYear 截止月份 */
public static List<YearMonth> getRangeYears(YearMonth preYear, YearMonth endYear) {
List<YearMonth> years = new ArrayList<>();
// 间隔的月数
long betweenYears = ChronoUnit.MONTHS.between(preYear, endYear);
if (betweenYears < 1) {
// 开始日期<=截止日期
return years;
}
// 创建一个从开始日期、每次加一个月的无限流,限制到截止月份为止
Stream.iterate(preYear, c -> c.plusMonths(1))
.limit(betweenYears + 1)
.forEach(years::add);
return years;
}
/** * 获取指定月份前的指定月数的日期列表 * @param endYear 截止月份 * @param betweenYears 间隔月数 */
public static List<YearMonth> getPreRangeYears(YearMonth endYear, int betweenYears) {
YearMonth preYear = endYear.minusYears(betweenYears);
return getRangeYears(preYear, endYear);
}
/** * 获取指定月份前的指定月数的日期列表 * @param preYear 开始月份 * @param betweenYears 间隔月数 */
public static List<YearMonth> getEndRangeYears(YearMonth preYear, int betweenYears) {
YearMonth endYear = preYear.plusMonths(betweenYears);
return getRangeYears(preYear, endYear);
}
/** * 获取间隔的日期列表 * @param preDate 开始日期 * @param endDate 截止日期 */
public static List<LocalDate> getRangeDays(LocalDate preDate, LocalDate endDate) {
List<LocalDate> dates = new ArrayList<>();
// 间隔的天数
long betweenDays = ChronoUnit.DAYS.between(preDate, endDate);
if (betweenDays < 1) {
// 开始日期<=截止日期
return dates;
}
// 创建一个从开始日期、每次加一天的无限流,限制到截止日期为止
Stream.iterate(preDate, c -> c.plusDays(1))
.limit(betweenDays + 1)
.forEach(dates::add);
return dates;
}
/** * 获取指定日期前的指定天数的日期列表 * @param endDate 截止日期 * @param betweenDays 间隔天数 */
public static List<LocalDate> getPreRangeDays(LocalDate endDate, int betweenDays) {
LocalDate preDate = endDate.minusDays(betweenDays);
return getRangeDays(preDate, endDate);
}
/** * 获取指定日期后的指定天数的日期列表 * @param preDate 开始日期 * @param betweenDays 间隔天数 */
public static List<LocalDate> getEndRangeDays(LocalDate preDate, int betweenDays) {
LocalDate endDate = preDate.plusDays(betweenDays);
return getRangeDays(preDate, endDate);
}
/** * 获取间隔的日期列表 * @param preDate 开始日期 * @param endDate 截止日期 */
public static List<Date> getRangeDays(Date preDate, Date endDate) {
List<Date> dates = new ArrayList<>();
// 间隔的天数
int betweenDays = (int) ((endDate.getTime() - preDate.getTime()) / (1000*3600*24));
if (betweenDays < 1) {
// 开始日期<=截止日期
return dates;
}
// 创建一个从开始日期、每次加一天的无限流,限制到截止日期为止
Stream.iterate(preDate, c -> DateUtil.plusDays(c, 1))
.limit(betweenDays + 1)
.forEach(dates::add);
return dates;
}
/** * 获取指定日期前的指定天数的日期列表 * @param endDate 截止日期 * @param betweenDays 间隔天数 */
public static List<Date> getPreRangeDays(Date endDate, int betweenDays) {
Date preDate = DateUtil.minusDays(endDate, betweenDays);
return getRangeDays(preDate, endDate);
}
/** * 获取指定日期后的指定天数的日期列表 * @param preDate 开始日期 * @param betweenDays 间隔天数 */
public static List<Date> getEndRangeDays(Date preDate, int betweenDays) {
Date endDate = DateUtil.plusDays(preDate, betweenDays);
return getRangeDays(preDate, endDate);
}
/** * 获取间隔的月份列表 * @param preYear 开始月份(yyyy-MM格式) * @param endYear 截止月份(yyyy-MM格式) */
public static List<YearMonth> getRangeYears(String preYear, String endYear) {
YearMonth pDate = YearMonth.parse(preYear);
YearMonth eDate = YearMonth.parse(endYear);
return getRangeYears(pDate, eDate);
}
/** * 获取间隔的日期列表 * @param preDate 开始日期(yyyy-MM-dd格式) * @param endDate 截止日期(yyyy-MM-dd格式) */
public static List<LocalDate> getRangeDays(String preDate, String endDate) {
LocalDate pDate = LocalDate.parse(preDate);
LocalDate eDate = LocalDate.parse(endDate);
return getRangeDays(pDate, eDate);
}
}
边栏推荐
- 解决C#非静态字段、方法或属性“islandnum.Program.getIslandCount(int[][], int, int)”要求对象引用
- [Dataset][VOC] Eyewear dataset 6000 in VOC format
- HCIP 第三天实验
- ASP.NET Core Web API 幂等性
- (部分不懂,笔记整理未完成)【图论】差分约束
- 宝塔+FastAdmin 404 Not Found
- MySQL - Multi-table query and case detailed explanation
- [Dataset][VOC] Male and female dataset voc format 6188 sheets
- chrome 插件开发指南
- 笔记本开机黑屏提示:ERROR 0199:System Security-Security password retry count exceeded
猜你喜欢

实验8 VLAN综合实验

论文《Deep Multifaceted Transformers for Multi-objective Ranking in Large-Scale E-commerce Recommender》

Go inside the basic knowledge

MySQL Advanced Study Notes

8/1 思维+扩展欧几里得+树上dp

optional

How does abaqus quickly import the assembly of other cae files?

File upload vulnerability (2)

正则表达式的理解学习

实验7 MPLS实验
随机推荐
ue先视频教程后深入
【暑期每日一题】洛谷 P1551 亲戚
System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。不可 访问的日志: Security
PHP Warning: putenv() has been disabled for security reasons in phar
Launch Space on-premises deployment (local) Beta!
nacos源码启动找不到istio包
Resolving C# non-static field, method or property "islandnum.Program.getIslandCount(int[][], int, int)" requires an object reference
typescript 'props' is declared but its value is never read solution
实例027:递归输出
Servlet
Leetcode周赛304
MySQL Advanced SQL Statements
2022年8月计划,着重ue4视频教程
Vscode connect to remote server "Acquiring the lock on the/home / ~ 'problem
HCIP day one
yml字符串读取时转成数字了怎么解决
PMP新考纲通关秘籍,告别抓瞎
MySql - there is no insert, there is update or ignored
数据库概论之MySQL表的增删改查1
实验8 VLAN综合实验