当前位置:网站首页>获取间隔的日期列表工具类
获取间隔的日期列表工具类
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);
}
}
边栏推荐
- 笔记本开机黑屏提示:ERROR 0199:System Security-Security password retry count exceeded
- About the local server problem after ue4.27 pixel streaming package
- APP special test: traffic test
- [npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
- nacos源码启动找不到istio包
- punch day05
- MySQL Advanced - MVCC (ultra-detailed finishing)
- 【暑期每日一题】洛谷 P1192 台阶问题
- odoo field 设置匿名函数domain
- pointer arithmetic in c language
猜你喜欢
随机推荐
(部分不懂,笔记整理未完成)【图论】差分约束
2022.07.31(LC_6132_使数组中所有元素都等于零)
nacos源码启动找不到istio包
武汉高性能计算大会2022举办,高性能计算生态发展再添新动力
Dataset: A detailed guide to the download link collection of commonly used datasets in machine learning
MySQL Advanced SQL Statements (2)
SimpleChannelInboundHandler使用总结
APP special test: traffic test
速看!PMP新考纲、PMBOK第七版解读
Dataset:机器学习中常用数据集下载链接集合之详细攻略
堡垒机、堡垒机的原理
实例026:递归求阶乘
Vscode connect to remote server "Acquiring the lock on the/home / ~ 'problem
Leetcode Weekly 304
File upload vulnerability (2)
享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
Specified URL is not reachable,caused by :‘Read timed out
Vscode连接远程服务器出现‘Acquiring lock on/home/~’问题
实例027:递归输出




![[数据集][VOC]男女数据集voc格式6188张](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)




