java查询两个时间戳之间包含的日期
时间: 2024-05-06 13:19:53 浏览: 143
以下是Java代码示例,可以查询两个时间戳之间包含的日期:
```java
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class DateUtil {
/**
* 获取两个时间戳之间包含的日期列表
* @param startTimestamp 开始时间戳
* @param endTimestamp 结束时间戳
* @return 包含的日期列表
*/
public static List<String> getDateList(long startTimestamp, long endTimestamp) {
List<String> dateList = new ArrayList<>();
// 将时间戳转换为日期
Date startDate = new Date(startTimestamp);
Date endDate = new Date(endTimestamp);
// 获取日历实例
Calendar calendar = Calendar.getInstance();
// 将日历设置为开始日期
calendar.setTime(startDate);
// 循环添加日期
while (calendar.getTime().before(endDate) || calendar.getTime().equals(endDate)) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
dateList.add(format.format(calendar.getTime()));
// 将日历日期加1天
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
return dateList;
}
}
```
使用示例:
```java
public static void main(String[] args) {
long startTimestamp = 1622505600000L; // 2021-06-01 00:00:00
long endTimestamp = 1625097600000L; // 2021-07-01 00:00:00
List<String> dateList = DateUtil.getDateList(startTimestamp, endTimestamp);
for (String date : dateList) {
System.out.println(date);
}
}
```
输出结果:
```
2021-06-01
2021-06-02
2021-06-03
2021-06-04
2021-06-05
2021-06-06
2021-06-07
2021-06-08
2021-06-09
2021-06-10
2021-06-11
2021-06-12
2021-06-13
2021-06-14
2021-06-15
2021-06-16
2021-06-17
2021-06-18
2021-06-19
2021-06-20
2021-06-21
2021-06-22
2021-06-23
2021-06-24
2021-06-25
2021-06-26
2021-06-27
2021-06-28
2021-06-29
2021-06-30
2021-07-01
```
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)