您好我需要爲java找到日期之間的StartDate =「2017-01-28」和EndDate =「2017-02-03」的例子我期待OutPut是如何在Java中迭代開始日期以結束日期
2017年1月28日 2017年1月29日 2017年1月30日 2017年1月31日 2017年2月1日 2017年2月2日 2017年2月3日 請幫幫我謝謝..
您好我需要爲java找到日期之間的StartDate =「2017-01-28」和EndDate =「2017-02-03」的例子我期待OutPut是如何在Java中迭代開始日期以結束日期
2017年1月28日 2017年1月29日 2017年1月30日 2017年1月31日 2017年2月1日 2017年2月2日 2017年2月3日 請幫幫我謝謝..
您可以使用Java日曆來實現此目的:
Date start = new Date();
Date end = new Date();
Calendar cStart = Calendar.getInstance(); cStart.setTime(start);
Calendar cEnd = Calendar.getInstance(); cEnd.setTime(end);
while (cStart.before(cEnd)) {
//add one day to date
cStart.add(Calendar.DAY_OF_MONTH, 1);
//do something...
}
嗯,你可以做這樣的事情(使用Joda Time)
for (LocalDate date = startDate; date.isBefore(endDate); date = date.plusDays(1))
{
...
}
我會全力推薦使用約達時間在內置的日期/ Calendar類。
回答Java 8使用包java.time
。
StringBuilder builder = new StringBuilder();
LocalDate startDate = LocalDate.parse("2017-01-28");
LocalDate endDate = LocalDate.parse("2017-02-03");
LocalDate d = startDate;
while (d.isBefore(endDate) || d.equals(endDate)) {
builder.append(d.format(DateTimeFormatter.ISO_DATE)).append(" ");
d = d.plusDays(1);
}
// "2017-01-28 2017-01-29 2017-01-30 2017-01-31 2017-02-01 2017-02-02 2017-02-03"
String result = builder.toString().trim();
你做了什麼? –
我試圖這種類型但不工作的(日期LOCALDATE = FROM日期; date.isBefore(結束日期);日期= date.plusDays(1)) \t \t { \t \t \t } \t –
N,它已經被要求很多次,並在Stackoverflow中回答。 –