当前位置:网站首页>获取间隔的日期列表工具类
获取间隔的日期列表工具类
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);
}
}
边栏推荐
- Py's mlxtend: a detailed guide to the introduction, installation, and usage of the mlxtend library
- Nodejs installation and global configuration (super detailed)
- Specified URL is not reachable,caused by :‘Read timed out
- Connection reset by peer problem analysis
- 实例031:字母识词
- 2022.07.31(LC_6132_使数组中所有元素都等于零)
- Pagoda+FastAdmin 404 Not Found
- Dataset: A detailed guide to the download link collection of commonly used datasets in machine learning
- chrome 插件开发指南
- 交换部分 VLAN
猜你喜欢
【npm install 报错问题合集】- npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
Toolbox App 1.25 New Features at a Glance | Version Update
optional
实验8 VLAN综合实验
HCIP 第一天
Vscode connect to remote server "Acquiring the lock on the/home / ~ 'problem
Revitalize rural circular economy and digital chain to link agricultural "ecological chain"
Redis 常用命令和基本数据结构(数据类型)
实验7 MPLS实验
有点奇怪!访问目的网址,主机能容器却不行
随机推荐
Unity Shader学习(七)纹理图像的简单使用
【论文精读】Geometric Structure Preserving Warp for Natural Image Stitching
Leetcode Weekly 304
Specified URL is not reachable,caused by :‘Read timed out
typescript 'props' is declared but its value is never read solution
Neo4j 中文开发者月刊 - 202207期
实验7 MPLS实验
Reverse resolve dns server
关于ue4.27像素流送打包后的本地服务器问题
FaceBook社媒营销高效转化技巧分享
C# Coding Conventions Handbook
笔记本开机黑屏提示:ERROR 0199:System Security-Security password retry count exceeded
CAT1 4G+Ethernet development board Tencent cloud mobile phone WeChat applet display temperature and delivery control
optional
MySQL classic 50 practice questions and the most detailed analysis of the whole network
MySQL high-level statements (1)
实例028:递归求等差数列
Revitalize rural circular economy and digital chain to link agricultural "ecological chain"
Submit code process
Detailed explanation of 9 common reasons for MySQL index failure