当前位置:网站首页>获取间隔的日期列表工具类
获取间隔的日期列表工具类
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);
}
}
边栏推荐
- Xgboost报错ValueError:无效的形状:标签(1650 2)
- MySQL driver jar package download -- nanny tutorial
- Detailed explanation of 9 common reasons for MySQL index failure
- Project development specification
- 实验8 VLAN综合实验
- Revitalize rural circular economy and digital chain to link agricultural "ecological chain"
- Specified URL is not reachable,caused by :‘Read timed out
- 武汉高性能计算大会2022举办,高性能计算生态发展再添新动力
- Dataset: A detailed guide to the download link collection of commonly used datasets in machine learning
- chrome 插件开发指南
猜你喜欢
![[Dataset][VOC] Male and female dataset voc format 6188 sheets](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[Dataset][VOC] Male and female dataset voc format 6188 sheets

Pagoda+FastAdmin 404 Not Found

Toolbox App 1.25 New Features at a Glance | Version Update

HCIP day 3 experiment

实例026:递归求阶乘

aTrust项目的相关操作与分享

rhce homework

See the picture to understand | How to choose sales indicators to measure the health of business growth

The second day HCIP

张驰课堂:六西格玛测量系统的误差分析与判定
随机推荐
Vscode connect to remote server "Acquiring the lock on the/home / ~ 'problem
解决:- SPY: No data found for this date range, symbol may be delisted报错
Dataset: A detailed guide to the download link collection of commonly used datasets in machine learning
MySQL classic 50 practice questions and the most detailed analysis of the whole network
Resolving C# non-static field, method or property "islandnum.Program.getIslandCount(int[][], int, int)" requires an object reference
有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
能与观众实时互动的Claper
How does abaqus quickly import the assembly of other cae files?
See the picture to understand | How to choose sales indicators to measure the health of business growth
Leetcode Weekly 304
【论文精读】Geometric Structure Preserving Warp for Natural Image Stitching
chrome 插件开发指南
数据库概论之MySQL表的增删改查2
MySQL Advanced SQL Statements (2)
【npm install 报错问题合集】- npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
HCIP 第一天
Xgboost报错ValueError:无效的形状:标签(1650 2)
yml字符串读取时转成数字了怎么解决
(部分不懂,笔记整理未完成)【图论】差分约束
MySQL - Multi-table query and case detailed explanation