当前位置:网站首页>获取间隔的日期列表工具类
获取间隔的日期列表工具类
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);
}
}
边栏推荐
- System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。不可 访问的日志: Security
- FaceBook社媒营销高效转化技巧分享
- MySQL Advanced SQL Statements (2)
- SimpleChannelInboundHandler使用总结
- 8/1 思维+扩展欧几里得+树上dp
- typescript 'props' is declared but its value is never read solution
- 【暑期每日一题】洛谷 P1192 台阶问题
- [数据集][VOC]眼睛佩戴数据集VOC格式6000张
- punch day05
- [Dataset][VOC] Male and female dataset voc format 6188 sheets
猜你喜欢

Redis 常用命令和基本数据结构(数据类型)

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

rhce homework
![[Dataset][VOC] Eyewear dataset 6000 in VOC format](/img/66/37f76d9ce5d5f68d6ea0e18710fa04.png)
[Dataset][VOC] Eyewear dataset 6000 in VOC format

2022.07.31(LC_6132_使数组中所有元素都等于零)

有点奇怪!访问目的网址,主机能容器却不行

MySQL Advanced SQL Statements

实例026:递归求阶乘

Neo4j 中文开发者月刊 - 202207期

【npm install 报错问题合集】- npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
随机推荐
abaqus如何快速导入其他cae文件的assembly?
数据库概论之MySQL表的增删改查1
MySQL Advanced Statements (1)
See the picture to understand | How to choose sales indicators to measure the health of business growth
MySQL Advanced Study Notes
【暑期每日一题】洛谷 P1192 台阶问题
chrome plugin development guide
MySQL high-level statements (1)
张驰课堂:六西格玛测量系统的误差分析与判定
有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
数据库概论之MySQL表的增删改查2
正则表达式的理解学习
request.getSession(), the story
实例030:回文数
SimpleChannelInboundHandler使用总结
MySQL 5.7 installation tutorial (full-step, nanny-level tutorial)
About the local server problem after ue4.27 pixel streaming package
typescript 'props' is declared but its value is never read solution
解决C#非静态字段、方法或属性“islandnum.Program.getIslandCount(int[][], int, int)”要求对象引用
Nodejs installation and global configuration (super detailed)