1人参与 • 2026-03-19 • Java
这是一个静态工具方法,用于获取两个日期之间的所有日期(不包含起始日期,包含结束日期?需要确认)。方法接收开始日期和结束日期两个参数,返回这两个日期之间的所有日期的 list 集合。
/**
* 获取两个日期之间的所有日期的开始时间集合
* 返回date的list
*/
public static list<date> getbetweendates(date start, date end){
list<date> result = new arraylist<>();
calendar tempstart = calendar.getinstance();
tempstart.settime(start);
tempstart.add(calendar.day_of_year,1);
calendar tempend = calendar.getinstance();
tempend.settime(end);
while (tempstart.before(tempend)){
result.add(tempstart.gettime());
tempstart.add(calendar.day_of_year, 1);
}
return result;
}
结果展示
//获取两个日期之间的所有日期的开始时间集合 date begindate = new date(); begindate.settime(1652976000000l); //2022-05-20 00:00:00 date enddate = new date(); enddate.settime(1655654400000l); //2022-06-20 00:00:00 system.out.println(getbetweendates(begindate, enddate)); //[sat may 21 00:00:00 cst 2022, sun may 22 00:00:00 cst 2022, mon may 23 00:00:00 cst 2022, tue may 24 00:00:00 cst 2022, wed may 25 00:00:00 cst 2022, thu may 26 00:00:00 cst 2022, fri may 27 00:00:00 cst 2022, sat may 28 00:00:00 cst 2022, sun may 29 00:00:00 cst 2022, mon may 30 00:00:00 cst 2022, tue may 31 00:00:00 cst 2022, wed jun 01 00:00:00 cst 2022, thu jun 02 00:00:00 cst 2022, fri jun 03 00:00:00 cst 2022, sat jun 04 00:00:00 cst 2022, sun jun 05 00:00:00 cst 2022, mon jun 06 00:00:00 cst 2022, tue jun 07 00:00:00 cst 2022, wed jun 08 00:00:00 cst 2022, thu jun 09 00:00:00 cst 2022, fri jun 10 00:00:00 cst 2022, sat jun 11 00:00:00 cst 2022, sun jun 12 00:00:00 cst 2022, mon jun 13 00:00:00 cst 2022, tue jun 14 00:00:00 cst 2022, wed jun 15 00:00:00 cst 2022, thu jun 16 00:00:00 cst 2022, fri jun 17 00:00:00 cst 2022, sat jun 18 00:00:00 cst 2022, sun jun 19 00:00:00 cst 2022]
这是一个静态工具方法,用于获取两个日期之间所有月份的集合。方法接收开始日期和结束日期两个参数,返回这两个日期之间的每个月的第一天组成的 list 集合。
start:开始日期(包含该日期所在的月份)
end:结束日期(不包含该日期所在的月份)
初始化开始日历:将 tempstart 设置为 start 日期所在月份的第一天(日期设为1号)
初始化结束日历:将 tempend 设置为 end 日期所在月份的第二天(日期设为2号,这是一个巧妙的设计)
循环遍历月份:只要 tempstart 在 tempend 之前,就将当前月份的第一天加入结果集,然后月份+1继续循环
/**
* 获取两个日期之间所有月份的集合
*/
public static list<date> getbetweenmonth(date start, date end){
list<date> result = new arraylist<>();
calendar tempstart = calendar.getinstance();
tempstart.settime(start);
tempstart.set(tempstart.get(calendar.year), tempstart.get(calendar.month), 1);
calendar tempend = calendar.getinstance();
tempend.settime(end);
tempend.set(tempend.get(calendar.year), tempend.get(calendar.month), 2);
while (tempstart.before(tempend)){
result.add(tempstart.gettime());
tempstart.add(calendar.month,1);
}
return result;
}结果展示
system.out.println(getbetweenmonth(begindate, enddate)); //[sun may 01 00:00:00 cst 2022, wed jun 01 00:00:00 cst 2022]
1.获取两个字符串日期之间所有日期的集合
/**
* 获取两个日期字符串之间的日期集合
* @param starttime:string
* @param endtime:string
* @return list:yyyy-mm-dd
*/
public static list<string> getbetweendate(string starttime, string endtime){
simpledateformat sdf = new simpledateformat(ymd);
// 声明保存日期集合
list<string> list = new arraylist<string>();
try {
// 转化成日期类型
date startdate = sdf.parse(starttime);
date enddate = sdf.parse(endtime);
//用calendar 进行日期比较判断
calendar calendar = calendar.getinstance();
while (startdate.gettime()<=enddate.gettime()){
// 把日期添加到集合
list.add(sdf.format(startdate));
// 设置日期
calendar.settime(startdate);
//把日期增加一天
calendar.add(calendar.date, 1);
// 获取增加后的日期
startdate=calendar.gettime();
}
} catch (parseexception e) {
e.printstacktrace();
}
return list;
}2.获取两个字符串日期之间所有的月份集合
/**
* 获取两个日期之间所有的月份集合
* @param starttime
* @param endtime
* @return:yyyy-mm
*/
public static list<string> getmonthbetweendate(string starttime, string endtime){
simpledateformat sdf = new simpledateformat(yyyy_mm);
// 声明保存日期集合
list<string> list = new arraylist<string>();
try {
// 转化成日期类型
date startdate = sdf.parse(starttime);
date enddate = sdf.parse(endtime);
//用calendar 进行日期比较判断
calendar calendar = calendar.getinstance();
while (startdate.gettime()<=enddate.gettime()){
// 把日期添加到集合
list.add(sdf.format(startdate));
// 设置日期
calendar.settime(startdate);
//把日期增加一天
calendar.add(calendar.month, 1);
// 获取增加后的日期
startdate=calendar.gettime();
}
} catch (parseexception e) {
e.printstacktrace();
}
return list;
}到此这篇关于java实现获取两个日期之间的所有日期的开始时间集合的文章就介绍到这了,更多相关java获取指定日期集合内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论