轉自:https://www.jb51.net/article/162132.htm
本文實例為大家分享了java獲取指定開始時間與結束時間之間的所有日期的具體代碼,供大家參考,具體內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import
java.text.SimpleDateFormat;
import
java.util.Calendar;
public
class
TimerTest {
public
static
void
main(String[] args)
throws
Exception {
String beginDate =
"2016-07-16"
;
//開始時間
String endDate =
"2016-07-25"
;
//結束時間
SimpleDateFormat sdf =
new
SimpleDateFormat(
"yyyy-MM-dd"
);
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(beginDate));
for
(
long
d = cal.getTimeInMillis(); d <= sdf.parse(endDate).getTime(); d = get_D_Plaus_1(cal)) {
System.out.println(sdf.format(d));
}
}
public
static
long
get_D_Plaus_1(Calendar c) {
c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) +
1
);
return
c.getTimeInMillis();
}
}
|
運行結果: